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; |
| 23 | import org.onosproject.net.intent.ConnectivityIntent; |
| 24 | import org.onosproject.net.intent.SinglePointToMultiPointIntent; |
| 25 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 26 | |
| 27 | import java.util.HashSet; |
| 28 | import java.util.Set; |
| 29 | |
| 30 | import static com.google.common.base.Preconditions.checkNotNull; |
| 31 | import static org.onlab.util.Tools.nullIsIllegal; |
| 32 | |
| 33 | /** |
| 34 | * Single Point to Multi Point intent codec. |
| 35 | */ |
| 36 | public class SinglePointToMultiPointIntentCodec extends JsonCodec<SinglePointToMultiPointIntent> { |
| 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(SinglePointToMultiPointIntent intent, CodecContext context) { |
| 43 | checkNotNull(intent, "Single Point to Multi 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 ingress = |
| 52 | connectPointCodec.encode(intent.ingressPoint(), context); |
| 53 | |
| 54 | final ObjectNode result2 = context.mapper().createObjectNode(); |
| 55 | final ArrayNode jsonconnectPoints = result2.putArray(CP_POINTS); |
| 56 | |
| 57 | if (intent.egressPoints() != null) { |
| 58 | for (final ConnectPoint cp : intent.egressPoints()) { |
| 59 | jsonconnectPoints.add(connectPointCodec.encode(cp, context)); |
| 60 | } |
| 61 | result.set(EGRESS_POINT, jsonconnectPoints); |
| 62 | } |
| 63 | result.set(INGRESS_POINT, ingress); |
| 64 | |
| 65 | return result; |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public SinglePointToMultiPointIntent decode(ObjectNode json, CodecContext context) { |
| 70 | SinglePointToMultiPointIntent.Builder builder = SinglePointToMultiPointIntent.builder(); |
| 71 | |
| 72 | IntentCodec.intentAttributes(json, context, builder); |
| 73 | ConnectivityIntentCodec.intentAttributes(json, context, builder); |
| 74 | |
| 75 | ObjectNode ingressJson = nullIsIllegal(get(json, INGRESS_POINT), |
| 76 | INGRESS_POINT + IntentCodec.MISSING_MEMBER_MESSAGE); |
| 77 | ConnectPoint ingress = context.codec(ConnectPoint.class) |
| 78 | .decode(ingressJson, context); |
| 79 | builder.ingressPoint(ingress); |
| 80 | |
| 81 | ObjectNode egressJson = nullIsIllegal(get(json, EGRESS_POINT), |
| 82 | EGRESS_POINT + IntentCodec.MISSING_MEMBER_MESSAGE); |
| 83 | if (egressJson != null) { |
| 84 | final JsonCodec<ConnectPoint> connectPointCodec = |
| 85 | context.codec(ConnectPoint.class); |
| 86 | JsonNode connectPointsJson = get(json, EGRESS_POINT).get(CP_POINTS); |
| 87 | |
| 88 | Set<ConnectPoint> egressCp = new HashSet<ConnectPoint>(); |
| 89 | if (connectPointsJson != null) { |
| 90 | for (int i = 0; i < connectPointsJson.size(); i++) { |
| 91 | egressCp.add(connectPointCodec.decode(get(connectPointsJson, i), |
| 92 | context)); |
| 93 | } |
| 94 | builder.egressPoints(egressCp); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | return builder.build(); |
| 99 | } |
| 100 | } |