Adding support for MultiPointToSinglePoint intents in the RESTful API

Change-Id: I25018b93ad5ebf7b4e98c30558c9aad89a86e10d
diff --git a/core/common/src/main/java/org/onosproject/codec/impl/CodecManager.java b/core/common/src/main/java/org/onosproject/codec/impl/CodecManager.java
index 2251ff2..7c06b76 100644
--- a/core/common/src/main/java/org/onosproject/codec/impl/CodecManager.java
+++ b/core/common/src/main/java/org/onosproject/codec/impl/CodecManager.java
@@ -78,6 +78,7 @@
 import org.onosproject.net.intent.PointToPointIntent;
 import org.onosproject.net.intent.SinglePointToMultiPointIntent;
 import org.onosproject.net.intent.util.IntentMiniSummary;
+import org.onosproject.net.intent.MultiPointToSinglePointIntent;
 import org.onosproject.net.key.DeviceKey;
 import org.onosproject.net.mcast.McastRoute;
 import org.onosproject.net.meter.Band;
@@ -126,6 +127,7 @@
         registerCodec(IntentMiniSummary.class, new IntentMiniSummaryCodec());
         registerCodec(PointToPointIntent.class, new PointToPointIntentCodec());
         registerCodec(SinglePointToMultiPointIntent.class, new SinglePointToMultiPointIntentCodec());
+        registerCodec(MultiPointToSinglePointIntent.class, new MultiPointToSinglePointIntentCodec());
         registerCodec(Intent.class, new IntentCodec());
         registerCodec(ConnectivityIntent.class, new ConnectivityIntentCodec());
         registerCodec(FlowEntry.class, new FlowEntryCodec());
diff --git a/core/common/src/main/java/org/onosproject/codec/impl/IntentCodec.java b/core/common/src/main/java/org/onosproject/codec/impl/IntentCodec.java
index 36a2c83..69d10bc 100644
--- a/core/common/src/main/java/org/onosproject/codec/impl/IntentCodec.java
+++ b/core/common/src/main/java/org/onosproject/codec/impl/IntentCodec.java
@@ -26,6 +26,7 @@
 import org.onosproject.net.intent.IntentState;
 import org.onosproject.net.intent.HostToHostIntent;
 import org.onosproject.net.intent.SinglePointToMultiPointIntent;
+import org.onosproject.net.intent.MultiPointToSinglePointIntent;
 import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.node.ArrayNode;
 import com.fasterxml.jackson.databind.node.ObjectNode;
