blob: dbd824c1929ca4403dcd0862b3704b32c666cd99 [file] [log] [blame]
Ray Milkeyc95bb9d2015-01-06 10:28:24 -08001/*
2 * Copyright 2015 Open Networking Laboratory
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 */
16package org.onosproject.codec.impl;
17
18import org.onosproject.codec.CodecContext;
19import org.onosproject.codec.JsonCodec;
20import org.onosproject.net.flow.TrafficSelector;
21import org.onosproject.net.flow.TrafficTreatment;
22import org.onosproject.net.intent.ConnectivityIntent;
Ray Milkeydb358082015-01-13 16:34:38 -080023import org.onosproject.net.intent.Constraint;
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080024import org.onosproject.net.intent.Intent;
25
Ray Milkeydb358082015-01-13 16:34:38 -080026import com.fasterxml.jackson.databind.node.ArrayNode;
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080027import com.fasterxml.jackson.databind.node.ObjectNode;
28
29import static com.google.common.base.Preconditions.checkNotNull;
30
31/**
32 * Connectivity intent codec.
33 */
Ray Milkey540b2ce2015-02-04 17:50:20 -080034public final class ConnectivityIntentCodec extends JsonCodec<ConnectivityIntent> {
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080035
36 @Override
37 public ObjectNode encode(ConnectivityIntent intent, CodecContext context) {
38 checkNotNull(intent, "Connectivity intent cannot be null");
39
40 final JsonCodec<Intent> intentCodec = context.codec(Intent.class);
41 final ObjectNode result = intentCodec.encode(intent, context);
42
43 if (intent.selector() != null) {
44 final JsonCodec<TrafficSelector> selectorCodec =
45 context.codec(TrafficSelector.class);
46 result.set("selector", selectorCodec.encode(intent.selector(), context));
47 }
48
49 if (intent.treatment() != null) {
50 final JsonCodec<TrafficTreatment> treatmentCodec =
51 context.codec(TrafficTreatment.class);
52 result.set("treatment", treatmentCodec.encode(intent.treatment(), context));
53 }
54
Ray Milkeyc24cde32015-03-10 18:20:18 -070055 result.put("priority", intent.priority());
56
Ray Milkeydb358082015-01-13 16:34:38 -080057 if (intent.constraints() != null) {
58 final ArrayNode jsonConstraints = result.putArray("constraints");
59
60 if (intent.constraints() != null) {
Ray Milkeyb9a8a182015-01-20 14:15:35 -080061 final JsonCodec<Constraint> constraintCodec =
62 context.codec(Constraint.class);
Ray Milkeydb358082015-01-13 16:34:38 -080063 for (final Constraint constraint : intent.constraints()) {
Ray Milkeyb9a8a182015-01-20 14:15:35 -080064 final ObjectNode constraintNode =
65 constraintCodec.encode(constraint, context);
66 jsonConstraints.add(constraintNode);
Ray Milkeydb358082015-01-13 16:34:38 -080067 }
68 }
69 }
70
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080071 return result;
72 }
73}