blob: a0ae92229a8369d9ce7f4828a9eb8a35240d1ce9 [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
Chiara Contolia8f69ff2016-07-28 01:06:07 +090018import com.fasterxml.jackson.databind.node.ArrayNode;
Ray Milkeyb48533e2019-04-08 13:56:32 -070019import com.fasterxml.jackson.databind.node.ObjectNode;
Chiara Contolia8f69ff2016-07-28 01:06:07 +090020import 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;
Chiara Contolia8f69ff2016-07-28 01:06:07 +090026
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 * Single Point to Multi Point intent codec.
35 */
36public class SinglePointToMultiPointIntentCodec extends JsonCodec<SinglePointToMultiPointIntent> {
37 private static final String INGRESS_POINT = "ingressPoint";
38 private static final String EGRESS_POINT = "egressPoint";
Chiara Contolia8f69ff2016-07-28 01:06:07 +090039
40 @Override
41 public ObjectNode encode(SinglePointToMultiPointIntent intent, CodecContext context) {
42 checkNotNull(intent, "Single Point to Multi Point intent cannot be null");
43
44 final JsonCodec<ConnectivityIntent> connectivityIntentCodec =
45 context.codec(ConnectivityIntent.class);
46 final ObjectNode result = connectivityIntentCodec.encode(intent, context);
47
48 final JsonCodec<ConnectPoint> connectPointCodec =
49 context.codec(ConnectPoint.class);
50 final ObjectNode ingress =
51 connectPointCodec.encode(intent.ingressPoint(), context);
52
Ray Milkeyb48533e2019-04-08 13:56:32 -070053 final ArrayNode jsonconnectPoints = context.mapper().createArrayNode();
Chiara Contolia8f69ff2016-07-28 01:06:07 +090054
55 if (intent.egressPoints() != null) {
56 for (final ConnectPoint cp : intent.egressPoints()) {
57 jsonconnectPoints.add(connectPointCodec.encode(cp, context));
58 }
59 result.set(EGRESS_POINT, jsonconnectPoints);
60 }
61 result.set(INGRESS_POINT, ingress);
62
63 return result;
64 }
65
66 @Override
67 public SinglePointToMultiPointIntent decode(ObjectNode json, CodecContext context) {
68 SinglePointToMultiPointIntent.Builder builder = SinglePointToMultiPointIntent.builder();
69
70 IntentCodec.intentAttributes(json, context, builder);
71 ConnectivityIntentCodec.intentAttributes(json, context, builder);
72
73 ObjectNode ingressJson = nullIsIllegal(get(json, INGRESS_POINT),
74 INGRESS_POINT + IntentCodec.MISSING_MEMBER_MESSAGE);
75 ConnectPoint ingress = context.codec(ConnectPoint.class)
76 .decode(ingressJson, context);
Ray Milkey4f7e3632019-02-19 15:35:20 -080077 builder.filteredIngressPoint(new FilteredConnectPoint(ingress));
Chiara Contolia8f69ff2016-07-28 01:06:07 +090078
Ray Milkeyb48533e2019-04-08 13:56:32 -070079 ArrayNode egressJson = nullIsIllegal((ArrayNode) json.get(EGRESS_POINT),
80 EGRESS_POINT + IntentCodec.MISSING_MEMBER_MESSAGE);
Chiara Contolia8f69ff2016-07-28 01:06:07 +090081
Ray Milkeyb48533e2019-04-08 13:56:32 -070082 final JsonCodec<ConnectPoint> connectPointCodec =
83 context.codec(ConnectPoint.class);
84
85 Set<FilteredConnectPoint> egressCp = new HashSet<>();
86
87 for (int i = 0; i < egressJson.size(); i++) {
88 ConnectPoint cp = connectPointCodec.decode(get(egressJson, i), context);
89 egressCp.add(new FilteredConnectPoint(cp));
Chiara Contolia8f69ff2016-07-28 01:06:07 +090090 }
Ray Milkeyb48533e2019-04-08 13:56:32 -070091 builder.filteredEgressPoints(egressCp);
Chiara Contolia8f69ff2016-07-28 01:06:07 +090092
93 return builder.build();
94 }
95}