blob: eb5305e00d19828c0c00a82a954630333d4e7609 [file] [log] [blame]
Ray Milkeyc95bb9d2015-01-06 10:28:24 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Ray Milkeyc95bb9d2015-01-06 10:28:24 -08003 *
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 */
16package org.onosproject.codec.impl;
17
18import org.onosproject.codec.CodecContext;
19import org.onosproject.codec.JsonCodec;
20import org.onosproject.net.ConnectPoint;
Ray Milkeya2cf3a12018-02-15 16:13:56 -080021import org.onosproject.net.FilteredConnectPoint;
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080022import org.onosproject.net.intent.ConnectivityIntent;
23import org.onosproject.net.intent.PointToPointIntent;
24
25import com.fasterxml.jackson.databind.node.ObjectNode;
26
27import static com.google.common.base.Preconditions.checkNotNull;
Ray Milkeyb82c42b2015-06-30 09:42:20 -070028import static org.onlab.util.Tools.nullIsIllegal;
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080029
30/**
31 * Point to point intent codec.
32 */
Ray Milkey540b2ce2015-02-04 17:50:20 -080033public final class PointToPointIntentCodec extends JsonCodec<PointToPointIntent> {
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080034
Ray Milkeyb82c42b2015-06-30 09:42:20 -070035 private static final String INGRESS_POINT = "ingressPoint";
36 private static final String EGRESS_POINT = "egressPoint";
37
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080038 @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 Milkeya2cf3a12018-02-15 16:13:56 -080049 connectPointCodec.encode(intent.filteredIngressPoint().connectPoint(), context);
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080050 final ObjectNode egress =
Ray Milkeya2cf3a12018-02-15 16:13:56 -080051 connectPointCodec.encode(intent.filteredEgressPoint().connectPoint(), context);
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080052
Ray Milkeyb82c42b2015-06-30 09:42:20 -070053 result.set(INGRESS_POINT, ingress);
54 result.set(EGRESS_POINT, egress);
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080055
56 return result;
57 }
Ray Milkeyb82c42b2015-06-30 09:42:20 -070058
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 Milkeya2cf3a12018-02-15 16:13:56 -080071 builder.filteredIngressPoint(new FilteredConnectPoint(ingress));
Ray Milkeyb82c42b2015-06-30 09:42:20 -070072
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 Milkeya2cf3a12018-02-15 16:13:56 -080077 builder.filteredEgressPoint(new FilteredConnectPoint(egress));
Ray Milkeyb82c42b2015-06-30 09:42:20 -070078
79 return builder.build();
80 }
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080081}