blob: 8f8bf3febaee0652040c2dc0681426999846e84d [file] [log] [blame]
DScano55a01032020-04-09 20:01:50 +02001/*
2 * Copyright 2020-present Open Networking Foundation
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.vpls.rest;
17
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.net.EncapsulationType;
24import org.onosproject.net.intf.Interface;
25import org.onosproject.vpls.api.VplsData;
26import org.slf4j.Logger;
27
28import java.util.ArrayList;
29import java.util.Collection;
30import java.util.stream.IntStream;
31
32import static com.google.common.base.Preconditions.checkNotNull;
33import static org.slf4j.LoggerFactory.getLogger;
34/**
35 * Vpls JSON codec.
36 */
37public final class VplsCodec extends JsonCodec<VplsData> {
38
39 // JSON field names
40 private static final String NAME = "name";
41 private static final String ENCAPSULATION_TYPE = "encapsulation";
42 private static final String INTERFACES = "interfaces";
43
44 private final Logger log = getLogger(getClass());
45
46 @Override
47 public ObjectNode encode(VplsData vplsData, CodecContext context) {
48 checkNotNull(vplsData, "Vpls cannot be null");
49 ObjectNode result = context.mapper().createObjectNode()
50 .put(NAME, vplsData.name())
51 .put(ENCAPSULATION_TYPE, vplsData.encapsulationType().toString());
52 ArrayNode interfaces = context.mapper().createArrayNode();
53 vplsData.interfaces().forEach(interf -> {
54 ObjectNode bandJson = context.codec(Interface.class).encode(interf, context);
55 interfaces.add(bandJson);
56 });
57 result.set(INTERFACES, interfaces);
58 return result;
59 }
60 @Override
61 public VplsData decode(ObjectNode json, CodecContext context) {
62 if (json == null || !json.isObject()) {
63 return null;
64 }
65
66 String vplsName = json.findValue(NAME).asText();
67 EncapsulationType encap = EncapsulationType.enumFromString(json.findValue(ENCAPSULATION_TYPE).asText());
68 VplsData vplsData = VplsData.of(vplsName, encap);
69
70 Collection<Interface> interfaceList = new ArrayList<>();
71 JsonNode interfacesJeson = json.findValue(INTERFACES);
72 JsonCodec<Interface> interfaceCodec = context.codec(Interface.class);
73 if (interfacesJeson != null) {
74 IntStream.range(0, interfacesJeson.size())
75 .forEach(i -> interfaceList.add(
76 interfaceCodec.decode(get(interfacesJeson, i),
77 context)));
78 vplsData.addInterfaces(interfaceList);
79 }
80 return vplsData;
81 }
82
83}
84