blob: c7efc21b37d3755d273f73170c9b9bce5e2ca6e4 [file] [log] [blame]
David K. Bainbridgeeda2b052017-07-12 09:41:04 -07001/*
Brian O'Connor180c1092017-08-03 22:46:14 -07002 * Copyright 2017-present Open Networking Foundation
David K. Bainbridgeeda2b052017-07-12 09:41:04 -07003 *
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 Ghoshc29c7a92017-08-01 09:59:13 +010016package org.opencord.sadis.impl;
David K. Bainbridgeeda2b052017-07-12 09:41:04 -070017
18import java.io.IOException;
19import java.net.MalformedURLException;
20import java.net.URL;
21import java.time.Duration;
22import java.util.ArrayList;
23import java.util.List;
24
Amit Ghosh38b232a2017-07-23 15:11:56 +010025import org.onlab.packet.Ip4Address;
David K. Bainbridgeeda2b052017-07-12 09:41:04 -070026import org.onlab.packet.VlanId;
27import org.onosproject.core.ApplicationId;
28import org.onosproject.net.config.Config;
Amit Ghoshc29c7a92017-08-01 09:59:13 +010029import org.opencord.sadis.SubscriberAndDeviceInformation;
30
David K. Bainbridgeeda2b052017-07-12 09:41:04 -070031import org.slf4j.Logger;
32import org.slf4j.LoggerFactory;
33
34import com.fasterxml.jackson.core.JsonParser;
35import com.fasterxml.jackson.core.JsonProcessingException;
36import com.fasterxml.jackson.core.ObjectCodec;
37import com.fasterxml.jackson.databind.DeserializationContext;
38import com.fasterxml.jackson.databind.JsonDeserializer;
39import com.fasterxml.jackson.databind.JsonNode;
40import com.fasterxml.jackson.databind.ObjectMapper;
41import 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" : {
51 * "enable" : true or false,
52 * "maxsize" : number of entries,
53 * "entryttl" : duration, i.e. 10s or 1m
54 * }
55 * },
56 * "entries" : [
57 * {
58 * "id" : "uniqueid",
59 * "ctag" : int,
60 * "stag" : int,
61 * "nasportid" : string,
62 * "port" : int,
63 * "slot" : int,
Amit Ghosh38b232a2017-07-23 15:11:56 +010064 * "hardwareidentifier" : string,
65 * "ipAddress" : string,
David K. Bainbridge8bf98e02017-08-07 10:41:56 -070066 * "nasId" : string,
67 * "circuitId" : string,
68 * "removeId" : string
David K. Bainbridgeeda2b052017-07-12 09:41:04 -070069 * }, ...
70 * ]
71 * }
72 * </pre>
73 */
74public class SadisConfig extends Config<ApplicationId> {
75
76 private final Logger log = LoggerFactory.getLogger(this.getClass());
77
78 private static final String SADIS_INTEGRATION = "integration";
79 private static final String SADIS_CACHE = "cache";
80 private static final String SADIS_CACHE_ENABLED = "enabled";
81 private static final String SADIS_CACHE_SIZE = "maxsize";
82 private static final String SADIS_CACHE_TTL = "ttl";
83 private static final String SADIS_URL = "url";
84 private static final String SADIS_ENTRIES = "entries";
85 private static final String DEFAULT_CACHE_TTL = "PT0S";
86
87 /**
88 * Returns SADIS integration URL.
89 *
90 * @return configured URL or null
91 * @throws MalformedURLException
92 * specified URL not valid
93 */
94 public URL getUrl() throws MalformedURLException {
95 final JsonNode integration = this.object.path(SADIS_INTEGRATION);
96 if (integration.isMissingNode()) {
97 return null;
98 }
99
100 final JsonNode url = integration.path(SADIS_URL);
101 if (url.isMissingNode()) {
102 return null;
103 }
104
105 return new URL(url.asText());
106 }
107
108 /**
109 * Returns configuration if cache should be used or not. Only used if
110 * integration URL is set.
111 *
112 * @return if cache should be used
113 */
114 public Boolean getCacheEnabled() {
115 final JsonNode integration = this.object.path(SADIS_INTEGRATION);
116 if (integration.isMissingNode()) {
117 return false;
118 }
119
120 final JsonNode cache = integration.path(SADIS_CACHE);
121 if (cache.isMissingNode()) {
122 return false;
123 }
124
125 return cache.path(SADIS_CACHE_ENABLED).asBoolean(false);
126 }
127
128 public int getCacheMaxSize() {
129 final JsonNode integration = this.object.path(SADIS_INTEGRATION);
130 if (integration.isMissingNode()) {
131 return -1;
132 }
133
134 final JsonNode cache = integration.path(SADIS_CACHE);
135 if (cache.isMissingNode()) {
136 return -1;
137 }
138
139 return cache.path(SADIS_CACHE_SIZE).asInt(-1);
140 }
141
142 public Duration getCacheTtl() {
143 final JsonNode integration = this.object.path(SADIS_INTEGRATION);
144 if (integration.isMissingNode()) {
145 return Duration.parse(DEFAULT_CACHE_TTL);
146 }
147
148 final JsonNode cache = integration.path(SADIS_CACHE);
149 if (cache.isMissingNode()) {
150 return Duration.parse(DEFAULT_CACHE_TTL);
151 }
152
153 return Duration.parse(cache.path(SADIS_CACHE_TTL).asText(DEFAULT_CACHE_TTL));
154 }
155
156 public List<SubscriberAndDeviceInformation> getEntries() {
157 List<SubscriberAndDeviceInformation> result = new ArrayList<SubscriberAndDeviceInformation>();
158 ObjectMapper mapper = new ObjectMapper();
159 SimpleModule module = new SimpleModule();
160 module.addDeserializer(VlanId.class, new VlanIdDeserializer());
Amit Ghosh38b232a2017-07-23 15:11:56 +0100161 module.addDeserializer(Ip4Address.class, new Ip4AddressDeserializer());
David K. Bainbridgeeda2b052017-07-12 09:41:04 -0700162 mapper.registerModule(module);
163 final JsonNode entries = this.object.path(SADIS_ENTRIES);
164 entries.forEach(entry -> {
165 try {
166 result.add(mapper.readValue(entry.toString(), SubscriberAndDeviceInformation.class));
167 } catch (IOException e) {
168 log.warn("Unable to parse configuration entry, '{}', error: {}", entry.toString(), e.getMessage());
169 }
170 });
171
172 return result;
173 }
174
175 public class VlanIdDeserializer extends JsonDeserializer<VlanId> {
176 @Override
177 public VlanId deserialize(JsonParser jp, DeserializationContext ctxt)
178 throws IOException, JsonProcessingException {
179 ObjectCodec oc = jp.getCodec();
180 JsonNode node = oc.readTree(jp);
181 return VlanId.vlanId((short) node.asInt());
182 }
183 }
Amit Ghosh38b232a2017-07-23 15:11:56 +0100184
185 public class Ip4AddressDeserializer extends JsonDeserializer<Ip4Address> {
186 @Override
187 public Ip4Address deserialize(JsonParser jp, DeserializationContext ctxt)
188 throws IOException, JsonProcessingException {
189 ObjectCodec oc = jp.getCodec();
190 JsonNode node = oc.readTree(jp);
191 return Ip4Address.valueOf((String) node.asText());
192 }
193 }
David K. Bainbridgeeda2b052017-07-12 09:41:04 -0700194}