blob: fd9689fdc74585605b06f1fb4a04dcf967f3b6fc [file] [log] [blame]
David K. Bainbridgeeda2b052017-07-12 09:41:04 -07001/*
2 * Copyright 2017-present Open Networking Laboratory
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;
17
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;
29import org.slf4j.Logger;
30import org.slf4j.LoggerFactory;
31
32import com.fasterxml.jackson.core.JsonParser;
33import com.fasterxml.jackson.core.JsonProcessingException;
34import com.fasterxml.jackson.core.ObjectCodec;
35import com.fasterxml.jackson.databind.DeserializationContext;
36import com.fasterxml.jackson.databind.JsonDeserializer;
37import com.fasterxml.jackson.databind.JsonNode;
38import com.fasterxml.jackson.databind.ObjectMapper;
39import com.fasterxml.jackson.databind.module.SimpleModule;
40
41/**
42 * Configuration options for the Subscriber And Device Information Service.
43 *
44 * <pre>
45 * "sadis" : {
46 * "integration" : {
47 * "url" : "http://{hostname}{:port}",
48 * "cache" : {
49 * "enable" : true or false,
50 * "maxsize" : number of entries,
51 * "entryttl" : duration, i.e. 10s or 1m
52 * }
53 * },
54 * "entries" : [
55 * {
56 * "id" : "uniqueid",
57 * "ctag" : int,
58 * "stag" : int,
59 * "nasportid" : string,
60 * "port" : int,
61 * "slot" : int,
Amit Ghosh38b232a2017-07-23 15:11:56 +010062 * "hardwareidentifier" : string,
63 * "ipAddress" : string,
64 * "nasId" : string
David K. Bainbridgeeda2b052017-07-12 09:41:04 -070065 * }, ...
66 * ]
67 * }
68 * </pre>
69 */
70public class SadisConfig extends Config<ApplicationId> {
71
72 private final Logger log = LoggerFactory.getLogger(this.getClass());
73
74 private static final String SADIS_INTEGRATION = "integration";
75 private static final String SADIS_CACHE = "cache";
76 private static final String SADIS_CACHE_ENABLED = "enabled";
77 private static final String SADIS_CACHE_SIZE = "maxsize";
78 private static final String SADIS_CACHE_TTL = "ttl";
79 private static final String SADIS_URL = "url";
80 private static final String SADIS_ENTRIES = "entries";
81 private static final String DEFAULT_CACHE_TTL = "PT0S";
82
83 /**
84 * Returns SADIS integration URL.
85 *
86 * @return configured URL or null
87 * @throws MalformedURLException
88 * specified URL not valid
89 */
90 public URL getUrl() throws MalformedURLException {
91 final JsonNode integration = this.object.path(SADIS_INTEGRATION);
92 if (integration.isMissingNode()) {
93 return null;
94 }
95
96 final JsonNode url = integration.path(SADIS_URL);
97 if (url.isMissingNode()) {
98 return null;
99 }
100
101 return new URL(url.asText());
102 }
103
104 /**
105 * Returns configuration if cache should be used or not. Only used if
106 * integration URL is set.
107 *
108 * @return if cache should be used
109 */
110 public Boolean getCacheEnabled() {
111 final JsonNode integration = this.object.path(SADIS_INTEGRATION);
112 if (integration.isMissingNode()) {
113 return false;
114 }
115
116 final JsonNode cache = integration.path(SADIS_CACHE);
117 if (cache.isMissingNode()) {
118 return false;
119 }
120
121 return cache.path(SADIS_CACHE_ENABLED).asBoolean(false);
122 }
123
124 public int getCacheMaxSize() {
125 final JsonNode integration = this.object.path(SADIS_INTEGRATION);
126 if (integration.isMissingNode()) {
127 return -1;
128 }
129
130 final JsonNode cache = integration.path(SADIS_CACHE);
131 if (cache.isMissingNode()) {
132 return -1;
133 }
134
135 return cache.path(SADIS_CACHE_SIZE).asInt(-1);
136 }
137
138 public Duration getCacheTtl() {
139 final JsonNode integration = this.object.path(SADIS_INTEGRATION);
140 if (integration.isMissingNode()) {
141 return Duration.parse(DEFAULT_CACHE_TTL);
142 }
143
144 final JsonNode cache = integration.path(SADIS_CACHE);
145 if (cache.isMissingNode()) {
146 return Duration.parse(DEFAULT_CACHE_TTL);
147 }
148
149 return Duration.parse(cache.path(SADIS_CACHE_TTL).asText(DEFAULT_CACHE_TTL));
150 }
151
152 public List<SubscriberAndDeviceInformation> getEntries() {
153 List<SubscriberAndDeviceInformation> result = new ArrayList<SubscriberAndDeviceInformation>();
154 ObjectMapper mapper = new ObjectMapper();
155 SimpleModule module = new SimpleModule();
156 module.addDeserializer(VlanId.class, new VlanIdDeserializer());
Amit Ghosh38b232a2017-07-23 15:11:56 +0100157 module.addDeserializer(Ip4Address.class, new Ip4AddressDeserializer());
David K. Bainbridgeeda2b052017-07-12 09:41:04 -0700158 mapper.registerModule(module);
159 final JsonNode entries = this.object.path(SADIS_ENTRIES);
160 entries.forEach(entry -> {
161 try {
162 result.add(mapper.readValue(entry.toString(), SubscriberAndDeviceInformation.class));
163 } catch (IOException e) {
164 log.warn("Unable to parse configuration entry, '{}', error: {}", entry.toString(), e.getMessage());
165 }
166 });
167
168 return result;
169 }
170
171 public class VlanIdDeserializer extends JsonDeserializer<VlanId> {
172 @Override
173 public VlanId deserialize(JsonParser jp, DeserializationContext ctxt)
174 throws IOException, JsonProcessingException {
175 ObjectCodec oc = jp.getCodec();
176 JsonNode node = oc.readTree(jp);
177 return VlanId.vlanId((short) node.asInt());
178 }
179 }
Amit Ghosh38b232a2017-07-23 15:11:56 +0100180
181 public class Ip4AddressDeserializer extends JsonDeserializer<Ip4Address> {
182 @Override
183 public Ip4Address deserialize(JsonParser jp, DeserializationContext ctxt)
184 throws IOException, JsonProcessingException {
185 ObjectCodec oc = jp.getCodec();
186 JsonNode node = oc.readTree(jp);
187 return Ip4Address.valueOf((String) node.asText());
188 }
189 }
David K. Bainbridgeeda2b052017-07-12 09:41:04 -0700190}