blob: 43170a82eeed103085668aa37582c7224b0f92b0 [file] [log] [blame]
Gamze Abaka1e5ccf52018-07-02 11:59:03 +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 java.io.IOException;
19import java.util.ArrayList;
20import java.util.List;
21
22import org.onlab.packet.Ip4Address;
Matteo Scandolo198aef92020-01-08 00:43:17 +000023import org.onlab.packet.MacAddress;
Gamze Abaka1e5ccf52018-07-02 11:59:03 +000024import org.onlab.packet.VlanId;
25import org.opencord.sadis.BaseConfig;
26import org.opencord.sadis.SubscriberAndDeviceInformation;
27
Matteo Scandolo198aef92020-01-08 00:43:17 +000028import org.opencord.sadis.UniTagInformation;
Gamze Abaka1e5ccf52018-07-02 11:59:03 +000029import org.slf4j.Logger;
30import org.slf4j.LoggerFactory;
31
32import com.fasterxml.jackson.core.JsonParser;
33import com.fasterxml.jackson.core.ObjectCodec;
34import com.fasterxml.jackson.databind.DeserializationContext;
35import com.fasterxml.jackson.databind.JsonDeserializer;
36import com.fasterxml.jackson.databind.JsonNode;
37import com.fasterxml.jackson.databind.ObjectMapper;
38import com.fasterxml.jackson.databind.module.SimpleModule;
39
40/**
41 * Configuration options for the Subscriber And Device Information Service.
42 *
43 * <pre>
44 * "sadis" : {
45 * "integration" : {
46 * "url" : "http://{hostname}{:port}",
47 * "cache" : {
48 * "maxsize" : number of entries,
49 * "entryttl" : duration, i.e. 10s or 1m
50 * }
51 * },
52 * "entries" : [
53 * {
54 * "id" : "uniqueid",
Gamze Abaka1e5ccf52018-07-02 11:59:03 +000055 * "nasportid" : string,
56 * "port" : int,
57 * "slot" : int,
58 * "hardwareidentifier" : string,
59 * "ipAddress" : string,
60 * "nasId" : string,
61 * "circuitId" : string,
Matteo Scandolo198aef92020-01-08 00:43:17 +000062 * "remoteId" : string,
63 * "uniTagList": [
64 * {
yasin sapli2c93ddb2021-06-11 17:46:26 +030065 * "uniTagMatch" : int,
66 * "ponCTag" : string,
67 * "ponSTag" : string,
68 * "usPonCTagPriority" : int,
69 * "dsPonCTagPriority" : int,
70 * "usPonSTagPriority" : int,
71 * "dsPonSTagPriority" : int,
72 * "technologyProfileId" : int,
73 * "upstreamBandwidthProfile" : string,
74 * "downstreamBandwidthProfile" : string,
75 * "upstreamOltBandwidthProfile" : string,
76 * "downstreamOltBandwidthProfile" : string,
77 * "enableMacLearning" : string,
78 * "configuredDacAddress" : string,
79 * "isDhcpRequired" : string,
80 * "isIgmpRequired" : string,
81 * "serviceName" : string
Matteo Scandolo198aef92020-01-08 00:43:17 +000082 * }
yasin sapli2c93ddb2021-06-11 17:46:26 +030083 * ]
Gamze Abaka1e5ccf52018-07-02 11:59:03 +000084 * }, ...
85 * ]
86 * }
87 * </pre>
88 */
89public class SubscriberAndDeviceInformationConfig extends BaseConfig<SubscriberAndDeviceInformation> {
90
91 private final Logger log = LoggerFactory.getLogger(this.getClass());
Matteo Scandolo198aef92020-01-08 00:43:17 +000092 private static final int NO_PCP = -1;
93 private static final String NO_SN = "";
94 private static final String UNI_TAG_MATCH = "uniTagMatch";
95 private static final String PON_C_TAG = "ponCTag";
96 private static final String PON_S_TAG = "ponSTag";
97 private static final String US_C_TAG_PCP = "usPonCTagPriority";
98 private static final String US_S_TAG_PCP = "usPonSTagPriority";
99 private static final String DS_C_TAG_PCP = "dsPonCTagPriority";
100 private static final String DS_S_TAG_PCP = "dsPonSTagPriority";
101 private static final String MAC_LEARNING = "enableMacLearning";
102 private static final String TP_ID = "technologyProfileId";
103 private static final String US_BW = "upstreamBandwidthProfile";
104 private static final String DS_BW = "downstreamBandwidthProfile";
yasin sapli2c93ddb2021-06-11 17:46:26 +0300105 private static final String US_OLT_BW = "upstreamOltBandwidthProfile";
106 private static final String DS_OLT_BW = "downstreamOltBandwidthProfile";
Matteo Scandolo198aef92020-01-08 00:43:17 +0000107 private static final String SERVICE_NAME = "serviceName";
108 private static final String IS_DHCP_REQ = "isDhcpRequired";
109 private static final String IS_IGMP_REQ = "isIgmpRequired";
110 private static final String MAC_ADDRESS = "configuredMacAddress";
Gamze Abaka1e5ccf52018-07-02 11:59:03 +0000111
112 public List<SubscriberAndDeviceInformation> getEntries() {
113 List<SubscriberAndDeviceInformation> result = new ArrayList<>();
114 ObjectMapper mapper = new ObjectMapper();
115 SimpleModule module = new SimpleModule();
116 module.addDeserializer(VlanId.class, new VlanIdDeserializer());
117 module.addDeserializer(Ip4Address.class, new Ip4AddressDeserializer());
Matteo Scandolo198aef92020-01-08 00:43:17 +0000118 module.addDeserializer(UniTagInformation.class, new UniTagDeserializer());
Gamze Abaka1e5ccf52018-07-02 11:59:03 +0000119 mapper.registerModule(module);
120 final JsonNode entries = this.object.path(ENTRIES);
121 entries.forEach(entry -> {
122 try {
123 result.add(mapper.readValue(entry.toString(), SubscriberAndDeviceInformation.class));
124 } catch (IOException e) {
125 log.warn("Unable to parse configuration entry, '{}', error: {}", entry, e.getMessage());
126 }
127 });
128
129 return result;
130 }
131
132 public class VlanIdDeserializer extends JsonDeserializer<VlanId> {
133 @Override
134 public VlanId deserialize(JsonParser jp, DeserializationContext ctxt)
135 throws IOException {
136 ObjectCodec oc = jp.getCodec();
137 JsonNode node = oc.readTree(jp);
138 return VlanId.vlanId((short) node.asInt());
139 }
140 }
141
142 public class Ip4AddressDeserializer extends JsonDeserializer<Ip4Address> {
143 @Override
144 public Ip4Address deserialize(JsonParser jp, DeserializationContext ctxt)
145 throws IOException {
146 ObjectCodec oc = jp.getCodec();
147 JsonNode node = oc.readTree(jp);
148 return Ip4Address.valueOf(node.asText());
149 }
150 }
Matteo Scandolo198aef92020-01-08 00:43:17 +0000151
152 public class UniTagDeserializer extends JsonDeserializer<UniTagInformation> {
153 @Override
154 public UniTagInformation deserialize(JsonParser jp, DeserializationContext ctxt)
155 throws IOException {
156 ObjectCodec oc = jp.getCodec();
157 JsonNode node = oc.readTree(jp);
158 return getUniTagInformation(node);
159 }
160 }
161
162 public UniTagInformation getUniTagInformation(JsonNode node) {
yasin sapli2c93ddb2021-06-11 17:46:26 +0300163 String usBw = node.get(US_BW) == null ? null : node.get(US_BW).asText();
164 String dsBw = node.get(DS_BW) == null ? null : node.get(DS_BW).asText();
Matteo Scandolo198aef92020-01-08 00:43:17 +0000165 return new UniTagInformation.Builder()
166 .setUniTagMatch(VlanId.vlanId(node.get(UNI_TAG_MATCH) == null ? VlanId.NO_VID
167 : (short) node.get(UNI_TAG_MATCH).asInt()))
168 .setPonCTag(VlanId.vlanId((short) node.get(PON_C_TAG).asInt()))
169 .setPonSTag(VlanId.vlanId((short) node.get(PON_S_TAG).asInt()))
170 .setUsPonCTagPriority(node.get(US_C_TAG_PCP) == null ? NO_PCP :
171 node.get(US_C_TAG_PCP).asInt())
172 .setUsPonSTagPriority(node.get(US_S_TAG_PCP) == null ? NO_PCP :
173 node.get(US_S_TAG_PCP).asInt())
174 .setDsPonCTagPriority(node.get(DS_C_TAG_PCP) == null ? NO_PCP :
175 node.get(DS_C_TAG_PCP).asInt())
176 .setDsPonSTagPriority(node.get(DS_S_TAG_PCP) == null ? NO_PCP :
177 node.get(DS_S_TAG_PCP).asInt())
178 .setEnableMacLearning(node.get(MAC_LEARNING) == null ? false :
179 node.get(MAC_LEARNING).asBoolean())
180 .setTechnologyProfileId(node.get(TP_ID).asInt())
yasin sapli2c93ddb2021-06-11 17:46:26 +0300181 .setUpstreamBandwidthProfile(usBw)
182 .setDownstreamBandwidthProfile(dsBw)
183 .setUpstreamOltBandwidthProfile(node.get(US_OLT_BW) == null ? usBw
184 : node.get(US_OLT_BW).asText())
185 .setDownstreamOltBandwidthProfile(node.get(DS_OLT_BW) == null ? dsBw
186 : node.get(DS_OLT_BW).asText())
Matteo Scandolo198aef92020-01-08 00:43:17 +0000187 .setServiceName(node.get(SERVICE_NAME) == null ? NO_SN :
188 node.get(SERVICE_NAME).asText())
189 .setIsDhcpRequired(node.get(IS_DHCP_REQ) == null ? false : node.get(IS_DHCP_REQ).asBoolean())
190 .setIsIgmpRequired(node.get(IS_IGMP_REQ) == null ? false : node.get(IS_IGMP_REQ).asBoolean())
191 .setConfiguredMacAddress(node.get(MAC_ADDRESS) == null ? MacAddress.NONE.toString() :
192 node.get(MAC_ADDRESS).asText())
193 .build();
194 }
Gamze Abaka1e5ccf52018-07-02 11:59:03 +0000195}