@@ -98,6 +99,8 @@
             return context.codec(HostToHostIntent.class).decode(json, context);
         } else if (type.equals(SinglePointToMultiPointIntent.class.getSimpleName())) {
             return context.codec(SinglePointToMultiPointIntent.class).decode(json, context);
+        } else if (type.equals(MultiPointToSinglePointIntent.class.getSimpleName())) {
+            return context.codec(MultiPointToSinglePointIntent.class).decode(json, context);
         }
 
         throw new IllegalArgumentException("Intent type "
diff --git a/core/common/src/main/java/org/onosproject/codec/impl/MultiPointToSinglePointIntentCodec.java b/core/common/src/main/java/org/onosproject/codec/impl/MultiPointToSinglePointIntentCodec.java
new file mode 100644
index 0000000..62d4da0
--- /dev/null
+++ b/core/common/src/main/java/org/onosproject/codec/impl/MultiPointToSinglePointIntentCodec.java
@@ -0,0 +1,100 @@
+/*
+ * Copyright 2017-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.codec.impl;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import org.onosproject.codec.CodecContext;
+import org.onosproject.codec.JsonCodec;
+import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.intent.ConnectivityIntent;
+import org.onosproject.net.intent.MultiPointToSinglePointIntent;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static org.onlab.util.Tools.nullIsIllegal;
+
+/**
+ * Multi Point to Single Point intent codec.
+ */
+public class MultiPointToSinglePointIntentCodec extends JsonCodec<MultiPointToSinglePointIntent> {
+    private static final String INGRESS_POINT = "ingressPoint";
+    private static final String EGRESS_POINT = "egressPoint";
+    private static final String CP_POINTS = "connectPoints";
+
+    @Override
+    public ObjectNode encode(MultiPointToSinglePointIntent intent, CodecContext context) {
+        checkNotNull(intent, "Multi Point to Single Point intent cannot be null");
+
+        final JsonCodec<ConnectivityIntent> connectivityIntentCodec =
+                context.codec(ConnectivityIntent.class);
+        final ObjectNode result = connectivityIntentCodec.encode(intent, context);
+
+        final JsonCodec<ConnectPoint> connectPointCodec =
+                context.codec(ConnectPoint.class);
+        final ObjectNode egress =
+                connectPointCodec.encode(intent.egressPoint(), context);
+
+        final ObjectNode objectCP = context.mapper().createObjectNode();
+        final ArrayNode jsonconnectPoints = objectCP.putArray(CP_POINTS);
+
+        if (intent.ingressPoints() != null) {
+            for (final ConnectPoint cp : intent.ingressPoints()) {
+                jsonconnectPoints.add(connectPointCodec.encode(cp, context));
+            }
+            result.set(INGRESS_POINT, jsonconnectPoints);
+        }
+        result.set(EGRESS_POINT, egress);
+
+        return result;
+    }
+
+    @Override
+    public MultiPointToSinglePointIntent decode(ObjectNode json, CodecContext context) {
+        MultiPointToSinglePointIntent.Builder builder = MultiPointToSinglePointIntent.builder();
+
+        IntentCodec.intentAttributes(json, context, builder);
+        ConnectivityIntentCodec.intentAttributes(json, context, builder);
+
+        ObjectNode egressJson = nullIsIllegal(get(json, EGRESS_POINT),
+                EGRESS_POINT + IntentCodec.MISSING_MEMBER_MESSAGE);
+        ConnectPoint egress = context.codec(ConnectPoint.class)
+                .decode(egressJson, context);
+        builder.egressPoint(egress);
+
+        ObjectNode ingressJson = nullIsIllegal(get(json, INGRESS_POINT),
+                INGRESS_POINT + IntentCodec.MISSING_MEMBER_MESSAGE);
+        if (ingressJson != null) {
+            final JsonCodec<ConnectPoint> connectPointCodec =
+                    context.codec(ConnectPoint.class);
+            JsonNode connectPointsJson = get(json, INGRESS_POINT).get(CP_POINTS);
+
+            Set<ConnectPoint> ingressCp = new HashSet<ConnectPoint>();
+            if (connectPointsJson != null) {
+                for (JsonNode cP : connectPointsJson) {
+                    ingressCp.add(connectPointCodec.decode((ObjectNode) cP,
+                            context));
+                }
+                builder.ingressPoints(ingressCp);
+            }
+        }
+
+        return builder.build();
+    }
+}
diff --git a/web/api/src/main/java/org/onosproject/rest/resources/IntentsWebResource.java b/web/api/src/main/java/org/onosproject/rest/resources/IntentsWebResource.java
index 675581d..7156ce1 100644
--- a/web/api/src/main/java/org/onosproject/rest/resources/IntentsWebResource.java
+++ b/web/api/src/main/java/org/onosproject/rest/resources/IntentsWebResource.java
@@ -22,6 +22,7 @@
 import org.onosproject.net.flow.FlowEntry;
 import org.onosproject.net.flow.FlowRuleService;
 import org.onosproject.net.intent.SinglePointToMultiPointIntent;
+import org.onosproject.net.intent.MultiPointToSinglePointIntent;
 import org.onosproject.net.intent.PointToPointIntent;
 import org.onosproject.net.intent.HostToHostIntent;
 import org.onosproject.net.intent.Intent;
@@ -74,6 +75,8 @@
     private static final String POINT_TO_POINT_INTENT = "PointToPointIntent";
     private static final String SINGLE_TO_MULTI_POINT_INTENT =
             "SinglePointToMultiPointIntent";
+    private static final String MULTI_TO_SINGLE_POINT_INTENT =
+            "MultiPointToSinglePointIntent";
     private static final String INTENT = "Intent";
     private static final String APP_ID = "appId";
     private static final String ID = "id";
@@ -181,6 +184,8 @@
             root = codec(PointToPointIntent.class).encode((PointToPointIntent) intent, this);
         } else if (intent instanceof SinglePointToMultiPointIntent) {
             root = codec(SinglePointToMultiPointIntent.class).encode((SinglePointToMultiPointIntent) intent, this);
+        } else if (intent instanceof MultiPointToSinglePointIntent) {
+            root = codec(MultiPointToSinglePointIntent.class).encode((MultiPointToSinglePointIntent) intent, this);
         } else {
             root = codec(Intent.class).encode(intent, this);
         }
@@ -229,6 +234,8 @@
             root.put(INTENT_TYPE, POINT_TO_POINT_INTENT);
         } else if (intent instanceof SinglePointToMultiPointIntent) {
             root.put(INTENT_TYPE, SINGLE_TO_MULTI_POINT_INTENT);
+        } else if (intent instanceof MultiPointToSinglePointIntent) {
+            root.put(INTENT_TYPE, MULTI_TO_SINGLE_POINT_INTENT);
         } else {
             root.put(INTENT_TYPE, INTENT);
         }