blob: 4208a5e422959ccb6f83d4c7056e46cd07765bd5 [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;
Daniele Moro82d9a362019-11-06 23:51:20 +000019import com.google.common.collect.Lists;
20import org.onlab.packet.Ip4Address;
21import org.onlab.packet.MacAddress;
Deepa vaddireddy386f38b2017-08-02 06:24:01 +000022import org.onosproject.codec.CodecContext;
23import org.onosproject.codec.JsonCodec;
24import org.opencord.sadis.SubscriberAndDeviceInformation;
Daniele Moro82d9a362019-11-06 23:51:20 +000025import org.opencord.sadis.UniTagInformation;
Deepa vaddireddy386f38b2017-08-02 06:24:01 +000026
Daniele Moro82d9a362019-11-06 23:51:20 +000027import java.util.ArrayList;
28import java.util.List;
David K. Bainbridge8bf98e02017-08-07 10:41:56 -070029
Daniele Moro82d9a362019-11-06 23:51:20 +000030public class SubscriberAndDeviceInformationCodec extends JsonCodec<SubscriberAndDeviceInformation> {
31
32 private static final String ID = "id";
33 private static final String NAS_PORT_ID = "nasPortId";
34 private static final String UPLINK_PORT = "uplinkPort";
35 private static final String SLOT = "slot";
36 private static final String HARDWARE_IDENTIFIER = "hardwareIdentifier";
37 private static final String IP_ADDRESS = "ipAddress";
38 private static final String NAS_ID = "nasId";
39 private static final String CIRCUIT_ID = "circuitId";
40 private static final String REMOTE_ID = "remoteId";
41 private static final String UNI_TAG_LIST = "uniTagList";
42 private static final String EMPTY_STRING = "";
43 private static final int NO_VALUE = -1;
44
45 @Override
46 public ObjectNode encode(SubscriberAndDeviceInformation entry, CodecContext context) {
47
48 List<ObjectNode> uniTagListNodes = Lists.newArrayList();
49 List<UniTagInformation> uniTagList = entry.uniTagList();
50 if (uniTagList != null) {
51 for (UniTagInformation uniTagInformation : uniTagList) {
52 uniTagListNodes.add(context.encode(uniTagInformation, UniTagInformation.class));
53 }
54 }
55 return context.mapper().createObjectNode()
56 .put(ID, entry.id())
57 .put(NAS_PORT_ID, entry.nasPortId())
58 .put(UPLINK_PORT, entry.uplinkPort())
59 .put(SLOT, entry.slot())
60 .put(HARDWARE_IDENTIFIER, (entry.hardwareIdentifier() == null) ? EMPTY_STRING :
61 entry.hardwareIdentifier().toString())
62 .put(IP_ADDRESS, (entry.ipAddress() == null) ? EMPTY_STRING : entry.ipAddress().toString())
63 .put(NAS_ID, entry.nasId())
64 .put(CIRCUIT_ID, (entry.circuitId() == null) ? EMPTY_STRING : entry.circuitId())
65 .put(REMOTE_ID, (entry.remoteId() == null) ? EMPTY_STRING : entry.remoteId())
66 .put(UNI_TAG_LIST, uniTagListNodes.toString());
67 }
68
69 @Override
70 public SubscriberAndDeviceInformation decode(ObjectNode json, CodecContext context) {
71 if (json == null || !json.isObject()) {
72 return null;
73 }
74 if (json.get(ID) == null) {
75 return null;
76 }
77
78 SubscriberAndDeviceInformation info = new SubscriberAndDeviceInformation();
79 info.setId(json.get(ID).asText());
80 info.setNasPortId(json.get(NAS_PORT_ID) == null ? EMPTY_STRING : json.get(NAS_PORT_ID).asText());
81 info.setUplinkPort(json.get(UPLINK_PORT) == null ? NO_VALUE : json.get(UPLINK_PORT).asInt());
82 info.setSlot(json.get(SLOT) == null ? NO_VALUE : json.get(SLOT).asInt());
83 info.setNasId(json.get(NAS_ID) == null ? EMPTY_STRING : json.get(NAS_ID).asText());
84 info.setCircuitId(json.get(CIRCUIT_ID) == null ? EMPTY_STRING : json.get(CIRCUIT_ID).asText());
85 info.setRemoteId(json.get(REMOTE_ID) == null ? EMPTY_STRING : json.get(REMOTE_ID).asText());
86
87 if (json.get(HARDWARE_IDENTIFIER) != null) {
88 info.setHardwareIdentifier(MacAddress.valueOf(json.get(HARDWARE_IDENTIFIER).asText()));
89 }
90
91 if (json.get(IP_ADDRESS) != null) {
92 info.setIPAddress(Ip4Address.valueOf(json.get(IP_ADDRESS).asText()));
93 }
94
95 if (json.get(UNI_TAG_LIST) != null) {
96 List<UniTagInformation> uniTagList = new ArrayList<>();
97 json.get(UNI_TAG_LIST).forEach(entry -> {
98 uniTagList.add(new SubscriberAndDeviceInformationConfig()
99 .getUniTagInformation(entry));
100
101 });
102 info.setUniTagList(uniTagList);
103 }
104 return info;
105 }
Deepa vaddireddy386f38b2017-08-02 06:24:01 +0000106}