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; |
Ray Milkey | 27cff8c | 2018-03-05 14:58:26 -0800 | [diff] [blame] | 23 | import org.onosproject.net.FilteredConnectPoint; |
nassima toumi | 9576131 | 2017-05-23 14:00:33 +0300 | [diff] [blame] | 24 | import org.onosproject.net.intent.ConnectivityIntent; |
| 25 | import org.onosproject.net.intent.MultiPointToSinglePointIntent; |
| 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 | * Multi Point to Single Point intent codec. |
| 36 | */ |
| 37 | public 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. Risdianto | a579c8f | 2017-04-13 19:38:46 +0900 | [diff] [blame] | 55 | // Check ingress are not null and not contain egress |
| 56 | ObjectNode objectCP = context.mapper().createObjectNode(); |
| 57 | ArrayNode jsonconnectPoints = objectCP.putArray(CP_POINTS); |
nassima toumi | 9576131 | 2017-05-23 14:00:33 +0300 | [diff] [blame] | 58 | |
| 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 toumi | 9576131 | 2017-05-23 14:00:33 +0300 | [diff] [blame] | 66 | return result; |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public MultiPointToSinglePointIntent decode(ObjectNode json, CodecContext context) { |
| 71 | MultiPointToSinglePointIntent.Builder builder = MultiPointToSinglePointIntent.builder(); |
nassima toumi | 9576131 | 2017-05-23 14:00:33 +0300 | [diff] [blame] | 72 | IntentCodec.intentAttributes(json, context, builder); |
| 73 | ConnectivityIntentCodec.intentAttributes(json, context, builder); |
| 74 | |
Aris C. Risdianto | a579c8f | 2017-04-13 19:38:46 +0900 | [diff] [blame] | 75 | ArrayNode ingressJson = nullIsIllegal((ArrayNode) json.get(INGRESS_POINT), |
| 76 | INGRESS_POINT + IntentCodec.MISSING_MEMBER_MESSAGE); |
nassima toumi | 9576131 | 2017-05-23 14:00:33 +0300 | [diff] [blame] | 77 | |
nassima toumi | 9576131 | 2017-05-23 14:00:33 +0300 | [diff] [blame] | 78 | if (ingressJson != null) { |
| 79 | final JsonCodec<ConnectPoint> connectPointCodec = |
| 80 | context.codec(ConnectPoint.class); |
Aris C. Risdianto | a579c8f | 2017-04-13 19:38:46 +0900 | [diff] [blame] | 81 | JsonNode connectPointsJson = json.get(INGRESS_POINT); |
nassima toumi | 9576131 | 2017-05-23 14:00:33 +0300 | [diff] [blame] | 82 | |
Ray Milkey | 27cff8c | 2018-03-05 14:58:26 -0800 | [diff] [blame] | 83 | Set<FilteredConnectPoint> ingressCp = new HashSet<>(); |
nassima toumi | 9576131 | 2017-05-23 14:00:33 +0300 | [diff] [blame] | 84 | if (connectPointsJson != null) { |
Aris C. Risdianto | a579c8f | 2017-04-13 19:38:46 +0900 | [diff] [blame] | 85 | for (int i = 0; i < connectPointsJson.size(); i++) { |
Ray Milkey | 27cff8c | 2018-03-05 14:58:26 -0800 | [diff] [blame] | 86 | ingressCp.add(new FilteredConnectPoint(connectPointCodec.decode(get(connectPointsJson, i), |
| 87 | context))); |
nassima toumi | 9576131 | 2017-05-23 14:00:33 +0300 | [diff] [blame] | 88 | } |
Ray Milkey | 27cff8c | 2018-03-05 14:58:26 -0800 | [diff] [blame] | 89 | builder.filteredIngressPoints(ingressCp); |
nassima toumi | 9576131 | 2017-05-23 14:00:33 +0300 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | |
Aris C. Risdianto | a579c8f | 2017-04-13 19:38:46 +0900 | [diff] [blame] | 93 | 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 Milkey | 27cff8c | 2018-03-05 14:58:26 -0800 | [diff] [blame] | 97 | builder.filteredEgressPoint(new FilteredConnectPoint(egress)); |
Aris C. Risdianto | a579c8f | 2017-04-13 19:38:46 +0900 | [diff] [blame] | 98 | |
nassima toumi | 9576131 | 2017-05-23 14:00:33 +0300 | [diff] [blame] | 99 | return builder.build(); |
| 100 | } |
| 101 | } |