Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [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 org.onosproject.codec.CodecContext; |
| 19 | import org.onosproject.codec.JsonCodec; |
| 20 | import org.onosproject.net.ConnectPoint; |
Ray Milkey | a2cf3a1 | 2018-02-15 16:13:56 -0800 | [diff] [blame] | 21 | import org.onosproject.net.FilteredConnectPoint; |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 22 | import org.onosproject.net.intent.ConnectivityIntent; |
| 23 | import org.onosproject.net.intent.PointToPointIntent; |
| 24 | |
| 25 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 26 | |
| 27 | import static com.google.common.base.Preconditions.checkNotNull; |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 28 | import static org.onlab.util.Tools.nullIsIllegal; |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * Point to point intent codec. |
| 32 | */ |
Ray Milkey | 540b2ce | 2015-02-04 17:50:20 -0800 | [diff] [blame] | 33 | public final class PointToPointIntentCodec extends JsonCodec<PointToPointIntent> { |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 34 | |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 35 | private static final String INGRESS_POINT = "ingressPoint"; |
| 36 | private static final String EGRESS_POINT = "egressPoint"; |
| 37 | |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 38 | @Override |
| 39 | public ObjectNode encode(PointToPointIntent intent, CodecContext context) { |
| 40 | checkNotNull(intent, "Point to point intent cannot be null"); |
| 41 | |
| 42 | final JsonCodec<ConnectivityIntent> connectivityIntentCodec = |
| 43 | context.codec(ConnectivityIntent.class); |
| 44 | final ObjectNode result = connectivityIntentCodec.encode(intent, context); |
| 45 | |
| 46 | final JsonCodec<ConnectPoint> connectPointCodec = |
| 47 | context.codec(ConnectPoint.class); |
| 48 | final ObjectNode ingress = |
Ray Milkey | a2cf3a1 | 2018-02-15 16:13:56 -0800 | [diff] [blame] | 49 | connectPointCodec.encode(intent.filteredIngressPoint().connectPoint(), context); |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 50 | final ObjectNode egress = |
Ray Milkey | a2cf3a1 | 2018-02-15 16:13:56 -0800 | [diff] [blame] | 51 | connectPointCodec.encode(intent.filteredEgressPoint().connectPoint(), context); |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 52 | |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 53 | result.set(INGRESS_POINT, ingress); |
| 54 | result.set(EGRESS_POINT, egress); |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 55 | |
| 56 | return result; |
| 57 | } |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 58 | |
| 59 | |
| 60 | @Override |
| 61 | public PointToPointIntent decode(ObjectNode json, CodecContext context) { |
| 62 | PointToPointIntent.Builder builder = PointToPointIntent.builder(); |
| 63 | |
| 64 | IntentCodec.intentAttributes(json, context, builder); |
| 65 | ConnectivityIntentCodec.intentAttributes(json, context, builder); |
| 66 | |
| 67 | ObjectNode ingressJson = nullIsIllegal(get(json, INGRESS_POINT), |
| 68 | INGRESS_POINT + IntentCodec.MISSING_MEMBER_MESSAGE); |
| 69 | ConnectPoint ingress = context.codec(ConnectPoint.class) |
| 70 | .decode(ingressJson, context); |
Ray Milkey | a2cf3a1 | 2018-02-15 16:13:56 -0800 | [diff] [blame] | 71 | builder.filteredIngressPoint(new FilteredConnectPoint(ingress)); |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 72 | |
| 73 | ObjectNode egressJson = nullIsIllegal(get(json, EGRESS_POINT), |
| 74 | EGRESS_POINT + IntentCodec.MISSING_MEMBER_MESSAGE); |
| 75 | ConnectPoint egress = context.codec(ConnectPoint.class) |
| 76 | .decode(egressJson, context); |
Ray Milkey | a2cf3a1 | 2018-02-15 16:13:56 -0800 | [diff] [blame] | 77 | builder.filteredEgressPoint(new FilteredConnectPoint(egress)); |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 78 | |
| 79 | return builder.build(); |
| 80 | } |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 81 | } |