blob: 42168042aab80977334ebb212336360243eddc73 [file] [log] [blame]
nassima toumi95761312017-05-23 14:00:33 +03001/*
2 * Copyright 2017-present Open Networking Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.codec.impl;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.node.ArrayNode;
20import org.onosproject.codec.CodecContext;
21import org.onosproject.codec.JsonCodec;
22import org.onosproject.net.ConnectPoint;
23import org.onosproject.net.intent.ConnectivityIntent;
24import org.onosproject.net.intent.MultiPointToSinglePointIntent;
25import com.fasterxml.jackson.databind.node.ObjectNode;
26
27import java.util.HashSet;
28import java.util.Set;
29
30import static com.google.common.base.Preconditions.checkNotNull;
31import static org.onlab.util.Tools.nullIsIllegal;
32
33/**
34 * Multi Point to Single Point intent codec.
35 */
36public class MultiPointToSinglePointIntentCodec extends JsonCodec<MultiPointToSinglePointIntent> {
37 private static final String INGRESS_POINT = "ingressPoint";
38 private static final String EGRESS_POINT = "egressPoint";
39 private static final String CP_POINTS = "connectPoints";
40
41 @Override
42 public ObjectNode encode(MultiPointToSinglePointIntent intent, CodecContext context) {
43 checkNotNull(intent, "Multi Point to Single Point intent cannot be null");
44
45 final JsonCodec<ConnectivityIntent> connectivityIntentCodec =
46 context.codec(ConnectivityIntent.class);
47 final ObjectNode result = connectivityIntentCodec.encode(intent, context);
48
49 final JsonCodec<ConnectPoint> connectPointCodec =
50 context.codec(ConnectPoint.class);
51 final ObjectNode egress =
52 connectPointCodec.encode(intent.egressPoint(), context);
53
Aris C. Risdiantoa579c8f2017-04-13 19:38:46 +090054 // Check ingress are not null and not contain egress
55 ObjectNode objectCP = context.mapper().createObjectNode();
56 ArrayNode jsonconnectPoints = objectCP.putArray(CP_POINTS);
nassima toumi95761312017-05-23 14:00:33 +030057
58 if (intent.ingressPoints() != null) {
59 for (final ConnectPoint cp : intent.ingressPoints()) {
60 jsonconnectPoints.add(connectPointCodec.encode(cp, context));
61 }
62 result.set(INGRESS_POINT, jsonconnectPoints);
63 }
64 result.set(EGRESS_POINT, egress);
nassima toumi95761312017-05-23 14:00:33 +030065 return result;
66 }
67
68 @Override
69 public MultiPointToSinglePointIntent decode(ObjectNode json, CodecContext context) {
70 MultiPointToSinglePointIntent.Builder builder = MultiPointToSinglePointIntent.builder();
nassima toumi95761312017-05-23 14:00:33 +030071 IntentCodec.intentAttributes(json, context, builder);
72 ConnectivityIntentCodec.intentAttributes(json, context, builder);
73
Aris C. Risdiantoa579c8f2017-04-13 19:38:46 +090074 ArrayNode ingressJson = nullIsIllegal((ArrayNode) json.get(INGRESS_POINT),
75 INGRESS_POINT + IntentCodec.MISSING_MEMBER_MESSAGE);
nassima toumi95761312017-05-23 14:00:33 +030076
nassima toumi95761312017-05-23 14:00:33 +030077 if (ingressJson != null) {
78 final JsonCodec<ConnectPoint> connectPointCodec =
79 context.codec(ConnectPoint.class);
Aris C. Risdiantoa579c8f2017-04-13 19:38:46 +090080 JsonNode connectPointsJson = json.get(INGRESS_POINT);
nassima toumi95761312017-05-23 14:00:33 +030081
82 Set<ConnectPoint> ingressCp = new HashSet<ConnectPoint>();
83 if (connectPointsJson != null) {
Aris C. Risdiantoa579c8f2017-04-13 19:38:46 +090084 for (int i = 0; i < connectPointsJson.size(); i++) {
85 ingressCp.add(connectPointCodec.decode(get(connectPointsJson, i),
86 context));
nassima toumi95761312017-05-23 14:00:33 +030087 }
88 builder.ingressPoints(ingressCp);
89 }
90 }
91
Aris C. Risdiantoa579c8f2017-04-13 19:38:46 +090092 ObjectNode egressJson = nullIsIllegal(get(json, EGRESS_POINT),
93 EGRESS_POINT + IntentCodec.MISSING_MEMBER_MESSAGE);
94 ConnectPoint egress = context.codec(ConnectPoint.class)
95 .decode(egressJson, context);
96 builder.egressPoint(egress);
97
nassima toumi95761312017-05-23 14:00:33 +030098 return builder.build();
99 }
100}