blob: 61e097bb6d0c6680f7f2468a3309b6753202feb5 [file] [log] [blame]
Gamze Abaka1e5ccf52018-07-02 11:59:03 +00001/*
2 * Copyright 2017-present Open Networking Foundation
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.impl;
17
18import java.io.IOException;
19import java.util.ArrayList;
20import java.util.List;
21
22import org.onlab.packet.Ip4Address;
23import org.onlab.packet.VlanId;
24import org.opencord.sadis.BaseConfig;
25import org.opencord.sadis.SubscriberAndDeviceInformation;
26
27import org.slf4j.Logger;
28import org.slf4j.LoggerFactory;
29
30import com.fasterxml.jackson.core.JsonParser;
31import com.fasterxml.jackson.core.ObjectCodec;
32import com.fasterxml.jackson.databind.DeserializationContext;
33import com.fasterxml.jackson.databind.JsonDeserializer;
34import com.fasterxml.jackson.databind.JsonNode;
35import com.fasterxml.jackson.databind.ObjectMapper;
36import com.fasterxml.jackson.databind.module.SimpleModule;
37
38/**
39 * Configuration options for the Subscriber And Device Information Service.
40 *
41 * <pre>
42 * "sadis" : {
43 * "integration" : {
44 * "url" : "http://{hostname}{:port}",
45 * "cache" : {
46 * "maxsize" : number of entries,
47 * "entryttl" : duration, i.e. 10s or 1m
48 * }
49 * },
50 * "entries" : [
51 * {
52 * "id" : "uniqueid",
Matteo Scandoloe4f4b632020-01-07 23:54:35 +000053 * "ctag" : int,
54 * "stag" : int,
Gamze Abaka1e5ccf52018-07-02 11:59:03 +000055 * "nasportid" : string,
56 * "port" : int,
57 * "slot" : int,
58 * "hardwareidentifier" : string,
59 * "ipAddress" : string,
60 * "nasId" : string,
61 * "circuitId" : string,
Matteo Scandoloe4f4b632020-01-07 23:54:35 +000062 * "removeId" : string,
63 * "technologyProfileId" : int,
64 * "upstreamBandwidthProfile" : string,
65 * "downstreamBandwidthProfile" : string
Gamze Abaka1e5ccf52018-07-02 11:59:03 +000066 * }, ...
67 * ]
68 * }
69 * </pre>
70 */
71public class SubscriberAndDeviceInformationConfig extends BaseConfig<SubscriberAndDeviceInformation> {
72
73 private final Logger log = LoggerFactory.getLogger(this.getClass());
74
75 public List<SubscriberAndDeviceInformation> getEntries() {
76 List<SubscriberAndDeviceInformation> result = new ArrayList<>();
77 ObjectMapper mapper = new ObjectMapper();
78 SimpleModule module = new SimpleModule();
79 module.addDeserializer(VlanId.class, new VlanIdDeserializer());
80 module.addDeserializer(Ip4Address.class, new Ip4AddressDeserializer());
81 mapper.registerModule(module);
82 final JsonNode entries = this.object.path(ENTRIES);
83 entries.forEach(entry -> {
84 try {
85 result.add(mapper.readValue(entry.toString(), SubscriberAndDeviceInformation.class));
86 } catch (IOException e) {
87 log.warn("Unable to parse configuration entry, '{}', error: {}", entry, e.getMessage());
88 }
89 });
90
91 return result;
92 }
93
94 public class VlanIdDeserializer extends JsonDeserializer<VlanId> {
95 @Override
96 public VlanId deserialize(JsonParser jp, DeserializationContext ctxt)
97 throws IOException {
98 ObjectCodec oc = jp.getCodec();
99 JsonNode node = oc.readTree(jp);
100 return VlanId.vlanId((short) node.asInt());
101 }
102 }
103
104 public class Ip4AddressDeserializer extends JsonDeserializer<Ip4Address> {
105 @Override
106 public Ip4Address deserialize(JsonParser jp, DeserializationContext ctxt)
107 throws IOException {
108 ObjectCodec oc = jp.getCodec();
109 JsonNode node = oc.readTree(jp);
110 return Ip4Address.valueOf(node.asText());
111 }
112 }
Gamze Abaka1e5ccf52018-07-02 11:59:03 +0000113}