blob: eb48fe00de0249787c4cde36c09ae503239587e5 [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";
Amit Ghosh2c3ff592017-11-13 07:04:41 +000084 private static final String SUBSCRIBER_ID_SUB_PATTERN = "%s";
David K. Bainbridgeeda2b052017-07-12 09:41:04 -070085
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 Ghosh2c3ff592017-11-13 07:04:41 +0000103 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. Bainbridgeeda2b052017-07-12 09:41:04 -0700108 return new URL(url.asText());
109 }
110
David K. Bainbridgeeda2b052017-07-12 09:41:04 -0700111 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 Ghosh38b232a2017-07-23 15:11:56 +0100144 module.addDeserializer(Ip4Address.class, new Ip4AddressDeserializer());
David K. Bainbridgeeda2b052017-07-12 09:41:04 -0700145 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 Ghosh38b232a2017-07-23 15:11:56 +0100167
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. Bainbridgeeda2b052017-07-12 09:41:04 -0700177}