blob: 8b8799553393b2b60896219da451964c0d075ea7 [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.codec.impl;
17
18import com.fasterxml.jackson.databind.node.ObjectNode;
19import com.google.common.collect.Lists;
20import org.onlab.packet.MacAddress;
21import org.onlab.packet.VlanId;
22import org.onosproject.codec.CodecContext;
23import org.onosproject.codec.JsonCodec;
24import org.onosproject.net.ConnectPoint;
25import org.onosproject.net.host.InterfaceIpAddress;
26import org.onosproject.net.intf.Interface;
27import org.slf4j.Logger;
28
29
30import java.util.List;
31
32
33import static com.google.common.base.Preconditions.checkNotNull;
34import static org.onlab.util.Tools.nullIsIllegal;
35import static org.slf4j.LoggerFactory.getLogger;
36
37/**
38 * Interface JSON codec.
39 */
40public final class InterfaceCodec extends JsonCodec<Interface> {
41 private final Logger log = getLogger(getClass());
42
43 // JSON field names
44 private static final String NAME = "name";
45 private static final String CONNECT_POINT = "connect point";
46 private static final String IPS = "ips";
47 private static final String MAC = "mac";
48 private static final String VLAN = "vlan";
49 private static final String VLAN_UNTAGGED = "vlan Untagged";
50 private static final String VLAN_TAGGED = "vlan Tagged";
51 private static final String VLAN_NATIVE = "vlan Native";
52 private static final String MISSING_NAME_MESSAGE =
53 " name is required in Interface";
54 private static final String MISSING_CONNECT_POINT_MESSAGE =
55 " connect point is required in Interface";
56
57 @Override
58 public ObjectNode encode(Interface interf, CodecContext context) {
59 checkNotNull(interf, "Interfaces cannot be null");
60 ObjectNode result = context.mapper().createObjectNode()
61 .put(NAME, interf.name())
62 .put(CONNECT_POINT, interf.connectPoint().toString())
63 .put(IPS, interf.ipAddressesList().toString())
64 .put(MAC, interf.mac().toString())
65 .put(VLAN, interf.vlan().toString())
66 .put(VLAN_UNTAGGED, interf.vlanUntagged().toString())
67 .put(VLAN_TAGGED, interf.vlanTagged().toString())
68 .put(VLAN_NATIVE, interf.vlanNative().toString());
69 return result;
70 }
71 @Override
72 public Interface decode(ObjectNode json, CodecContext context) {
73 if (json == null || !json.isObject()) {
74 return null;
75 }
76
77 String name = nullIsIllegal(json.findValue(NAME),
78 NAME + MISSING_NAME_MESSAGE).asText();
79 ConnectPoint connectPoint = ConnectPoint.deviceConnectPoint(nullIsIllegal(json.findValue(CONNECT_POINT),
80 CONNECT_POINT + MISSING_CONNECT_POINT_MESSAGE).asText());
81 List<InterfaceIpAddress> ipAddresses = Lists.newArrayList();
82 if (json.findValue(IPS) != null) {
83 json.findValue(IPS).forEach(ip -> {
84 ipAddresses.add(InterfaceIpAddress.valueOf(ip.asText()));
85 });
86 }
87
88 MacAddress macAddr = json.findValue(MAC) == null ?
89 null : MacAddress.valueOf(json.findValue(MAC).asText());
90 VlanId vlanId = json.findValue(VLAN) == null ?
91 VlanId.NONE : VlanId.vlanId(Short.parseShort(json.findValue(VLAN).asText()));
92 Interface inter = new Interface(
93 name,
94 connectPoint,
95 ipAddresses,
96 macAddr,
97 vlanId);
98
99 return inter;
100 }
101
102}
103