blob: 62d4da079e8d5981c1b4db1a4de3f980213c4e57 [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
54 final ObjectNode objectCP = context.mapper().createObjectNode();
55 final ArrayNode jsonconnectPoints = objectCP.putArray(CP_POINTS);
56
57 if (intent.ingressPoints() != null) {
58 for (final ConnectPoint cp : intent.ingressPoints()) {
59 jsonconnectPoints.add(connectPointCodec.encode(cp, context));
60 }
61 result.set(INGRESS_POINT, jsonconnectPoints);
62 }
63 result.set(EGRESS_POINT, egress);
64
65 return result;
66 }
67
68 @Override
69 public MultiPointToSinglePointIntent decode(ObjectNode json, CodecContext context) {
70 MultiPointToSinglePointIntent.Builder builder = MultiPointToSinglePointIntent.builder();
71
72 IntentCodec.intentAttributes(json, context, builder);
73 ConnectivityIntentCodec.intentAttributes(json, context, builder);
74
75 ObjectNode egressJson = nullIsIllegal(get(json, EGRESS_POINT),
76 EGRESS_POINT + IntentCodec.MISSING_MEMBER_MESSAGE);
77 ConnectPoint egress = context.codec(ConnectPoint.class)
78 .decode(egressJson, context);
79 builder.egressPoint(egress);
80
81 ObjectNode ingressJson = nullIsIllegal(get(json, INGRESS_POINT),
82 INGRESS_POINT + IntentCodec.MISSING_MEMBER_MESSAGE);
83 if (ingressJson != null) {
84 final JsonCodec<ConnectPoint> connectPointCodec =
85 context.codec(ConnectPoint.class);
86 JsonNode connectPointsJson = get(json, INGRESS_POINT).get(CP_POINTS);
87
88 Set<ConnectPoint> ingressCp = new HashSet<ConnectPoint>();
89 if (connectPointsJson != null) {
90 for (JsonNode cP : connectPointsJson) {
91 ingressCp.add(connectPointCodec.decode((ObjectNode) cP,
92 context));
93 }
94 builder.ingressPoints(ingressCp);
95 }
96 }
97
98 return builder.build();
99 }
100}