Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.opencord.sadis.impl; |
| 17 | |
| 18 | import java.io.IOException; |
| 19 | import java.util.ArrayList; |
| 20 | import java.util.List; |
| 21 | |
| 22 | import org.onlab.packet.Ip4Address; |
Matteo Scandolo | 198aef9 | 2020-01-08 00:43:17 +0000 | [diff] [blame] | 23 | import org.onlab.packet.MacAddress; |
Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame] | 24 | import org.onlab.packet.VlanId; |
| 25 | import org.opencord.sadis.BaseConfig; |
| 26 | import org.opencord.sadis.SubscriberAndDeviceInformation; |
| 27 | |
Matteo Scandolo | 198aef9 | 2020-01-08 00:43:17 +0000 | [diff] [blame] | 28 | import org.opencord.sadis.UniTagInformation; |
Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame] | 29 | import org.slf4j.Logger; |
| 30 | import org.slf4j.LoggerFactory; |
| 31 | |
| 32 | import com.fasterxml.jackson.core.JsonParser; |
| 33 | import com.fasterxml.jackson.core.ObjectCodec; |
| 34 | import com.fasterxml.jackson.databind.DeserializationContext; |
| 35 | import com.fasterxml.jackson.databind.JsonDeserializer; |
| 36 | import com.fasterxml.jackson.databind.JsonNode; |
| 37 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 38 | import 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 Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame] | 55 | * "nasportid" : string, |
| 56 | * "port" : int, |
| 57 | * "slot" : int, |
| 58 | * "hardwareidentifier" : string, |
| 59 | * "ipAddress" : string, |
| 60 | * "nasId" : string, |
| 61 | * "circuitId" : string, |
Matteo Scandolo | 198aef9 | 2020-01-08 00:43:17 +0000 | [diff] [blame] | 62 | * "remoteId" : string, |
| 63 | * "uniTagList": [ |
| 64 | * { |
yasin sapli | 2c93ddb | 2021-06-11 17:46:26 +0300 | [diff] [blame] | 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 | * "upstreamOltBandwidthProfile" : string, |
| 76 | * "downstreamOltBandwidthProfile" : string, |
| 77 | * "enableMacLearning" : string, |
| 78 | * "configuredDacAddress" : string, |
| 79 | * "isDhcpRequired" : string, |
| 80 | * "isIgmpRequired" : string, |
yasin sapli | 9e3ebed | 2021-07-09 14:06:35 +0000 | [diff] [blame^] | 81 | * "isPppoeRequired" : string, |
yasin sapli | 2c93ddb | 2021-06-11 17:46:26 +0300 | [diff] [blame] | 82 | * "serviceName" : string |
Matteo Scandolo | 198aef9 | 2020-01-08 00:43:17 +0000 | [diff] [blame] | 83 | * } |
yasin sapli | 2c93ddb | 2021-06-11 17:46:26 +0300 | [diff] [blame] | 84 | * ] |
Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame] | 85 | * }, ... |
| 86 | * ] |
| 87 | * } |
| 88 | * </pre> |
| 89 | */ |
| 90 | public class SubscriberAndDeviceInformationConfig extends BaseConfig<SubscriberAndDeviceInformation> { |
| 91 | |
| 92 | private final Logger log = LoggerFactory.getLogger(this.getClass()); |
Matteo Scandolo | 198aef9 | 2020-01-08 00:43:17 +0000 | [diff] [blame] | 93 | private static final int NO_PCP = -1; |
Andrea Campanella | 30041b9 | 2022-01-28 16:09:08 +0100 | [diff] [blame] | 94 | private static final int NO_TP = -1; |
Matteo Scandolo | 198aef9 | 2020-01-08 00:43:17 +0000 | [diff] [blame] | 95 | private static final String NO_SN = ""; |
| 96 | private static final String UNI_TAG_MATCH = "uniTagMatch"; |
| 97 | private static final String PON_C_TAG = "ponCTag"; |
| 98 | private static final String PON_S_TAG = "ponSTag"; |
| 99 | private static final String US_C_TAG_PCP = "usPonCTagPriority"; |
| 100 | private static final String US_S_TAG_PCP = "usPonSTagPriority"; |
| 101 | private static final String DS_C_TAG_PCP = "dsPonCTagPriority"; |
| 102 | private static final String DS_S_TAG_PCP = "dsPonSTagPriority"; |
| 103 | private static final String MAC_LEARNING = "enableMacLearning"; |
| 104 | private static final String TP_ID = "technologyProfileId"; |
| 105 | private static final String US_BW = "upstreamBandwidthProfile"; |
| 106 | private static final String DS_BW = "downstreamBandwidthProfile"; |
yasin sapli | 2c93ddb | 2021-06-11 17:46:26 +0300 | [diff] [blame] | 107 | private static final String US_OLT_BW = "upstreamOltBandwidthProfile"; |
| 108 | private static final String DS_OLT_BW = "downstreamOltBandwidthProfile"; |
Matteo Scandolo | 198aef9 | 2020-01-08 00:43:17 +0000 | [diff] [blame] | 109 | private static final String SERVICE_NAME = "serviceName"; |
| 110 | private static final String IS_DHCP_REQ = "isDhcpRequired"; |
| 111 | private static final String IS_IGMP_REQ = "isIgmpRequired"; |
yasin sapli | 9e3ebed | 2021-07-09 14:06:35 +0000 | [diff] [blame^] | 112 | private static final String IS_PPPOE_REQ = "isPppoeRequired"; |
Matteo Scandolo | 198aef9 | 2020-01-08 00:43:17 +0000 | [diff] [blame] | 113 | private static final String MAC_ADDRESS = "configuredMacAddress"; |
Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame] | 114 | |
| 115 | public List<SubscriberAndDeviceInformation> getEntries() { |
| 116 | List<SubscriberAndDeviceInformation> result = new ArrayList<>(); |
| 117 | ObjectMapper mapper = new ObjectMapper(); |
| 118 | SimpleModule module = new SimpleModule(); |
| 119 | module.addDeserializer(VlanId.class, new VlanIdDeserializer()); |
| 120 | module.addDeserializer(Ip4Address.class, new Ip4AddressDeserializer()); |
Matteo Scandolo | 198aef9 | 2020-01-08 00:43:17 +0000 | [diff] [blame] | 121 | module.addDeserializer(UniTagInformation.class, new UniTagDeserializer()); |
Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame] | 122 | mapper.registerModule(module); |
| 123 | final JsonNode entries = this.object.path(ENTRIES); |
| 124 | entries.forEach(entry -> { |
| 125 | try { |
| 126 | result.add(mapper.readValue(entry.toString(), SubscriberAndDeviceInformation.class)); |
| 127 | } catch (IOException e) { |
| 128 | log.warn("Unable to parse configuration entry, '{}', error: {}", entry, e.getMessage()); |
| 129 | } |
| 130 | }); |
| 131 | |
| 132 | return result; |
| 133 | } |
| 134 | |
| 135 | public class VlanIdDeserializer extends JsonDeserializer<VlanId> { |
| 136 | @Override |
| 137 | public VlanId deserialize(JsonParser jp, DeserializationContext ctxt) |
| 138 | throws IOException { |
| 139 | ObjectCodec oc = jp.getCodec(); |
| 140 | JsonNode node = oc.readTree(jp); |
| 141 | return VlanId.vlanId((short) node.asInt()); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | public class Ip4AddressDeserializer extends JsonDeserializer<Ip4Address> { |
| 146 | @Override |
| 147 | public Ip4Address deserialize(JsonParser jp, DeserializationContext ctxt) |
| 148 | throws IOException { |
| 149 | ObjectCodec oc = jp.getCodec(); |
| 150 | JsonNode node = oc.readTree(jp); |
| 151 | return Ip4Address.valueOf(node.asText()); |
| 152 | } |
| 153 | } |
Matteo Scandolo | 198aef9 | 2020-01-08 00:43:17 +0000 | [diff] [blame] | 154 | |
| 155 | public class UniTagDeserializer extends JsonDeserializer<UniTagInformation> { |
| 156 | @Override |
| 157 | public UniTagInformation deserialize(JsonParser jp, DeserializationContext ctxt) |
| 158 | throws IOException { |
| 159 | ObjectCodec oc = jp.getCodec(); |
| 160 | JsonNode node = oc.readTree(jp); |
| 161 | return getUniTagInformation(node); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | public UniTagInformation getUniTagInformation(JsonNode node) { |
yasin sapli | 2c93ddb | 2021-06-11 17:46:26 +0300 | [diff] [blame] | 166 | String usBw = node.get(US_BW) == null ? null : node.get(US_BW).asText(); |
| 167 | String dsBw = node.get(DS_BW) == null ? null : node.get(DS_BW).asText(); |
Matteo Scandolo | 198aef9 | 2020-01-08 00:43:17 +0000 | [diff] [blame] | 168 | return new UniTagInformation.Builder() |
| 169 | .setUniTagMatch(VlanId.vlanId(node.get(UNI_TAG_MATCH) == null ? VlanId.NO_VID |
| 170 | : (short) node.get(UNI_TAG_MATCH).asInt())) |
Andrea Campanella | 30041b9 | 2022-01-28 16:09:08 +0100 | [diff] [blame] | 171 | .setPonCTag(node.get(PON_C_TAG) == null ? VlanId.vlanId(VlanId.NO_VID) : |
| 172 | VlanId.vlanId(node.get(PON_C_TAG).shortValue())) |
| 173 | .setPonSTag(node.get(PON_S_TAG) == null ? VlanId.vlanId(VlanId.NO_VID) : |
| 174 | VlanId.vlanId(node.get(PON_S_TAG).shortValue())) |
Matteo Scandolo | 198aef9 | 2020-01-08 00:43:17 +0000 | [diff] [blame] | 175 | .setUsPonCTagPriority(node.get(US_C_TAG_PCP) == null ? NO_PCP : |
| 176 | node.get(US_C_TAG_PCP).asInt()) |
| 177 | .setUsPonSTagPriority(node.get(US_S_TAG_PCP) == null ? NO_PCP : |
| 178 | node.get(US_S_TAG_PCP).asInt()) |
| 179 | .setDsPonCTagPriority(node.get(DS_C_TAG_PCP) == null ? NO_PCP : |
| 180 | node.get(DS_C_TAG_PCP).asInt()) |
| 181 | .setDsPonSTagPriority(node.get(DS_S_TAG_PCP) == null ? NO_PCP : |
| 182 | node.get(DS_S_TAG_PCP).asInt()) |
Andrea Campanella | 30041b9 | 2022-01-28 16:09:08 +0100 | [diff] [blame] | 183 | .setEnableMacLearning(node.get(MAC_LEARNING) != null && node.get(MAC_LEARNING).asBoolean()) |
| 184 | .setTechnologyProfileId(node.get(TP_ID) == null ? NO_TP : node.get(TP_ID).asInt()) |
yasin sapli | 2c93ddb | 2021-06-11 17:46:26 +0300 | [diff] [blame] | 185 | .setUpstreamBandwidthProfile(usBw) |
| 186 | .setDownstreamBandwidthProfile(dsBw) |
| 187 | .setUpstreamOltBandwidthProfile(node.get(US_OLT_BW) == null ? usBw |
| 188 | : node.get(US_OLT_BW).asText()) |
| 189 | .setDownstreamOltBandwidthProfile(node.get(DS_OLT_BW) == null ? dsBw |
| 190 | : node.get(DS_OLT_BW).asText()) |
Matteo Scandolo | 198aef9 | 2020-01-08 00:43:17 +0000 | [diff] [blame] | 191 | .setServiceName(node.get(SERVICE_NAME) == null ? NO_SN : |
| 192 | node.get(SERVICE_NAME).asText()) |
Andrea Campanella | 30041b9 | 2022-01-28 16:09:08 +0100 | [diff] [blame] | 193 | .setIsDhcpRequired(node.get(IS_DHCP_REQ) != null && node.get(IS_DHCP_REQ).asBoolean()) |
| 194 | .setIsIgmpRequired(node.get(IS_IGMP_REQ) != null && node.get(IS_IGMP_REQ).asBoolean()) |
yasin sapli | 9e3ebed | 2021-07-09 14:06:35 +0000 | [diff] [blame^] | 195 | .setIsPppoeRequired(node.get(IS_PPPOE_REQ) != null && node.get(IS_PPPOE_REQ).asBoolean()) |
Matteo Scandolo | 198aef9 | 2020-01-08 00:43:17 +0000 | [diff] [blame] | 196 | .setConfiguredMacAddress(node.get(MAC_ADDRESS) == null ? MacAddress.NONE.toString() : |
| 197 | node.get(MAC_ADDRESS).asText()) |
| 198 | .build(); |
| 199 | } |
Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame] | 200 | } |