blob: f460913f3c57887fe45ab138e3acda17f85298fa [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;
23import org.onosproject.net.intent.Intent;
24
25import com.fasterxml.jackson.databind.node.ObjectNode;
26
27import static com.google.common.base.Preconditions.checkNotNull;
28
29/**
30 * Connectivity intent codec.
31 */
32public class ConnectivityIntentCodec extends JsonCodec<ConnectivityIntent> {
33
34 @Override
35 public ObjectNode encode(ConnectivityIntent intent, CodecContext context) {
36 checkNotNull(intent, "Connectivity intent cannot be null");
37
38 final JsonCodec<Intent> intentCodec = context.codec(Intent.class);
39 final ObjectNode result = intentCodec.encode(intent, context);
40
41 if (intent.selector() != null) {
42 final JsonCodec<TrafficSelector> selectorCodec =
43 context.codec(TrafficSelector.class);
44 result.set("selector", selectorCodec.encode(intent.selector(), context));
45 }
46
47 if (intent.treatment() != null) {
48 final JsonCodec<TrafficTreatment> treatmentCodec =
49 context.codec(TrafficTreatment.class);
50 result.set("treatment", treatmentCodec.encode(intent.treatment(), context));
51 }
52
53 return result;
54 }
55}