blob: 6d56b25c5045a2b8dee68070fae97f45ea58863d [file] [log] [blame]
Chiara Contolia8f69ff2016-07-28 01:06:07 +09001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Chiara Contolia8f69ff2016-07-28 01:06:07 +09003 *
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;
Ray Milkey4f7e3632019-02-19 15:35:20 -080023import org.onosproject.net.FilteredConnectPoint;
Chiara Contolia8f69ff2016-07-28 01:06:07 +090024import org.onosproject.net.intent.ConnectivityIntent;
25import org.onosproject.net.intent.SinglePointToMultiPointIntent;
26import com.fasterxml.jackson.databind.node.ObjectNode;
27
28import java.util.HashSet;
29import java.util.Set;
30
31import static com.google.common.base.Preconditions.checkNotNull;
32import static org.onlab.util.Tools.nullIsIllegal;
33
34/**
35 * Single Point to Multi Point intent codec.
36 */
37public class SinglePointToMultiPointIntentCodec extends JsonCodec<SinglePointToMultiPointIntent> {
38 private static final String INGRESS_POINT = "ingressPoint";
39 private static final String EGRESS_POINT = "egressPoint";
40 private static final String CP_POINTS = "connectPoints";
41
42 @Override
43 public ObjectNode encode(SinglePointToMultiPointIntent intent, CodecContext context) {
44 checkNotNull(intent, "Single Point to Multi Point intent cannot be null");
45
46 final JsonCodec<ConnectivityIntent> connectivityIntentCodec =
47 context.codec(ConnectivityIntent.class);
48 final ObjectNode result = connectivityIntentCodec.encode(intent, context);
49
50 final JsonCodec<ConnectPoint> connectPointCodec =
51 context.codec(ConnectPoint.class);
52 final ObjectNode ingress =
53 connectPointCodec.encode(intent.ingressPoint(), context);
54
55 final ObjectNode result2 = context.mapper().createObjectNode();
56 final ArrayNode jsonconnectPoints = result2.putArray(CP_POINTS);
57
58 if (intent.egressPoints() != null) {
59 for (final ConnectPoint cp : intent.egressPoints()) {
60 jsonconnectPoints.add(connectPointCodec.encode(cp, context));
61 }
62 result.set(EGRESS_POINT, jsonconnectPoints);
63 }
64 result.set(INGRESS_POINT, ingress);
65
66 return result;
67 }
68
69 @Override
70 public SinglePointToMultiPointIntent decode(ObjectNode json, CodecContext context) {
71 SinglePointToMultiPointIntent.Builder builder = SinglePointToMultiPointIntent.builder();
72
73 IntentCodec.intentAttributes(json, context, builder);
74 ConnectivityIntentCodec.intentAttributes(json, context, builder);
75
76 ObjectNode ingressJson = nullIsIllegal(get(json, INGRESS_POINT),
77 INGRESS_POINT + IntentCodec.MISSING_MEMBER_MESSAGE);
78 ConnectPoint ingress = context.codec(ConnectPoint.class)
79 .decode(ingressJson, context);
Ray Milkey4f7e3632019-02-19 15:35:20 -080080 builder.filteredIngressPoint(new FilteredConnectPoint(ingress));
Chiara Contolia8f69ff2016-07-28 01:06:07 +090081
82 ObjectNode egressJson = nullIsIllegal(get(json, EGRESS_POINT),
83 EGRESS_POINT + IntentCodec.MISSING_MEMBER_MESSAGE);
84 if (egressJson != null) {
85 final JsonCodec<ConnectPoint> connectPointCodec =
86 context.codec(ConnectPoint.class);
87 JsonNode connectPointsJson = get(json, EGRESS_POINT).get(CP_POINTS);
88
Ray Milkey4f7e3632019-02-19 15:35:20 -080089 Set<FilteredConnectPoint> egressCp = new HashSet<>();
Chiara Contolia8f69ff2016-07-28 01:06:07 +090090 if (connectPointsJson != null) {
91 for (int i = 0; i < connectPointsJson.size(); i++) {
Ray Milkey4f7e3632019-02-19 15:35:20 -080092 ConnectPoint cp = connectPointCodec.decode(get(connectPointsJson, i), context);
93 egressCp.add(new FilteredConnectPoint(cp));
Chiara Contolia8f69ff2016-07-28 01:06:07 +090094 }
Ray Milkey4f7e3632019-02-19 15:35:20 -080095 builder.filteredEgressPoints(egressCp);
Chiara Contolia8f69ff2016-07-28 01:06:07 +090096 }
97 }
98
99 return builder.build();
100 }
101}