Repository: batfish/pybatfish

Issue:
Pretty-printing ICMP flows should not include src/dst port 
Example of current rendering:
```
Src IP: 2.1.2.1
Src Port: 0
Dst IP: 2.1.1.1
Dst Port: 0
IP Protocol: ICMP
Start Location: as2core1
```

Discussion:
batfish-bot: 

<!-- Reviewable:start -->
This change is [<img src="https://reviewable.io/review_button.png" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/batfish/pybatfish/190)
<!-- Reviewable:end -->


Patch:
diff --git a/jupyter_notebooks/README.md b/jupyter_notebooks/README.md
index 2a6435a..444001f 100644
--- a/jupyter_notebooks/README.md
+++ b/jupyter_notebooks/README.md
@@ -11,7 +11,9 @@ The current list of notebooks are:
    - Shows how to use analyze ACLs and firewalls rules using Batfish. 
 - [Provably Safe ACL and Firewall Changes](Provably%20Safe%20ACL%20and%20Firewall%20Changes.ipynb) [[video](https://www.youtube.com/watch?v=MJYLVL9UOWk)]
    - Shows a workflow to ensure changes to ACLs and firewall rules are correct and safe.
-
+- [Introduction to Forwarding Analysis](Introduction%20to%20Forwarding%20Analysis.ipynb)
+   - Shows how forwarding paths can be analyzed using traceroute and reachability queries.
+   
 We will continue adding notebooks that demonstrating additional use cases of Batfish. 
 
 ------
diff --git a/pybatfish/datamodel/flow.py b/pybatfish/datamodel/flow.py
index fd25b71..e92dca7 100644
--- a/pybatfish/datamodel/flow.py
+++ b/pybatfish/datamodel/flow.py
@@ -113,15 +113,13 @@ class Flow(DataModelElement):
         iface_str = self._iface_str()
         vrf_str = self._vrf_str()
         return \
-            "start={node}{iface}{vrf} [{src_ip}:{src_port}->{dst_ip}:{dst_port}" \
+            "start={node}{iface}{vrf} [{src}->{dst}" \
             " {ip_proto}{dscp}{ecn}{offset}{length}{state}{flags}]".format(
                 node=self.ingressNode,
                 iface=iface_str,
                 vrf=vrf_str,
-                src_ip=self.srcIp,
-                src_port=self.srcPort,
-                dst_ip=self.dstIp,
-                dst_port=self.dstPort,
+                src=self._ip_port(self.srcIp, self.srcPort),
+                dst=self._ip_port(self.dstIp, self.dstPort),
                 ip_proto=self.get_ip_protocol_str(),
                 dscp=(" dscp={}".format(self.dscp) if self.dscp != 0 else ""),
                 ecn=(" ecn={}".format(self.ecn) if self.ecn != 0 else ""),
@@ -164,17 +162,32 @@ class Flow(DataModelElement):
         else:
             return self.ipProtocol
 
+    def _has_ports(self):
+        # type: () -> bool
+        return self.ipProtocol in ['TCP', 'UDP', 'DCCP', 'SCTP']
+
     def _repr_html_(self):
         # type: () -> str
-        return "Src IP: {src_ip}<br>Src Port: {src_port}<br>Dst IP: {dst_ip}<br>Dst Port: {dst_port}<br>IP Protocol: {ip_protocol}<br>Start Location: {node}{iface}{vrf}" \
-            .format(node=self.ingressNode,
-                    iface=escape_html(self._iface_str()),
-                    vrf=escape_html(self._vrf_str()),
-                    src_ip=self.srcIp,
-                    src_port=self.srcPort,
-                    dst_ip=self.dstIp,
-                    dst_port=self.dstPort,
-                    ip_protocol=self.get_ip_protocol_str())
+        lines = []
+        lines.append('Src IP: %s' % self.srcIp)
+        if self._has_ports():
+            lines.append('Src Port: %d' % self.srcPort)
+        lines.append('Dst IP: %s' % self.dstIp)
+        if self._has_ports():
+            lines.append('Dst Port: %d' % self.dstPort)
+        lines.append('IP Protocol: %s' % self.get_ip_protocol_str())
+        lines.append('Start Location: {node}{iface}{vrf}'.format(
+            node=self.ingressNode,
+            iface=self.ingressInterface,
+            vrf=self.ingressVrf))
+        return '<br>'.join(lines)
+
+    def _ip_port(self, ip, port):
+        # type: (str, int) -> str
+        if self._has_ports():
+            return "{ip}:{port}".format(ip=ip, port=port)
+        else:
+            return self.srcIp
 
 
 @attr.s(frozen=True)


Test patch:
diff --git a/tests/datamodel/test_flow.py b/tests/datamodel/test_flow.py
index 28a26ce..a869cd0 100644
--- a/tests/datamodel/test_flow.py
+++ b/tests/datamodel/test_flow.py
@@ -62,7 +62,7 @@ def testFlowDeserialization():
 
     # check the string representation has the essential elements (without forcing a strict format)
     flowStr = str(flow)
-    assert "5.5.1.1:0" in flowStr
+    assert "5.5.1.1" in flowStr
     assert "intface" in flowStr
     assert "vrfAbc" in flowStr
 
@@ -209,5 +209,75 @@ def test_match_tcp_generators():
                                                  MatchTcpFlags.match_rst()]
 
 
+def test_flow_repr_html_ports():
+    # ICMP flows do not have ports
+    flowDict = {
+        "dscp": 0,
+        "dstIp": "2.1.1.1",
+        "dstPort": 0,
+        "ecn": 0,
+        "fragmentOffset": 0,
+        "icmpCode": 255,
+        "icmpVar": 255,
+        "ingressNode": "ingress",
+        "ipProtocol": "ICMP",
+        "packetLength": 0,
+        "srcIp": "5.5.1.1",
+        "srcPort": 0,
+        "state": "NEW",
+        "tag": "BASE",
+        "tcpFlagsAck": 0,
+        "tcpFlagsCwr": 0,
+        "tcpFlagsEce": 0,
+        "tcpFlagsFin": 0,
+        "tcpFlagsPsh": 0,
+        "tcpFlagsRst": 0,
+        "tcpFlagsSyn": 0,
+        "tcpFlagsUrg": 0
+    }
+    assert("Port" not in Flow.from_dict(flowDict)._repr_html_())
+
+    # UDP
+    flowDict['ipProtocol'] = "UDP"
+    assert ("Port" in Flow.from_dict(flowDict)._repr_html_())
+
+
+def test_flow_str_ports():
+    # ICMP flows do not have ports
+    flowDict = {
+        "dscp": 0,
+        "dstIp": "2.1.1.1",
+        "dstPort": 1234,
+        "ecn": 0,
+        "fragmentOffset": 0,
+        "icmpCode": 255,
+        "icmpVar": 255,
+        "ingressNode": "ingress",
+        "ipProtocol": "ICMP",
+        "packetLength": 0,
+        "srcIp": "5.5.1.1",
+        "srcPort": 2345,
+        "state": "NEW",
+        "tag": "BASE",
+        "tcpFlagsAck": 0,
+        "tcpFlagsCwr": 0,
+        "tcpFlagsEce": 0,
+        "tcpFlagsFin": 0,
+        "tcpFlagsPsh": 0,
+        "tcpFlagsRst": 0,
+        "tcpFlagsSyn": 0,
+        "tcpFlagsUrg": 0
+    }
+    str = repr(Flow.from_dict(flowDict))
+    assert("2.1.1.1:1234" not in str)
+    assert("5.5.1.1:2345" not in str)
+
+    # UDP
+    flowDict['ipProtocol'] = "UDP"
+    str = repr(Flow.from_dict(flowDict))
+    assert("2.1.1.1:1234" not in str)
+    assert("5.5.1.1:2345" not in str)
+
+
 if __name__ == "__main__":
     pytest.main()