blob: d7944330a2f4de82c23992c1995d13155dc9ec40 [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 * {
65 * "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 * "enableMacLearning" : string,
76 * "configuredDacAddress" : string,
77 * "isDhcpRequired" : string,
78 * "isIgmpRequired" : string,
79 * "serviceName" : string
80 * }
81 * ]
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;
91 private static final String NO_SN = "";
92 private static final String UNI_TAG_MATCH = "uniTagMatch";
93 private static final String PON_C_TAG = "ponCTag";
94 private static final String PON_S_TAG = "ponSTag";
95 private static final String US_C_TAG_PCP = "usPonCTagPriority";
96 private static final String US_S_TAG_PCP = "usPonSTagPriority";
97 private static final String DS_C_TAG_PCP = "dsPonCTagPriority";
98 private static final String DS_S_TAG_PCP = "dsPonSTagPriority";
99 private static final String MAC_LEARNING = "enableMacLearning";
100 private static final String TP_ID = "technologyProfileId";
101 private static final String US_BW = "upstreamBandwidthProfile";
102 private static final String DS_BW = "downstreamBandwidthProfile";
103 private static final String SERVICE_NAME = "serviceName";
104 private static final String IS_DHCP_REQ = "isDhcpRequired";
105 private static final String IS_IGMP_REQ = "isIgmpRequired";
106 private static final String MAC_ADDRESS = "configuredMacAddress";
Gamze Abaka1e5ccf52018-07-02 11:59:03 +0000107
108 public List<SubscriberAndDeviceInformation> getEntries() {
109 List<SubscriberAndDeviceInformation> result = new ArrayList<>();
110 ObjectMapper mapper = new ObjectMapper();
111 SimpleModule module = new SimpleModule();
112 module.addDeserializer(VlanId.class, new VlanIdDeserializer());
113 module.addDeserializer(Ip4Address.class, new Ip4AddressDeserializer());
Matteo Scandolo198aef92020-01-08 00:43:17 +0000114 module.addDeserializer(UniTagInformation.class, new UniTagDeserializer());
Gamze Abaka1e5ccf52018-07-02 11:59:03 +0000115 mapper.registerModule(module);
116 final JsonNode entries = this.object.path(ENTRIES);
117 entries.forEach(entry -> {
118 try {
119 result.add(mapper.readValue(entry.toString(), SubscriberAndDeviceInformation.class));
120 } catch (IOException e) {
121 log.warn("Unable to parse configuration entry, '{}', error: {}", entry, e.getMessage());
122 }
123 });
124
125 return result;
126 }
127
128 public class VlanIdDeserializer extends JsonDeserializer<VlanId> {
129 @Override
130 public VlanId deserialize(JsonParser jp, DeserializationContext ctxt)
131 throws IOException {
132 ObjectCodec oc = jp.getCodec();
133 JsonNode node = oc.readTree(jp);
134 return VlanId.vlanId((short) node.asInt());
135 }
136 }
137
138 public class Ip4AddressDeserializer extends JsonDeserializer<Ip4Address> {
139 @Override
140 public Ip4Address deserialize(JsonParser jp, DeserializationContext ctxt)
141 throws IOException {
142 ObjectCodec oc = jp.getCodec();
143 JsonNode node = oc.readTree(jp);
144 return Ip4Address.valueOf(node.asText());
145 }
146 }
Matteo Scandolo198aef92020-01-08 00:43:17 +0000147
148 public class UniTagDeserializer extends JsonDeserializer<UniTagInformation> {
149 @Override
150 public UniTagInformation deserialize(JsonParser jp, DeserializationContext ctxt)
151 throws IOException {
152 ObjectCodec oc = jp.getCodec();
153 JsonNode node = oc.readTree(jp);
154 return getUniTagInformation(node);
155 }
156 }
157
158 public UniTagInformation getUniTagInformation(JsonNode node) {
159 return new UniTagInformation.Builder()
160 .setUniTagMatch(VlanId.vlanId(node.get(UNI_TAG_MATCH) == null ? VlanId.NO_VID
161 : (short) node.get(UNI_TAG_MATCH).asInt()))
162 .setPonCTag(VlanId.vlanId((short) node.get(PON_C_TAG).asInt()))
163 .setPonSTag(VlanId.vlanId((short) node.get(PON_S_TAG).asInt()))
164 .setUsPonCTagPriority(node.get(US_C_TAG_PCP) == null ? NO_PCP :
165 node.get(US_C_TAG_PCP).asInt())
166 .setUsPonSTagPriority(node.get(US_S_TAG_PCP) == null ? NO_PCP :
167 node.get(US_S_TAG_PCP).asInt())
168 .setDsPonCTagPriority(node.get(DS_C_TAG_PCP) == null ? NO_PCP :
169 node.get(DS_C_TAG_PCP).asInt())
170 .setDsPonSTagPriority(node.get(DS_S_TAG_PCP) == null ? NO_PCP :
171 node.get(DS_S_TAG_PCP).asInt())
172 .setEnableMacLearning(node.get(MAC_LEARNING) == null ? false :
173 node.get(MAC_LEARNING).asBoolean())
174 .setTechnologyProfileId(node.get(TP_ID).asInt())
175 .setUpstreamBandwidthProfile(node.get(US_BW) == null ? null
176 : node.get(US_BW).asText())
177 .setDownstreamBandwidthProfile(node.get(DS_BW) == null ? null
178 : node.get(DS_BW).asText())
179 .setServiceName(node.get(SERVICE_NAME) == null ? NO_SN :
180 node.get(SERVICE_NAME).asText())
181 .setIsDhcpRequired(node.get(IS_DHCP_REQ) == null ? false : node.get(IS_DHCP_REQ).asBoolean())
182 .setIsIgmpRequired(node.get(IS_IGMP_REQ) == null ? false : node.get(IS_IGMP_REQ).asBoolean())
183 .setConfiguredMacAddress(node.get(MAC_ADDRESS) == null ? MacAddress.NONE.toString() :
184 node.get(MAC_ADDRESS).asText())
185 .build();
186 }
Gamze Abaka1e5ccf52018-07-02 11:59:03 +0000187}