blob: 985d6f08e96d83e174e38460bf4233392dedd67b [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
Gamze Abaka1e5ccf52018-07-02 11:59:03 +000018import com.fasterxml.jackson.core.JsonParser;
19import com.fasterxml.jackson.core.ObjectCodec;
20import com.fasterxml.jackson.databind.DeserializationContext;
21import com.fasterxml.jackson.databind.JsonDeserializer;
22import com.fasterxml.jackson.databind.JsonNode;
23import com.fasterxml.jackson.databind.ObjectMapper;
24import com.fasterxml.jackson.databind.module.SimpleModule;
Andrea Campanellacd431d92022-02-08 10:10:17 +010025import org.onlab.packet.Ip4Address;
26import org.onlab.packet.VlanId;
27import org.opencord.sadis.BaseConfig;
28import org.opencord.sadis.SubscriberAndDeviceInformation;
29import org.opencord.sadis.UniTagInformation;
30import org.slf4j.Logger;
31import org.slf4j.LoggerFactory;
32
33import java.io.IOException;
34import java.util.ArrayList;
35import java.util.List;
Gamze Abaka1e5ccf52018-07-02 11:59:03 +000036
37/**
38 * Configuration options for the Subscriber And Device Information Service.
39 *
40 * <pre>
41 * "sadis" : {
42 * "integration" : {
43 * "url" : "http://{hostname}{:port}",
44 * "cache" : {
45 * "maxsize" : number of entries,
46 * "entryttl" : duration, i.e. 10s or 1m
47 * }
48 * },
49 * "entries" : [
50 * {
51 * "id" : "uniqueid",
Gamze Abaka1e5ccf52018-07-02 11:59:03 +000052 * "nasportid" : string,
53 * "port" : int,
54 * "slot" : int,
55 * "hardwareidentifier" : string,
56 * "ipAddress" : string,
57 * "nasId" : string,
58 * "circuitId" : string,
Matteo Scandolo198aef92020-01-08 00:43:17 +000059 * "remoteId" : string,
60 * "uniTagList": [
61 * {
yasin sapli2c93ddb2021-06-11 17:46:26 +030062 * "uniTagMatch" : int,
63 * "ponCTag" : string,
64 * "ponSTag" : string,
65 * "usPonCTagPriority" : int,
66 * "dsPonCTagPriority" : int,
67 * "usPonSTagPriority" : int,
68 * "dsPonSTagPriority" : int,
69 * "technologyProfileId" : int,
70 * "upstreamBandwidthProfile" : string,
71 * "downstreamBandwidthProfile" : string,
72 * "upstreamOltBandwidthProfile" : string,
73 * "downstreamOltBandwidthProfile" : string,
74 * "enableMacLearning" : string,
Andrea Campanellacd431d92022-02-08 10:10:17 +010075 * "configuredMacAddress" : string,
yasin sapli2c93ddb2021-06-11 17:46:26 +030076 * "isDhcpRequired" : string,
77 * "isIgmpRequired" : string,
yasin sapli9e3ebed2021-07-09 14:06:35 +000078 * "isPppoeRequired" : string,
yasin sapli2c93ddb2021-06-11 17:46:26 +030079 * "serviceName" : string
Matteo Scandolo198aef92020-01-08 00:43:17 +000080 * }
yasin sapli2c93ddb2021-06-11 17:46:26 +030081 * ]
Gamze Abaka1e5ccf52018-07-02 11:59:03 +000082 * }, ...
83 * ]
84 * }
85 * </pre>
86 */
87public class SubscriberAndDeviceInformationConfig extends BaseConfig<SubscriberAndDeviceInformation> {
88
89 private final Logger log = LoggerFactory.getLogger(this.getClass());
Matteo Scandolo198aef92020-01-08 00:43:17 +000090 private static final int NO_PCP = -1;
Andrea Campanella30041b92022-01-28 16:09:08 +010091 private static final int NO_TP = -1;
Matteo Scandolo198aef92020-01-08 00:43:17 +000092 private static final String NO_SN = "";
Andrea Campanellacd431d92022-02-08 10:10:17 +010093 private static final String EMPTY_MAC = "";
Matteo Scandolo198aef92020-01-08 00:43:17 +000094 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";
yasin sapli9e3ebed2021-07-09 14:06:35 +0000110 private static final String IS_PPPOE_REQ = "isPppoeRequired";
Matteo Scandolo198aef92020-01-08 00:43:17 +0000111 private static final String MAC_ADDRESS = "configuredMacAddress";
Gamze Abaka1e5ccf52018-07-02 11:59:03 +0000112
113 public List<SubscriberAndDeviceInformation> getEntries() {
114 List<SubscriberAndDeviceInformation> result = new ArrayList<>();
115 ObjectMapper mapper = new ObjectMapper();
116 SimpleModule module = new SimpleModule();
117 module.addDeserializer(VlanId.class, new VlanIdDeserializer());
118 module.addDeserializer(Ip4Address.class, new Ip4AddressDeserializer());
Matteo Scandolo198aef92020-01-08 00:43:17 +0000119 module.addDeserializer(UniTagInformation.class, new UniTagDeserializer());
Gamze Abaka1e5ccf52018-07-02 11:59:03 +0000120 mapper.registerModule(module);
121 final JsonNode entries = this.object.path(ENTRIES);
122 entries.forEach(entry -> {
123 try {
124 result.add(mapper.readValue(entry.toString(), SubscriberAndDeviceInformation.class));
125 } catch (IOException e) {
126 log.warn("Unable to parse configuration entry, '{}', error: {}", entry, e.getMessage());
127 }
128 });
129
130 return result;
131 }
132
133 public class VlanIdDeserializer extends JsonDeserializer<VlanId> {
134 @Override
135 public VlanId deserialize(JsonParser jp, DeserializationContext ctxt)
136 throws IOException {
137 ObjectCodec oc = jp.getCodec();
138 JsonNode node = oc.readTree(jp);
139 return VlanId.vlanId((short) node.asInt());
140 }
141 }
142
143 public class Ip4AddressDeserializer extends JsonDeserializer<Ip4Address> {
144 @Override
145 public Ip4Address deserialize(JsonParser jp, DeserializationContext ctxt)
146 throws IOException {
147 ObjectCodec oc = jp.getCodec();
148 JsonNode node = oc.readTree(jp);
149 return Ip4Address.valueOf(node.asText());
150 }
151 }
Matteo Scandolo198aef92020-01-08 00:43:17 +0000152
153 public class UniTagDeserializer extends JsonDeserializer<UniTagInformation> {
154 @Override
155 public UniTagInformation deserialize(JsonParser jp, DeserializationContext ctxt)
156 throws IOException {
157 ObjectCodec oc = jp.getCodec();
158 JsonNode node = oc.readTree(jp);
159 return getUniTagInformation(node);
160 }
161 }
162
163 public UniTagInformation getUniTagInformation(JsonNode node) {
yasin sapli2c93ddb2021-06-11 17:46:26 +0300164 String usBw = node.get(US_BW) == null ? null : node.get(US_BW).asText();
165 String dsBw = node.get(DS_BW) == null ? null : node.get(DS_BW).asText();
Matteo Scandolo198aef92020-01-08 00:43:17 +0000166 return new UniTagInformation.Builder()
167 .setUniTagMatch(VlanId.vlanId(node.get(UNI_TAG_MATCH) == null ? VlanId.NO_VID
168 : (short) node.get(UNI_TAG_MATCH).asInt()))
Andrea Campanella30041b92022-01-28 16:09:08 +0100169 .setPonCTag(node.get(PON_C_TAG) == null ? VlanId.vlanId(VlanId.NO_VID) :
170 VlanId.vlanId(node.get(PON_C_TAG).shortValue()))
171 .setPonSTag(node.get(PON_S_TAG) == null ? VlanId.vlanId(VlanId.NO_VID) :
172 VlanId.vlanId(node.get(PON_S_TAG).shortValue()))
Matteo Scandolo198aef92020-01-08 00:43:17 +0000173 .setUsPonCTagPriority(node.get(US_C_TAG_PCP) == null ? NO_PCP :
174 node.get(US_C_TAG_PCP).asInt())
175 .setUsPonSTagPriority(node.get(US_S_TAG_PCP) == null ? NO_PCP :
176 node.get(US_S_TAG_PCP).asInt())
177 .setDsPonCTagPriority(node.get(DS_C_TAG_PCP) == null ? NO_PCP :
178 node.get(DS_C_TAG_PCP).asInt())
179 .setDsPonSTagPriority(node.get(DS_S_TAG_PCP) == null ? NO_PCP :
180 node.get(DS_S_TAG_PCP).asInt())
Andrea Campanella30041b92022-01-28 16:09:08 +0100181 .setEnableMacLearning(node.get(MAC_LEARNING) != null && node.get(MAC_LEARNING).asBoolean())
182 .setTechnologyProfileId(node.get(TP_ID) == null ? NO_TP : node.get(TP_ID).asInt())
yasin sapli2c93ddb2021-06-11 17:46:26 +0300183 .setUpstreamBandwidthProfile(usBw)
184 .setDownstreamBandwidthProfile(dsBw)
185 .setUpstreamOltBandwidthProfile(node.get(US_OLT_BW) == null ? usBw
186 : node.get(US_OLT_BW).asText())
187 .setDownstreamOltBandwidthProfile(node.get(DS_OLT_BW) == null ? dsBw
188 : node.get(DS_OLT_BW).asText())
Matteo Scandolo198aef92020-01-08 00:43:17 +0000189 .setServiceName(node.get(SERVICE_NAME) == null ? NO_SN :
190 node.get(SERVICE_NAME).asText())
Andrea Campanella30041b92022-01-28 16:09:08 +0100191 .setIsDhcpRequired(node.get(IS_DHCP_REQ) != null && node.get(IS_DHCP_REQ).asBoolean())
192 .setIsIgmpRequired(node.get(IS_IGMP_REQ) != null && node.get(IS_IGMP_REQ).asBoolean())
yasin sapli9e3ebed2021-07-09 14:06:35 +0000193 .setIsPppoeRequired(node.get(IS_PPPOE_REQ) != null && node.get(IS_PPPOE_REQ).asBoolean())
Andrea Campanellacd431d92022-02-08 10:10:17 +0100194 .setConfiguredMacAddress(node.get(MAC_ADDRESS) == null ? EMPTY_MAC :
Matteo Scandolo198aef92020-01-08 00:43:17 +0000195 node.get(MAC_ADDRESS).asText())
196 .build();
197 }
Gamze Abaka1e5ccf52018-07-02 11:59:03 +0000198}