David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 180c109 | 2017-08-03 22:46:14 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 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 | */ |
Amit Ghosh | c29c7a9 | 2017-08-01 09:59:13 +0100 | [diff] [blame] | 16 | package org.opencord.sadis.impl; |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 17 | |
| 18 | import java.io.IOException; |
| 19 | import java.net.MalformedURLException; |
| 20 | import java.net.URL; |
| 21 | import java.time.Duration; |
| 22 | import java.util.ArrayList; |
| 23 | import java.util.List; |
| 24 | |
Amit Ghosh | 38b232a | 2017-07-23 15:11:56 +0100 | [diff] [blame] | 25 | import org.onlab.packet.Ip4Address; |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 26 | import org.onlab.packet.VlanId; |
| 27 | import org.onosproject.core.ApplicationId; |
| 28 | import org.onosproject.net.config.Config; |
Amit Ghosh | c29c7a9 | 2017-08-01 09:59:13 +0100 | [diff] [blame] | 29 | import org.opencord.sadis.SubscriberAndDeviceInformation; |
| 30 | |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 31 | import org.slf4j.Logger; |
| 32 | import org.slf4j.LoggerFactory; |
| 33 | |
| 34 | import com.fasterxml.jackson.core.JsonParser; |
| 35 | import com.fasterxml.jackson.core.JsonProcessingException; |
| 36 | import com.fasterxml.jackson.core.ObjectCodec; |
| 37 | import com.fasterxml.jackson.databind.DeserializationContext; |
| 38 | import com.fasterxml.jackson.databind.JsonDeserializer; |
| 39 | import com.fasterxml.jackson.databind.JsonNode; |
| 40 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 41 | import com.fasterxml.jackson.databind.module.SimpleModule; |
| 42 | |
| 43 | /** |
| 44 | * Configuration options for the Subscriber And Device Information Service. |
| 45 | * |
| 46 | * <pre> |
| 47 | * "sadis" : { |
| 48 | * "integration" : { |
| 49 | * "url" : "http://{hostname}{:port}", |
| 50 | * "cache" : { |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 51 | * "maxsize" : number of entries, |
| 52 | * "entryttl" : duration, i.e. 10s or 1m |
| 53 | * } |
| 54 | * }, |
| 55 | * "entries" : [ |
| 56 | * { |
| 57 | * "id" : "uniqueid", |
| 58 | * "ctag" : int, |
| 59 | * "stag" : int, |
| 60 | * "nasportid" : string, |
| 61 | * "port" : int, |
| 62 | * "slot" : int, |
Amit Ghosh | 38b232a | 2017-07-23 15:11:56 +0100 | [diff] [blame] | 63 | * "hardwareidentifier" : string, |
| 64 | * "ipAddress" : string, |
David K. Bainbridge | 8bf98e0 | 2017-08-07 10:41:56 -0700 | [diff] [blame] | 65 | * "nasId" : string, |
| 66 | * "circuitId" : string, |
| 67 | * "removeId" : string |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 68 | * }, ... |
| 69 | * ] |
| 70 | * } |
| 71 | * </pre> |
| 72 | */ |
| 73 | public class SadisConfig extends Config<ApplicationId> { |
| 74 | |
| 75 | private final Logger log = LoggerFactory.getLogger(this.getClass()); |
| 76 | |
| 77 | private static final String SADIS_INTEGRATION = "integration"; |
| 78 | private static final String SADIS_CACHE = "cache"; |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 79 | private static final String SADIS_CACHE_SIZE = "maxsize"; |
| 80 | private static final String SADIS_CACHE_TTL = "ttl"; |
| 81 | private static final String SADIS_URL = "url"; |
| 82 | private static final String SADIS_ENTRIES = "entries"; |
| 83 | private static final String DEFAULT_CACHE_TTL = "PT0S"; |
Amit Ghosh | 2c3ff59 | 2017-11-13 07:04:41 +0000 | [diff] [blame^] | 84 | private static final String SUBSCRIBER_ID_SUB_PATTERN = "%s"; |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 85 | |
| 86 | /** |
| 87 | * Returns SADIS integration URL. |
| 88 | * |
| 89 | * @return configured URL or null |
| 90 | * @throws MalformedURLException |
| 91 | * specified URL not valid |
| 92 | */ |
| 93 | public URL getUrl() throws MalformedURLException { |
| 94 | final JsonNode integration = this.object.path(SADIS_INTEGRATION); |
| 95 | if (integration.isMissingNode()) { |
| 96 | return null; |
| 97 | } |
| 98 | |
| 99 | final JsonNode url = integration.path(SADIS_URL); |
| 100 | if (url.isMissingNode()) { |
| 101 | return null; |
| 102 | } |
Amit Ghosh | 2c3ff59 | 2017-11-13 07:04:41 +0000 | [diff] [blame^] | 103 | StringBuffer buf = new StringBuffer(SUBSCRIBER_ID_SUB_PATTERN); |
| 104 | if (!url.asText().contains(buf)) { |
| 105 | log.error("Error in url, missing {}", SUBSCRIBER_ID_SUB_PATTERN); |
| 106 | return null; |
| 107 | } |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 108 | return new URL(url.asText()); |
| 109 | } |
| 110 | |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 111 | public int getCacheMaxSize() { |
| 112 | final JsonNode integration = this.object.path(SADIS_INTEGRATION); |
| 113 | if (integration.isMissingNode()) { |
| 114 | return -1; |
| 115 | } |
| 116 | |
| 117 | final JsonNode cache = integration.path(SADIS_CACHE); |
| 118 | if (cache.isMissingNode()) { |
| 119 | return -1; |
| 120 | } |
| 121 | |
| 122 | return cache.path(SADIS_CACHE_SIZE).asInt(-1); |
| 123 | } |
| 124 | |
| 125 | public Duration getCacheTtl() { |
| 126 | final JsonNode integration = this.object.path(SADIS_INTEGRATION); |
| 127 | if (integration.isMissingNode()) { |
| 128 | return Duration.parse(DEFAULT_CACHE_TTL); |
| 129 | } |
| 130 | |
| 131 | final JsonNode cache = integration.path(SADIS_CACHE); |
| 132 | if (cache.isMissingNode()) { |
| 133 | return Duration.parse(DEFAULT_CACHE_TTL); |
| 134 | } |
| 135 | |
| 136 | return Duration.parse(cache.path(SADIS_CACHE_TTL).asText(DEFAULT_CACHE_TTL)); |
| 137 | } |
| 138 | |
| 139 | public List<SubscriberAndDeviceInformation> getEntries() { |
| 140 | List<SubscriberAndDeviceInformation> result = new ArrayList<SubscriberAndDeviceInformation>(); |
| 141 | ObjectMapper mapper = new ObjectMapper(); |
| 142 | SimpleModule module = new SimpleModule(); |
| 143 | module.addDeserializer(VlanId.class, new VlanIdDeserializer()); |
Amit Ghosh | 38b232a | 2017-07-23 15:11:56 +0100 | [diff] [blame] | 144 | module.addDeserializer(Ip4Address.class, new Ip4AddressDeserializer()); |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 145 | mapper.registerModule(module); |
| 146 | final JsonNode entries = this.object.path(SADIS_ENTRIES); |
| 147 | entries.forEach(entry -> { |
| 148 | try { |
| 149 | result.add(mapper.readValue(entry.toString(), SubscriberAndDeviceInformation.class)); |
| 150 | } catch (IOException e) { |
| 151 | log.warn("Unable to parse configuration entry, '{}', error: {}", entry.toString(), e.getMessage()); |
| 152 | } |
| 153 | }); |
| 154 | |
| 155 | return result; |
| 156 | } |
| 157 | |
| 158 | public class VlanIdDeserializer extends JsonDeserializer<VlanId> { |
| 159 | @Override |
| 160 | public VlanId deserialize(JsonParser jp, DeserializationContext ctxt) |
| 161 | throws IOException, JsonProcessingException { |
| 162 | ObjectCodec oc = jp.getCodec(); |
| 163 | JsonNode node = oc.readTree(jp); |
| 164 | return VlanId.vlanId((short) node.asInt()); |
| 165 | } |
| 166 | } |
Amit Ghosh | 38b232a | 2017-07-23 15:11:56 +0100 | [diff] [blame] | 167 | |
| 168 | public class Ip4AddressDeserializer extends JsonDeserializer<Ip4Address> { |
| 169 | @Override |
| 170 | public Ip4Address deserialize(JsonParser jp, DeserializationContext ctxt) |
| 171 | throws IOException, JsonProcessingException { |
| 172 | ObjectCodec oc = jp.getCodec(); |
| 173 | JsonNode node = oc.readTree(jp); |
| 174 | return Ip4Address.valueOf((String) node.asText()); |
| 175 | } |
| 176 | } |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 177 | } |