blob: 0ccd15b75e431aa2ef0df54d40778a4b662451fc [file] [log] [blame]
Ray Milkeyc95bb9d2015-01-06 10:28:24 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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
Ray Milkeyb82c42b2015-06-30 09:42:20 -070018import java.util.ArrayList;
19import java.util.stream.IntStream;
20
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080021import org.onosproject.codec.CodecContext;
22import org.onosproject.codec.JsonCodec;
23import org.onosproject.net.flow.TrafficSelector;
24import org.onosproject.net.flow.TrafficTreatment;
25import org.onosproject.net.intent.ConnectivityIntent;
Ray Milkeydb358082015-01-13 16:34:38 -080026import org.onosproject.net.intent.Constraint;
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080027import org.onosproject.net.intent.Intent;
28
Ray Milkeyb82c42b2015-06-30 09:42:20 -070029import com.fasterxml.jackson.databind.JsonNode;
Ray Milkeydb358082015-01-13 16:34:38 -080030import com.fasterxml.jackson.databind.node.ArrayNode;
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080031import com.fasterxml.jackson.databind.node.ObjectNode;
32
33import static com.google.common.base.Preconditions.checkNotNull;
34
35/**
36 * Connectivity intent codec.
37 */
Ray Milkey540b2ce2015-02-04 17:50:20 -080038public final class ConnectivityIntentCodec extends JsonCodec<ConnectivityIntent> {
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080039
Ray Milkeyb82c42b2015-06-30 09:42:20 -070040 private static final String CONSTRAINTS = "constraints";
41 private static final String SELECTOR = "selector";
42 private static final String TREATMENT = "treatment";
43
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080044 @Override
45 public ObjectNode encode(ConnectivityIntent intent, CodecContext context) {
46 checkNotNull(intent, "Connectivity intent cannot be null");
47
48 final JsonCodec<Intent> intentCodec = context.codec(Intent.class);
49 final ObjectNode result = intentCodec.encode(intent, context);
50
51 if (intent.selector() != null) {
52 final JsonCodec<TrafficSelector> selectorCodec =
53 context.codec(TrafficSelector.class);
Ray Milkeyb82c42b2015-06-30 09:42:20 -070054 result.set(SELECTOR, selectorCodec.encode(intent.selector(), context));
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080055 }
56
57 if (intent.treatment() != null) {
58 final JsonCodec<TrafficTreatment> treatmentCodec =
59 context.codec(TrafficTreatment.class);
Ray Milkeyb82c42b2015-06-30 09:42:20 -070060 result.set(TREATMENT, treatmentCodec.encode(intent.treatment(), context));
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080061 }
62
Ray Milkeyb82c42b2015-06-30 09:42:20 -070063 result.put(IntentCodec.PRIORITY, intent.priority());
Ray Milkeyc24cde32015-03-10 18:20:18 -070064
Ray Milkeydb358082015-01-13 16:34:38 -080065 if (intent.constraints() != null) {
Ray Milkeyb82c42b2015-06-30 09:42:20 -070066 final ArrayNode jsonConstraints = result.putArray(CONSTRAINTS);
Ray Milkeydb358082015-01-13 16:34:38 -080067
68 if (intent.constraints() != null) {
Ray Milkeyb9a8a182015-01-20 14:15:35 -080069 final JsonCodec<Constraint> constraintCodec =
70 context.codec(Constraint.class);
Ray Milkeydb358082015-01-13 16:34:38 -080071 for (final Constraint constraint : intent.constraints()) {
Ray Milkeyb9a8a182015-01-20 14:15:35 -080072 final ObjectNode constraintNode =
73 constraintCodec.encode(constraint, context);
74 jsonConstraints.add(constraintNode);
Ray Milkeydb358082015-01-13 16:34:38 -080075 }
76 }
77 }
78
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080079 return result;
80 }
Ray Milkeyb82c42b2015-06-30 09:42:20 -070081
82 /**
83 * Extracts connectivity intent specific attributes from a JSON object
84 * and adds them to a builder.
85 *
86 * @param json root JSON object
87 * @param context code context
88 * @param builder builder to use for storing the attributes. Constraints,
89 * selector and treatment are modified by this call.
90 */
91 public static void intentAttributes(ObjectNode json, CodecContext context,
92 ConnectivityIntent.Builder builder) {
93 JsonNode constraintsJson = json.get(CONSTRAINTS);
94 if (constraintsJson != null) {
95 JsonCodec<Constraint> constraintsCodec = context.codec(Constraint.class);
96 ArrayList<Constraint> constraints = new ArrayList<>(constraintsJson.size());
97 IntStream.range(0, constraintsJson.size())
98 .forEach(i -> constraints.add(
99 constraintsCodec.decode(get(constraintsJson, i),
100 context)));
101 builder.constraints(constraints);
102 }
103
104 ObjectNode selectorJson = get(json, SELECTOR);
105 if (selectorJson != null) {
106 JsonCodec<TrafficSelector> selectorCodec = context.codec(TrafficSelector.class);
107 TrafficSelector selector = selectorCodec.decode(selectorJson, context);
108 builder.selector(selector);
109 }
110
111 ObjectNode treatmentJson = get(json, TREATMENT);
112 if (treatmentJson != null) {
113 JsonCodec<TrafficTreatment> treatmentCodec = context.codec(TrafficTreatment.class);
114 TrafficTreatment treatment = treatmentCodec.decode(treatmentJson, context);
115 builder.treatment(treatment);
116 }
117 }
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800118}