blob: 1c4ab8cbff767ad922b4a711d0e9b23a802ce09c [file] [log] [blame]
Deepa vaddireddy386f38b2017-08-02 06:24:01 +00001/*
2 * Copyright 2017-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.opencord.sadis.impl;
17
18import com.fasterxml.jackson.databind.node.ObjectNode;
Matteo Scandolo198aef92020-01-08 00:43:17 +000019import com.google.common.collect.Lists;
20import org.onlab.packet.Ip4Address;
21import org.onlab.packet.MacAddress;
amit.ghosh3b225bb2022-02-17 18:41:00 +010022import org.onlab.packet.VlanId;
Deepa vaddireddy386f38b2017-08-02 06:24:01 +000023import org.onosproject.codec.CodecContext;
24import org.onosproject.codec.JsonCodec;
25import org.opencord.sadis.SubscriberAndDeviceInformation;
Matteo Scandolo198aef92020-01-08 00:43:17 +000026import org.opencord.sadis.UniTagInformation;
Deepa vaddireddy386f38b2017-08-02 06:24:01 +000027
Matteo Scandolo198aef92020-01-08 00:43:17 +000028import java.util.ArrayList;
29import java.util.List;
David K. Bainbridge8bf98e02017-08-07 10:41:56 -070030
Matteo Scandolo198aef92020-01-08 00:43:17 +000031public class SubscriberAndDeviceInformationCodec extends JsonCodec<SubscriberAndDeviceInformation> {
32
33 private static final String ID = "id";
34 private static final String NAS_PORT_ID = "nasPortId";
35 private static final String UPLINK_PORT = "uplinkPort";
36 private static final String SLOT = "slot";
37 private static final String HARDWARE_IDENTIFIER = "hardwareIdentifier";
38 private static final String IP_ADDRESS = "ipAddress";
39 private static final String NAS_ID = "nasId";
40 private static final String CIRCUIT_ID = "circuitId";
41 private static final String REMOTE_ID = "remoteId";
42 private static final String UNI_TAG_LIST = "uniTagList";
amit.ghosh3b225bb2022-02-17 18:41:00 +010043 private static final String NNI_DHCP_TRAP_VID = "nniDhcpTrapVid";
Matteo Scandolo198aef92020-01-08 00:43:17 +000044 private static final String EMPTY_STRING = "";
45 private static final int NO_VALUE = -1;
46
47 @Override
48 public ObjectNode encode(SubscriberAndDeviceInformation entry, CodecContext context) {
49
50 List<ObjectNode> uniTagListNodes = Lists.newArrayList();
51 List<UniTagInformation> uniTagList = entry.uniTagList();
52 if (uniTagList != null) {
53 for (UniTagInformation uniTagInformation : uniTagList) {
54 uniTagListNodes.add(context.encode(uniTagInformation, UniTagInformation.class));
55 }
56 }
57 return context.mapper().createObjectNode()
58 .put(ID, entry.id())
59 .put(NAS_PORT_ID, entry.nasPortId())
60 .put(UPLINK_PORT, entry.uplinkPort())
61 .put(SLOT, entry.slot())
62 .put(HARDWARE_IDENTIFIER, (entry.hardwareIdentifier() == null) ? EMPTY_STRING :
63 entry.hardwareIdentifier().toString())
64 .put(IP_ADDRESS, (entry.ipAddress() == null) ? EMPTY_STRING : entry.ipAddress().toString())
65 .put(NAS_ID, entry.nasId())
66 .put(CIRCUIT_ID, (entry.circuitId() == null) ? EMPTY_STRING : entry.circuitId())
67 .put(REMOTE_ID, (entry.remoteId() == null) ? EMPTY_STRING : entry.remoteId())
amit.ghosh3b225bb2022-02-17 18:41:00 +010068 .put(NNI_DHCP_TRAP_VID, entry.nniDhcpTrapVid().toShort())
Matteo Scandolo198aef92020-01-08 00:43:17 +000069 .put(UNI_TAG_LIST, uniTagListNodes.toString());
70 }
71
72 @Override
73 public SubscriberAndDeviceInformation decode(ObjectNode json, CodecContext context) {
74 if (json == null || !json.isObject()) {
75 return null;
76 }
77 if (json.get(ID) == null) {
78 return null;
79 }
80
81 SubscriberAndDeviceInformation info = new SubscriberAndDeviceInformation();
82 info.setId(json.get(ID).asText());
83 info.setNasPortId(json.get(NAS_PORT_ID) == null ? EMPTY_STRING : json.get(NAS_PORT_ID).asText());
84 info.setUplinkPort(json.get(UPLINK_PORT) == null ? NO_VALUE : json.get(UPLINK_PORT).asInt());
85 info.setSlot(json.get(SLOT) == null ? NO_VALUE : json.get(SLOT).asInt());
86 info.setNasId(json.get(NAS_ID) == null ? EMPTY_STRING : json.get(NAS_ID).asText());
87 info.setCircuitId(json.get(CIRCUIT_ID) == null ? EMPTY_STRING : json.get(CIRCUIT_ID).asText());
88 info.setRemoteId(json.get(REMOTE_ID) == null ? EMPTY_STRING : json.get(REMOTE_ID).asText());
amit.ghosh3b225bb2022-02-17 18:41:00 +010089 info.setNniDhcpTrapVid(json.get(NNI_DHCP_TRAP_VID) == null ? VlanId.vlanId(VlanId.NO_VID) :
90 VlanId.vlanId(json.get(NNI_DHCP_TRAP_VID).shortValue()));
Matteo Scandolo198aef92020-01-08 00:43:17 +000091
92 if (json.get(HARDWARE_IDENTIFIER) != null) {
93 info.setHardwareIdentifier(MacAddress.valueOf(json.get(HARDWARE_IDENTIFIER).asText()));
94 }
95
96 if (json.get(IP_ADDRESS) != null) {
97 info.setIPAddress(Ip4Address.valueOf(json.get(IP_ADDRESS).asText()));
98 }
99
100 if (json.get(UNI_TAG_LIST) != null) {
101 List<UniTagInformation> uniTagList = new ArrayList<>();
102 json.get(UNI_TAG_LIST).forEach(entry -> {
103 uniTagList.add(new SubscriberAndDeviceInformationConfig()
104 .getUniTagInformation(entry));
105
106 });
107 info.setUniTagList(uniTagList);
108 }
109 return info;
110 }
Deepa vaddireddy386f38b2017-08-02 06:24:01 +0000111}