blob: 199df622cb24d47b3a189e9e4a49e9cdf9c67298 [file] [log] [blame]
Jian Li2d3a1d12017-04-05 00:56:54 +09001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Jian Li2d3a1d12017-04-05 00:56:54 +09003 *
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 */
Jian Li2e818b02017-04-12 19:28:30 +090016package org.onosproject.mapping.codec;
Jian Li2d3a1d12017-04-05 00:56:54 +090017
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.node.ArrayNode;
20import com.fasterxml.jackson.databind.node.ObjectNode;
21import org.onosproject.codec.CodecContext;
22import org.onosproject.codec.JsonCodec;
23import org.onosproject.mapping.DefaultMappingValue;
24import org.onosproject.mapping.MappingTreatment;
25import org.onosproject.mapping.MappingValue;
26import org.onosproject.mapping.actions.MappingAction;
27
28import java.util.stream.IntStream;
29
30import static com.google.common.base.Preconditions.checkNotNull;
31
32/**
33 * Mapping value codec.
34 */
35public final class MappingValueCodec extends JsonCodec<MappingValue> {
36
Ray Milkey9c9cde42018-01-12 14:22:06 -080037 static final String ACTION = "action";
38 static final String TREATMENTS = "treatments";
Jian Li2d3a1d12017-04-05 00:56:54 +090039
40 @Override
41 public ObjectNode encode(MappingValue value, CodecContext context) {
42 checkNotNull(value, "Mapping value cannot be null");
43
44 final ObjectNode result = context.mapper().createObjectNode();
45 final ArrayNode jsonTreatments = result.putArray(TREATMENTS);
46
47 final JsonCodec<MappingTreatment> treatmentCodec =
48 context.codec(MappingTreatment.class);
49
50 final JsonCodec<MappingAction> actionCodec =
51 context.codec(MappingAction.class);
52
53 for (final MappingTreatment treatment : value.treatments()) {
54 jsonTreatments.add(treatmentCodec.encode(treatment, context));
55 }
56
57 result.set(ACTION, actionCodec.encode(value.action(), context));
58
59 return result;
60 }
61
62 @Override
63 public MappingValue decode(ObjectNode json, CodecContext context) {
64 if (json == null || !json.isObject()) {
65 return null;
66 }
67
68 final JsonCodec<MappingTreatment> treatmentCodec =
69 context.codec(MappingTreatment.class);
70
71 JsonNode treatmentJson = json.get(TREATMENTS);
72 MappingValue.Builder builder = DefaultMappingValue.builder();
73
74 if (treatmentJson != null) {
75 IntStream.range(0, treatmentJson.size())
76 .forEach(i -> builder.add(
77 treatmentCodec.decode(get(treatmentJson, i),
78 context)));
79 }
80
81 ObjectNode actionJson = get(json, ACTION);
82 if (actionJson != null) {
83 final JsonCodec<MappingAction> actionCodec =
84 context.codec(MappingAction.class);
85 builder.withAction(actionCodec.decode(actionJson, context));
86 }
87
88 return builder.build();
89 }
90}