blob: b465f57e6c0abbe4aae82a6e2673c34377c3f6b8 [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;
Ray Milkey27cff8c2018-03-05 14:58:26 -080023import org.onosproject.net.FilteredConnectPoint;
nassima toumi95761312017-05-23 14:00:33 +030024import org.onosproject.net.intent.ConnectivityIntent;
25import org.onosproject.net.intent.MultiPointToSinglePointIntent;
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 * Multi Point to Single Point intent codec.
36 */
37public class MultiPointToSinglePointIntentCodec extends JsonCodec<MultiPointToSinglePointIntent> {
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(MultiPointToSinglePointIntent intent, CodecContext context) {
44 checkNotNull(intent, "Multi Point to Single 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 egress =
53 connectPointCodec.encode(intent.egressPoint(), context);
54
Aris C. Risdiantoa579c8f2017-04-13 19:38:46 +090055 // Check ingress are not null and not contain egress
56 ObjectNode objectCP = context.mapper().createObjectNode();
57 ArrayNode jsonconnectPoints = objectCP.putArray(CP_POINTS);
nassima toumi95761312017-05-23 14:00:33 +030058
59 if (intent.ingressPoints() != null) {
60 for (final ConnectPoint cp : intent.ingressPoints()) {
61 jsonconnectPoints.add(connectPointCodec.encode(cp, context));
62 }
63 result.set(INGRESS_POINT, jsonconnectPoints);
64 }
65 result.set(EGRESS_POINT, egress);
nassima toumi95761312017-05-23 14:00:33 +030066 return result;
67 }
68
69 @Override
70 public MultiPointToSinglePointIntent decode(ObjectNode json, CodecContext context) {
71 MultiPointToSinglePointIntent.Builder builder = MultiPointToSinglePointIntent.builder();
nassima toumi95761312017-05-23 14:00:33 +030072 IntentCodec.intentAttributes(json, context, builder);
73 ConnectivityIntentCodec.intentAttributes(json, context, builder);
74
Aris C. Risdiantoa579c8f2017-04-13 19:38:46 +090075 ArrayNode ingressJson = nullIsIllegal((ArrayNode) json.get(INGRESS_POINT),
76 INGRESS_POINT + IntentCodec.MISSING_MEMBER_MESSAGE);
nassima toumi95761312017-05-23 14:00:33 +030077
nassima toumi95761312017-05-23 14:00:33 +030078 if (ingressJson != null) {
79 final JsonCodec<ConnectPoint> connectPointCodec =
80 context.codec(ConnectPoint.class);
Aris C. Risdiantoa579c8f2017-04-13 19:38:46 +090081 JsonNode connectPointsJson = json.get(INGRESS_POINT);
nassima toumi95761312017-05-23 14:00:33 +030082
Ray Milkey27cff8c2018-03-05 14:58:26 -080083 Set<FilteredConnectPoint> ingressCp = new HashSet<>();
nassima toumi95761312017-05-23 14:00:33 +030084 if (connectPointsJson != null) {
Aris C. Risdiantoa579c8f2017-04-13 19:38:46 +090085 for (int i = 0; i < connectPointsJson.size(); i++) {
Ray Milkey27cff8c2018-03-05 14:58:26 -080086 ingressCp.add(new FilteredConnectPoint(connectPointCodec.decode(get(connectPointsJson, i),
87 context)));
nassima toumi95761312017-05-23 14:00:33 +030088 }
Ray Milkey27cff8c2018-03-05 14:58:26 -080089 builder.filteredIngressPoints(ingressCp);
nassima toumi95761312017-05-23 14:00:33 +030090 }
91 }
92
Aris C. Risdiantoa579c8f2017-04-13 19:38:46 +090093 ObjectNode egressJson = nullIsIllegal(get(json, EGRESS_POINT),
94 EGRESS_POINT + IntentCodec.MISSING_MEMBER_MESSAGE);
95 ConnectPoint egress = context.codec(ConnectPoint.class)
96 .decode(egressJson, context);
Ray Milkey27cff8c2018-03-05 14:58:26 -080097 builder.filteredEgressPoint(new FilteredConnectPoint(egress));
Aris C. Risdiantoa579c8f2017-04-13 19:38:46 +090098
nassima toumi95761312017-05-23 14:00:33 +030099 return builder.build();
100 }
101}