aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/onos/core/common/src/main/java/org/onosproject/codec/impl/ConnectivityIntentCodec.java
blob: 9e8cd86cbce60c14650455bfb0ddf95d35014d55 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
 * Copyright 2015 Open Networking Laboratory
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.onosproject.codec.impl;

import java.util.ArrayList;
import java.util.stream.IntStream;

import org.onosproject.codec.CodecContext;
import org.onosproject.codec.JsonCodec;
import org.onosproject.net.flow.TrafficSelector;
import org.onosproject.net.flow.TrafficTreatment;
import org.onosproject.net.intent.ConnectivityIntent;
import org.onosproject.net.intent.Constraint;
import org.onosproject.net.intent.Intent;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * Connectivity intent codec.
 */
public final class ConnectivityIntentCodec extends JsonCodec<ConnectivityIntent> {

    private static final String CONSTRAINTS = "constraints";
    private static final String SELECTOR = "selector";
    private static final String TREATMENT = "treatment";

    @Override
    public ObjectNode encode(ConnectivityIntent intent, CodecContext context) {
        checkNotNull(intent, "Connectivity intent cannot be null");

        final JsonCodec<Intent> intentCodec = context.codec(Intent.class);
        final ObjectNode result = intentCodec.encode(intent, context);

        if (intent.selector() != null) {
            final JsonCodec<TrafficSelector> selectorCodec =
                    context.codec(TrafficSelector.class);
            result.set(SELECTOR, selectorCodec.encode(intent.selector(), context));
        }

        if (intent.treatment() != null) {
            final JsonCodec<TrafficTreatment> treatmentCodec =
                    context.codec(TrafficTreatment.class);
            result.set(TREATMENT, treatmentCodec.encode(intent.treatment(), context));
        }

        result.put(IntentCodec.PRIORITY, intent.priority());

        if (intent.constraints() != null) {
            final ArrayNode jsonConstraints = result.putArray(CONSTRAINTS);

            if (intent.constraints() != null) {
                final JsonCodec<Constraint> constraintCodec =
                        context.codec(Constraint.class);
                for (final Constraint constraint : intent.constraints()) {
                    final ObjectNode constraintNode =
                            constraintCodec.encode(constraint, context);
                    jsonConstraints.add(constraintNode);
                }
            }
        }

        return result;
    }

    /**
     * Extracts connectivity intent specific attributes from a JSON object
     * and adds them to a builder.
     *
     * @param json root JSON object
     * @param context code context
     * @param builder builder to use for storing the attributes. Constraints,
     *                selector and treatment are modified by this call.
     */
    public static void intentAttributes(ObjectNode json, CodecContext context,
                                        ConnectivityIntent.Builder builder) {
        JsonNode constraintsJson = json.get(CONSTRAINTS);
        if (constraintsJson != null) {
            JsonCodec<Constraint> constraintsCodec = context.codec(Constraint.class);
            ArrayList<Constraint> constraints = new ArrayList<>(constraintsJson.size());
            IntStream.range(0, constraintsJson.size())
                    .forEach(i -> constraints.add(
                            constraintsCodec.decode(get(constraintsJson, i),
                                    context)));
            builder.constraints(constraints);
        }

        ObjectNode selectorJson = get(json, SELECTOR);
        if (selectorJson != null) {
            JsonCodec<TrafficSelector> selectorCodec = context.codec(TrafficSelector.class);
            TrafficSelector selector = selectorCodec.decode(selectorJson, context);
            builder.selector(selector);
        }

        ObjectNode treatmentJson = get(json, TREATMENT);
        if (treatmentJson != null) {
            JsonCodec<TrafficTreatment> treatmentCodec = context.codec(TrafficTreatment.class);
            TrafficTreatment treatment = treatmentCodec.decode(treatmentJson, context);
            builder.treatment(treatment);
        }
    }
}