blob: 42a11af26b601f84a94c9a9f256224eac5a4bd42 [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" : {
David K. Bainbridgeeda2b052017-07-12 09:41:04 -070051 * "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 Ghosh38b232a2017-07-23 15:11:56 +010063 * "hardwareidentifier" : string,
64 * "ipAddress" : string,
David K. Bainbridge8bf98e02017-08-07 10:41:56 -070065 * "nasId" : string,
66 * "circuitId" : string,
67 * "removeId" : string
David K. Bainbridgeeda2b052017-07-12 09:41:04 -070068 * }, ...
69 * ]
70 * }
71 * </pre>
72 */
73public 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. Bainbridgeeda2b052017-07-12 09:41:04 -070079 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
David K. Bainbridgeeda2b052017-07-12 09:41:04 -0700106 public int getCacheMaxSize() {
107 final JsonNode integration = this.object.path(SADIS_INTEGRATION);
108 if (integration.isMissingNode()) {
109 return -1;
110 }
111
112 final JsonNode cache = integration.path(SADIS_CACHE);
113 if (cache.isMissingNode()) {
114 return -1;
115 }
116
117 return cache.path(SADIS_CACHE_SIZE).asInt(-1);
118 }
119
120 public Duration getCacheTtl() {
121 final JsonNode integration = this.object.path(SADIS_INTEGRATION);
122 if (integration.isMissingNode()) {
123 return Duration.parse(DEFAULT_CACHE_TTL);
124 }
125
126 final JsonNode cache = integration.path(SADIS_CACHE);
127 if (cache.isMissingNode()) {
128 return Duration.parse(DEFAULT_CACHE_TTL);
129 }
130
131 return Duration.parse(cache.path(SADIS_CACHE_TTL).asText(DEFAULT_CACHE_TTL));
132 }
133
134 public List<SubscriberAndDeviceInformation> getEntries() {
135 List<SubscriberAndDeviceInformation> result = new ArrayList<SubscriberAndDeviceInformation>();
136 ObjectMapper mapper = new ObjectMapper();
137 SimpleModule module = new SimpleModule();
138 module.addDeserializer(VlanId.class, new VlanIdDeserializer());
Amit Ghosh38b232a2017-07-23 15:11:56 +0100139 module.addDeserializer(Ip4Address.class, new Ip4AddressDeserializer());
David K. Bainbridgeeda2b052017-07-12 09:41:04 -0700140 mapper.registerModule(module);
141 final JsonNode entries = this.object.path(SADIS_ENTRIES);
142 entries.forEach(entry -> {
143 try {
144 result.add(mapper.readValue(entry.toString(), SubscriberAndDeviceInformation.class));
145 } catch (IOException e) {
146 log.warn("Unable to parse configuration entry, '{}', error: {}", entry.toString(), e.getMessage());
147 }
148 });
149
150 return result;
151 }
152
153 public class VlanIdDeserializer extends JsonDeserializer<VlanId> {
154 @Override
155 public VlanId deserialize(JsonParser jp, DeserializationContext ctxt)
156 throws IOException, JsonProcessingException {
157 ObjectCodec oc = jp.getCodec();
158 JsonNode node = oc.readTree(jp);
159 return VlanId.vlanId((short) node.asInt());
160 }
161 }
Amit Ghosh38b232a2017-07-23 15:11:56 +0100162
163 public class Ip4AddressDeserializer extends JsonDeserializer<Ip4Address> {
164 @Override
165 public Ip4Address deserialize(JsonParser jp, DeserializationContext ctxt)
166 throws IOException, JsonProcessingException {
167 ObjectCodec oc = jp.getCodec();
168 JsonNode node = oc.readTree(jp);
169 return Ip4Address.valueOf((String) node.asText());
170 }
171 }
David K. Bainbridgeeda2b052017-07-12 09:41:04 -0700172}