blob: 85a5bd667bc2585256ee3e7bb3d0c8e8b089ba67 [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,
66 * "nasId" : string
David K. Bainbridgeeda2b052017-07-12 09:41:04 -070067 * }, ...
68 * ]
69 * }
70 * </pre>
71 */
72public class SadisConfig extends Config<ApplicationId> {
73
74 private final Logger log = LoggerFactory.getLogger(this.getClass());
75
76 private static final String SADIS_INTEGRATION = "integration";
77 private static final String SADIS_CACHE = "cache";
78 private static final String SADIS_CACHE_ENABLED = "enabled";
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";
84
85 /**
86 * Returns SADIS integration URL.
87 *
88 * @return configured URL or null
89 * @throws MalformedURLException
90 * specified URL not valid
91 */
92 public URL getUrl() throws MalformedURLException {
93 final JsonNode integration = this.object.path(SADIS_INTEGRATION);
94 if (integration.isMissingNode()) {
95 return null;
96 }
97
98 final JsonNode url = integration.path(SADIS_URL);
99 if (url.isMissingNode()) {
100 return null;
101 }
102
103 return new URL(url.asText());
104 }
105
106 /**
107 * Returns configuration if cache should be used or not. Only used if
108 * integration URL is set.
109 *
110 * @return if cache should be used
111 */
112 public Boolean getCacheEnabled() {
113 final JsonNode integration = this.object.path(SADIS_INTEGRATION);
114 if (integration.isMissingNode()) {
115 return false;
116 }
117
118 final JsonNode cache = integration.path(SADIS_CACHE);
119 if (cache.isMissingNode()) {
120 return false;
121 }
122
123 return cache.path(SADIS_CACHE_ENABLED).asBoolean(false);
124 }
125
126 public int getCacheMaxSize() {
127 final JsonNode integration = this.object.path(SADIS_INTEGRATION);
128 if (integration.isMissingNode()) {
129 return -1;
130 }
131
132 final JsonNode cache = integration.path(SADIS_CACHE);
133 if (cache.isMissingNode()) {
134 return -1;
135 }
136
137 return cache.path(SADIS_CACHE_SIZE).asInt(-1);
138 }
139
140 public Duration getCacheTtl() {
141 final JsonNode integration = this.object.path(SADIS_INTEGRATION);
142 if (integration.isMissingNode()) {
143 return Duration.parse(DEFAULT_CACHE_TTL);
144 }
145
146 final JsonNode cache = integration.path(SADIS_CACHE);
147 if (cache.isMissingNode()) {
148 return Duration.parse(DEFAULT_CACHE_TTL);
149 }
150
151 return Duration.parse(cache.path(SADIS_CACHE_TTL).asText(DEFAULT_CACHE_TTL));
152 }
153
154 public List<SubscriberAndDeviceInformation> getEntries() {
155 List<SubscriberAndDeviceInformation> result = new ArrayList<SubscriberAndDeviceInformation>();
156 ObjectMapper mapper = new ObjectMapper();
157 SimpleModule module = new SimpleModule();
158 module.addDeserializer(VlanId.class, new VlanIdDeserializer());
Amit Ghosh38b232a2017-07-23 15:11:56 +0100159 module.addDeserializer(Ip4Address.class, new Ip4AddressDeserializer());
David K. Bainbridgeeda2b052017-07-12 09:41:04 -0700160 mapper.registerModule(module);
161 final JsonNode entries = this.object.path(SADIS_ENTRIES);
162 entries.forEach(entry -> {
163 try {
164 result.add(mapper.readValue(entry.toString(), SubscriberAndDeviceInformation.class));
165 } catch (IOException e) {
166 log.warn("Unable to parse configuration entry, '{}', error: {}", entry.toString(), e.getMessage());
167 }
168 });
169
170 return result;
171 }
172
173 public class VlanIdDeserializer extends JsonDeserializer<VlanId> {
174 @Override
175 public VlanId deserialize(JsonParser jp, DeserializationContext ctxt)
176 throws IOException, JsonProcessingException {
177 ObjectCodec oc = jp.getCodec();
178 JsonNode node = oc.readTree(jp);
179 return VlanId.vlanId((short) node.asInt());
180 }
181 }
Amit Ghosh38b232a2017-07-23 15:11:56 +0100182
183 public class Ip4AddressDeserializer extends JsonDeserializer<Ip4Address> {
184 @Override
185 public Ip4Address deserialize(JsonParser jp, DeserializationContext ctxt)
186 throws IOException, JsonProcessingException {
187 ObjectCodec oc = jp.getCodec();
188 JsonNode node = oc.readTree(jp);
189 return Ip4Address.valueOf((String) node.asText());
190 }
191 }
David K. Bainbridgeeda2b052017-07-12 09:41:04 -0700192}