Chiara Contoli | a8f69ff | 2016-07-28 01:06:07 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Chiara Contoli | a8f69ff | 2016-07-28 01:06:07 +0900 | [diff] [blame] | 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 | */ |
| 16 | package org.onosproject.codec.impl; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 20 | import org.onosproject.codec.CodecContext; |
| 21 | import org.onosproject.codec.JsonCodec; |
| 22 | import org.onosproject.net.ConnectPoint; |
Ray Milkey | 4f7e363 | 2019-02-19 15:35:20 -0800 | [diff] [blame^] | 23 | import org.onosproject.net.FilteredConnectPoint; |
Chiara Contoli | a8f69ff | 2016-07-28 01:06:07 +0900 | [diff] [blame] | 24 | import org.onosproject.net.intent.ConnectivityIntent; |
| 25 | import org.onosproject.net.intent.SinglePointToMultiPointIntent; |
| 26 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 27 | |
| 28 | import java.util.HashSet; |
| 29 | import java.util.Set; |
| 30 | |
| 31 | import static com.google.common.base.Preconditions.checkNotNull; |
| 32 | import static org.onlab.util.Tools.nullIsIllegal; |
| 33 | |
| 34 | /** |
| 35 | * Single Point to Multi Point intent codec. |
| 36 | */ |
| 37 | public 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 Milkey | 4f7e363 | 2019-02-19 15:35:20 -0800 | [diff] [blame^] | 80 | builder.filteredIngressPoint(new FilteredConnectPoint(ingress)); |
Chiara Contoli | a8f69ff | 2016-07-28 01:06:07 +0900 | [diff] [blame] | 81 | |
| 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 Milkey | 4f7e363 | 2019-02-19 15:35:20 -0800 | [diff] [blame^] | 89 | Set<FilteredConnectPoint> egressCp = new HashSet<>(); |
Chiara Contoli | a8f69ff | 2016-07-28 01:06:07 +0900 | [diff] [blame] | 90 | if (connectPointsJson != null) { |
| 91 | for (int i = 0; i < connectPointsJson.size(); i++) { |
Ray Milkey | 4f7e363 | 2019-02-19 15:35:20 -0800 | [diff] [blame^] | 92 | ConnectPoint cp = connectPointCodec.decode(get(connectPointsJson, i), context); |
| 93 | egressCp.add(new FilteredConnectPoint(cp)); |
Chiara Contoli | a8f69ff | 2016-07-28 01:06:07 +0900 | [diff] [blame] | 94 | } |
Ray Milkey | 4f7e363 | 2019-02-19 15:35:20 -0800 | [diff] [blame^] | 95 | builder.filteredEgressPoints(egressCp); |
Chiara Contoli | a8f69ff | 2016-07-28 01:06:07 +0900 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |
| 99 | return builder.build(); |
| 100 | } |
| 101 | } |