ONOS-1856: Supports ports (~65535) for the tunnel policy
 - Add some missing Java docs

Change-Id: I0ef750efdb9b667a5b5edbd91cf7b4cc54afd38c
diff --git a/src/main/java/org/onosproject/segmentrouting/SegmentRoutingManager.java b/src/main/java/org/onosproject/segmentrouting/SegmentRoutingManager.java
index 41cb65b..02ed88a 100644
--- a/src/main/java/org/onosproject/segmentrouting/SegmentRoutingManager.java
+++ b/src/main/java/org/onosproject/segmentrouting/SegmentRoutingManager.java
@@ -249,6 +249,12 @@
         return policyHandler.getPolicies();
     }
 
+    /**
+     * Returns the tunnel object with the tunnel ID.
+     *
+     * @param tunnelId Tunnel ID
+     * @return Tunnel reference
+     */
     public Tunnel getTunnel(String tunnelId) {
         return tunnelHandler.getTunnel(tunnelId);
     }
@@ -269,10 +275,12 @@
     }
 
     /**
+     * Returns the next objective ID for the NeighborSet given. If the nextObjectiveID does not exist,
+     * a new one is created and returned.
      *
-     * @param deviceId
-     * @param ns
-     * @return
+     * @param deviceId Device ID
+     * @param ns NegighborSet
+     * @return next objective ID
      */
     public int getNextObjectiveId(DeviceId deviceId, NeighborSet ns) {
 
@@ -287,10 +295,11 @@
     }
 
     /**
+     * Removes the next objective ID.
      *
-     * @param deviceId
-     * @param objectiveId
-     * @return
+     * @param deviceId Device ID
+     * @param objectiveId next objective ID to remove
+     * @return true, if succeeds, false otherwise
      */
     public boolean removeNextObjective(DeviceId deviceId, int objectiveId) {
         return groupHandlerMap.get(deviceId).removeGroup(objectiveId);
diff --git a/src/main/java/org/onosproject/segmentrouting/web/PolicyCodec.java b/src/main/java/org/onosproject/segmentrouting/web/PolicyCodec.java
index 1f9e576..3e2524d 100644
--- a/src/main/java/org/onosproject/segmentrouting/web/PolicyCodec.java
+++ b/src/main/java/org/onosproject/segmentrouting/web/PolicyCodec.java
@@ -26,6 +26,8 @@
 import org.onosproject.net.flow.criteria.Criterion;
 import org.onosproject.net.flow.criteria.IPCriterion;
 import org.onosproject.net.flow.criteria.IPProtocolCriterion;
+import org.onosproject.net.flow.criteria.TcpPortCriterion;
+import org.onosproject.net.flow.criteria.UdpPortCriterion;
 import org.onosproject.segmentrouting.Policy;
 import org.onosproject.segmentrouting.TunnelPolicy;
 
@@ -39,6 +41,8 @@
     private static final String DST_IP = "dst_ip";
     private static final String SRC_IP = "src_ip";
     private static final String PROTO_TYPE = "proto_type";
+    private static final String SRC_PORT = "src_tp_port";
+    private static final String DST_PORT = "dst_tp_port";
 
     @Override
     public ObjectNode encode(Policy policy, CodecContext context) {
@@ -61,6 +65,29 @@
         if (policy.selector().getCriterion(Criterion.Type.IP_PROTO) != null) {
             IPProtocolCriterion protocolCriterion =
                     (IPProtocolCriterion) policy.selector().getCriterion(Criterion.Type.IP_PROTO);
+            result.put(PROTO_TYPE, protocolCriterion.protocol());
+        }
+        if (policy.selector().getCriterion(Criterion.Type.TCP_SRC) != null) {
+            TcpPortCriterion tcpPortCriterion =
+                    (TcpPortCriterion) policy.selector().getCriterion(Criterion.Type.TCP_SRC);
+            result.put(SRC_PORT, tcpPortCriterion.toString());
+        } else if (policy.selector().getCriterion(Criterion.Type.UDP_SRC) != null) {
+            UdpPortCriterion udpPortCriterion =
+                    (UdpPortCriterion) policy.selector().getCriterion(Criterion.Type.UDP_SRC);
+            result.put(SRC_PORT, udpPortCriterion.toString());
+        }
+        if (policy.selector().getCriterion(Criterion.Type.TCP_DST) != null) {
+            TcpPortCriterion tcpPortCriterion =
+                    (TcpPortCriterion) policy.selector().getCriterion(Criterion.Type.TCP_DST);
+            result.put(DST_PORT, tcpPortCriterion.toString());
+        } else if (policy.selector().getCriterion(Criterion.Type.UDP_DST) != null) {
+            UdpPortCriterion udpPortCriterion =
+                    (UdpPortCriterion) policy.selector().getCriterion(Criterion.Type.UDP_DST);
+            result.put(DST_PORT, udpPortCriterion.toString());
+        }
+        if (policy.selector().getCriterion(Criterion.Type.IP_PROTO) != null) {
+            IPProtocolCriterion protocolCriterion =
+                    (IPProtocolCriterion) policy.selector().getCriterion(Criterion.Type.IP_PROTO);
             result.put(PROTO_TYPE, protocolCriterion.toString());
         }
 
@@ -81,6 +108,8 @@
         String srcIp = json.path(SRC_IP).asText();
         String tunnelId = json.path(TUNNEL_ID).asText();
         String protoType = json.path(PROTO_TYPE).asText();
+        short srcPort = json.path(SRC_PORT).shortValue();
+        short dstPort = json.path(DST_PORT).shortValue();
 
         if (tunnelId != null) {
             TrafficSelector.Builder tsb = DefaultTrafficSelector.builder();
@@ -94,8 +123,22 @@
             if (protoType != null && !protoType.isEmpty()) {
                 Short ipProto = Short.valueOf(IpProtocol.valueOf(protoType).value());
                 tsb.matchIPProtocol(ipProto.byteValue());
+                if (IpProtocol.valueOf(protoType).equals(IpProtocol.TCP)) {
+                    if (srcPort != 0) {
+                        tsb.matchTcpSrc(srcPort);
+                    }
+                    if (dstPort != 0) {
+                        tsb.matchTcpDst(dstPort);
+                    }
+                } else if (IpProtocol.valueOf(protoType).equals(IpProtocol.UDP)) {
+                    if (srcPort != 0) {
+                        tsb.matchUdpSrc(srcPort);
+                    }
+                    if (dstPort != 0) {
+                        tsb.matchUdpDst(dstPort);
+                    }
+                }
             }
-
             TunnelPolicy.Builder tpb = TunnelPolicy.builder().setPolicyId(pid);
             if (tunnelId != null) {
                 tpb.setTunnelId(tunnelId);