From a8f69ff3fdf70a71acf7bfdaee31221e6c58fd01 Mon Sep 17 00:00:00 2001 From: Chiara Contoli Date: Thu, 28 Jul 2016 01:06:07 +0900 Subject: [PATCH] Adding SinglePointToMultiPointIntentCodec for support SP2MP intent over REST Change-Id: Ic04529520ad1719f3d241b3a79d8e0602bfeac4e --- .../onosproject/codec/impl/CodecManager.java | 2 + .../onosproject/codec/impl/IntentCodec.java | 8 +- .../SinglePointToMultiPointIntentCodec.java | 100 ++++++++++++++++++ .../rest/resources/IntentsWebResource.java | 7 +- 4 files changed, 112 insertions(+), 5 deletions(-) create mode 100644 core/common/src/main/java/org/onosproject/codec/impl/SinglePointToMultiPointIntentCodec.java 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 ea2f33914c..da9b8f10d2 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 @@ -63,6 +63,7 @@ import org.onosproject.net.intent.Constraint; import org.onosproject.net.intent.HostToHostIntent; import org.onosproject.net.intent.Intent; import org.onosproject.net.intent.PointToPointIntent; +import org.onosproject.net.intent.SinglePointToMultiPointIntent; import org.onosproject.net.key.DeviceKey; import org.onosproject.net.mcast.McastRoute; import org.onosproject.net.meter.Band; @@ -108,6 +109,7 @@ public class CodecManager implements CodecService { registerCodec(HostLocation.class, new HostLocationCodec()); registerCodec(HostToHostIntent.class, new HostToHostIntentCodec()); registerCodec(PointToPointIntent.class, new PointToPointIntentCodec()); + registerCodec(SinglePointToMultiPointIntent.class, new SinglePointToMultiPointIntentCodec()); 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 448a2e0397..a51665b62c 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 @@ -19,12 +19,12 @@ import org.onosproject.codec.CodecContext; import org.onosproject.codec.JsonCodec; import org.onosproject.core.CoreService; import org.onosproject.net.NetworkResource; -import org.onosproject.net.intent.HostToHostIntent; +import org.onosproject.net.intent.PointToPointIntent; import org.onosproject.net.intent.Intent; import org.onosproject.net.intent.IntentService; import org.onosproject.net.intent.IntentState; -import org.onosproject.net.intent.PointToPointIntent; - +import org.onosproject.net.intent.HostToHostIntent; +import org.onosproject.net.intent.SinglePointToMultiPointIntent; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ObjectNode; @@ -83,6 +83,8 @@ public final class IntentCodec extends JsonCodec { return context.codec(PointToPointIntent.class).decode(json, context); } else if (type.equals(HostToHostIntent.class.getSimpleName())) { return context.codec(HostToHostIntent.class).decode(json, context); + } else if (type.equals(SinglePointToMultiPointIntent.class.getSimpleName())) { + return context.codec(SinglePointToMultiPointIntent.class).decode(json, context); } throw new IllegalArgumentException("Intent type " diff --git a/core/common/src/main/java/org/onosproject/codec/impl/SinglePointToMultiPointIntentCodec.java b/core/common/src/main/java/org/onosproject/codec/impl/SinglePointToMultiPointIntentCodec.java new file mode 100644 index 0000000000..b3d1a25d32 --- /dev/null +++ b/core/common/src/main/java/org/onosproject/codec/impl/SinglePointToMultiPointIntentCodec.java @@ -0,0 +1,100 @@ +/* + * Copyright 2015-present Open Networking Laboratory + * + * 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.SinglePointToMultiPointIntent; +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; + +/** + * Single Point to Multi Point intent codec. + */ +public class SinglePointToMultiPointIntentCodec extends JsonCodec { + 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(SinglePointToMultiPointIntent intent, CodecContext context) { + checkNotNull(intent, "Single Point to Multi Point intent cannot be null"); + + final JsonCodec connectivityIntentCodec = + context.codec(ConnectivityIntent.class); + final ObjectNode result = connectivityIntentCodec.encode(intent, context); + + final JsonCodec connectPointCodec = + context.codec(ConnectPoint.class); + final ObjectNode ingress = + connectPointCodec.encode(intent.ingressPoint(), context); + + final ObjectNode result2 = context.mapper().createObjectNode(); + final ArrayNode jsonconnectPoints = result2.putArray(CP_POINTS); + + if (intent.egressPoints() != null) { + for (final ConnectPoint cp : intent.egressPoints()) { + jsonconnectPoints.add(connectPointCodec.encode(cp, context)); + } + result.set(EGRESS_POINT, jsonconnectPoints); + } + result.set(INGRESS_POINT, ingress); + + return result; + } + + @Override + public SinglePointToMultiPointIntent decode(ObjectNode json, CodecContext context) { + SinglePointToMultiPointIntent.Builder builder = SinglePointToMultiPointIntent.builder(); + + IntentCodec.intentAttributes(json, context, builder); + ConnectivityIntentCodec.intentAttributes(json, context, builder); + + ObjectNode ingressJson = nullIsIllegal(get(json, INGRESS_POINT), + INGRESS_POINT + IntentCodec.MISSING_MEMBER_MESSAGE); + ConnectPoint ingress = context.codec(ConnectPoint.class) + .decode(ingressJson, context); + builder.ingressPoint(ingress); + + ObjectNode egressJson = nullIsIllegal(get(json, EGRESS_POINT), + EGRESS_POINT + IntentCodec.MISSING_MEMBER_MESSAGE); + if (egressJson != null) { + final JsonCodec connectPointCodec = + context.codec(ConnectPoint.class); + JsonNode connectPointsJson = get(json, EGRESS_POINT).get(CP_POINTS); + + Set egressCp = new HashSet(); + if (connectPointsJson != null) { + for (int i = 0; i < connectPointsJson.size(); i++) { + egressCp.add(connectPointCodec.decode(get(connectPointsJson, i), + context)); + } + builder.egressPoints(egressCp); + } + } + + 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 6fc545159d..b22ea08cb1 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 @@ -18,14 +18,15 @@ package org.onosproject.rest.resources; import com.fasterxml.jackson.databind.node.ObjectNode; import org.onosproject.core.ApplicationId; import org.onosproject.core.CoreService; +import org.onosproject.net.intent.SinglePointToMultiPointIntent; +import org.onosproject.net.intent.PointToPointIntent; import org.onosproject.net.intent.HostToHostIntent; import org.onosproject.net.intent.Intent; +import org.onosproject.net.intent.IntentState; import org.onosproject.net.intent.IntentEvent; import org.onosproject.net.intent.IntentListener; import org.onosproject.net.intent.IntentService; -import org.onosproject.net.intent.IntentState; import org.onosproject.net.intent.Key; -import org.onosproject.net.intent.PointToPointIntent; import org.onosproject.rest.AbstractWebResource; import org.slf4j.Logger; @@ -108,6 +109,8 @@ public class IntentsWebResource extends AbstractWebResource { root = codec(HostToHostIntent.class).encode((HostToHostIntent) intent, this); } else if (intent instanceof PointToPointIntent) { root = codec(PointToPointIntent.class).encode((PointToPointIntent) intent, this); + } else if (intent instanceof SinglePointToMultiPointIntent) { + root = codec(SinglePointToMultiPointIntent.class).encode((SinglePointToMultiPointIntent) intent, this); } else { root = codec(Intent.class).encode(intent, this); }