blob: cc054feffb51fd921efb0c1a09d2e47f96f9df6e [file] [log] [blame]
Jian Licb42c312017-03-30 16:20:33 +09001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Jian Licb42c312017-03-30 16:20:33 +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 Licb42c312017-03-30 16:20:33 +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.DefaultMappingTreatment;
24import org.onosproject.mapping.MappingTreatment;
25import org.onosproject.mapping.addresses.MappingAddress;
26import org.onosproject.mapping.instructions.MappingInstruction;
27
28import java.util.stream.IntStream;
29
30import static com.google.common.base.Preconditions.checkNotNull;
31
32/**
33 * Mapping treatment codec.
34 */
35public final class MappingTreatmentCodec extends JsonCodec<MappingTreatment> {
36
37 private static final String INSTRUCTIONS = "instructions";
38 private static final String ADDRESS = "address";
39
40 @Override
41 public ObjectNode encode(MappingTreatment treatment, CodecContext context) {
42 checkNotNull(treatment, "Mapping treatment cannot be null");
43
44 final ObjectNode result = context.mapper().createObjectNode();
45 final ArrayNode jsonInstructions = result.putArray(INSTRUCTIONS);
46
47 final JsonCodec<MappingInstruction> instructionCodec =
48 context.codec(MappingInstruction.class);
49
50 final JsonCodec<MappingAddress> addressCodec =
51 context.codec(MappingAddress.class);
52
53 for (final MappingInstruction instruction : treatment.instructions()) {
54 jsonInstructions.add(instructionCodec.encode(instruction, context));
55 }
56
57 result.set(ADDRESS, addressCodec.encode(treatment.address(), context));
58
59 return result;
60 }
61
62 @Override
63 public MappingTreatment decode(ObjectNode json, CodecContext context) {
64 if (json == null || !json.isObject()) {
65 return null;
66 }
67
68 final JsonCodec<MappingInstruction> instructionCodec =
69 context.codec(MappingInstruction.class);
70
71 JsonNode instructionJson = json.get(INSTRUCTIONS);
72 MappingTreatment.Builder builder = DefaultMappingTreatment.builder();
73
74 if (instructionJson != null) {
75 IntStream.range(0, instructionJson.size())
76 .forEach(i -> builder.add(
77 instructionCodec.decode(get(instructionJson, i),
78 context)));
79 }
80
81 ObjectNode addressJson = get(json, ADDRESS);
82 if (addressJson != null) {
83 final JsonCodec<MappingAddress> addressCodec =
84 context.codec(MappingAddress.class);
85 builder.withAddress(addressCodec.decode(addressJson, context));
86 }
87
88 return builder.build();
89 }
90}