nassima toumi | 9576131 | 2017-05-23 14:00:33 +0300 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 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.MultiPointToSinglePointIntent; |
| 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 | * Multi Point to Single Point intent codec. |
| 35 | */ |
| 36 | public class MultiPointToSinglePointIntentCodec extends JsonCodec<MultiPointToSinglePointIntent> { |
| 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(MultiPointToSinglePointIntent intent, CodecContext context) { |
| 43 | checkNotNull(intent, "Multi Point to Single 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 egress = |
| 52 | connectPointCodec.encode(intent.egressPoint(), context); |
| 53 | |
Aris C. Risdianto | a579c8f | 2017-04-13 19:38:46 +0900 | [diff] [blame] | 54 | // Check ingress are not null and not contain egress |
| 55 | ObjectNode objectCP = context.mapper().createObjectNode(); |
| 56 | ArrayNode jsonconnectPoints = objectCP.putArray(CP_POINTS); |
nassima toumi | 9576131 | 2017-05-23 14:00:33 +0300 | [diff] [blame] | 57 | |
| 58 | if (intent.ingressPoints() != null) { |
| 59 | for (final ConnectPoint cp : intent.ingressPoints()) { |
| 60 | jsonconnectPoints.add(connectPointCodec.encode(cp, context)); |
| 61 | } |
| 62 | result.set(INGRESS_POINT, jsonconnectPoints); |
| 63 | } |
| 64 | result.set(EGRESS_POINT, egress); |
nassima toumi | 9576131 | 2017-05-23 14:00:33 +0300 | [diff] [blame] | 65 | return result; |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public MultiPointToSinglePointIntent decode(ObjectNode json, CodecContext context) { |
| 70 | MultiPointToSinglePointIntent.Builder builder = MultiPointToSinglePointIntent.builder(); |
nassima toumi | 9576131 | 2017-05-23 14:00:33 +0300 | [diff] [blame] | 71 | IntentCodec.intentAttributes(json, context, builder); |
| 72 | ConnectivityIntentCodec.intentAttributes(json, context, builder); |
| 73 | |
Aris C. Risdianto | a579c8f | 2017-04-13 19:38:46 +0900 | [diff] [blame] | 74 | ArrayNode ingressJson = nullIsIllegal((ArrayNode) json.get(INGRESS_POINT), |
| 75 | INGRESS_POINT + IntentCodec.MISSING_MEMBER_MESSAGE); |
nassima toumi | 9576131 | 2017-05-23 14:00:33 +0300 | [diff] [blame] | 76 | |
nassima toumi | 9576131 | 2017-05-23 14:00:33 +0300 | [diff] [blame] | 77 | if (ingressJson != null) { |
| 78 | final JsonCodec<ConnectPoint> connectPointCodec = |
| 79 | context.codec(ConnectPoint.class); |
Aris C. Risdianto | a579c8f | 2017-04-13 19:38:46 +0900 | [diff] [blame] | 80 | JsonNode connectPointsJson = json.get(INGRESS_POINT); |
nassima toumi | 9576131 | 2017-05-23 14:00:33 +0300 | [diff] [blame] | 81 | |
| 82 | Set<ConnectPoint> ingressCp = new HashSet<ConnectPoint>(); |
| 83 | if (connectPointsJson != null) { |
Aris C. Risdianto | a579c8f | 2017-04-13 19:38:46 +0900 | [diff] [blame] | 84 | for (int i = 0; i < connectPointsJson.size(); i++) { |
| 85 | ingressCp.add(connectPointCodec.decode(get(connectPointsJson, i), |
| 86 | context)); |
nassima toumi | 9576131 | 2017-05-23 14:00:33 +0300 | [diff] [blame] | 87 | } |
| 88 | builder.ingressPoints(ingressCp); |
| 89 | } |
| 90 | } |
| 91 | |
Aris C. Risdianto | a579c8f | 2017-04-13 19:38:46 +0900 | [diff] [blame] | 92 | ObjectNode egressJson = nullIsIllegal(get(json, EGRESS_POINT), |
| 93 | EGRESS_POINT + IntentCodec.MISSING_MEMBER_MESSAGE); |
| 94 | ConnectPoint egress = context.codec(ConnectPoint.class) |
| 95 | .decode(egressJson, context); |
| 96 | builder.egressPoint(egress); |
| 97 | |
nassima toumi | 9576131 | 2017-05-23 14:00:33 +0300 | [diff] [blame] | 98 | return builder.build(); |
| 99 | } |
| 100 | } |