blob: 75951f70ca5842ffe49ea35c5287ce20f1c074f1 [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;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import org.onosproject.core.ApplicationId;
20import org.onosproject.net.config.Config;
21import org.slf4j.Logger;
22import org.slf4j.LoggerFactory;
23
24import java.net.MalformedURLException;
25import java.net.URL;
26import java.time.Duration;
27import java.util.List;
28
29public abstract class BaseConfig<T extends BaseInformation> extends Config<ApplicationId> {
30
31 private final Logger log = LoggerFactory.getLogger(this.getClass());
32
33 protected static final String INTEGRATION = "integration";
34 protected static final String CACHE = "cache";
35 protected static final String CACHE_SIZE = "maxsize";
36 protected static final String CACHE_TTL = "ttl";
37 protected static final String URL = "url";
38 protected static final String ENTRIES = "entries";
39 protected static final String DEFAULT_CACHE_TTL = "PT0S";
40 protected static final String ID_SUB_PATTERN = "%s";
41
42 /**
43 * Returns Integration URL.
44 *
45 * @return configured URL or null
46 * @throws MalformedURLException specified URL not valid
47 */
48 public final URL getUrl() throws MalformedURLException {
49 final JsonNode integration = this.object.path(INTEGRATION);
50 if (integration.isMissingNode()) {
51 return null;
52 }
53
54 final JsonNode url = integration.path(URL);
55 if (url.isMissingNode()) {
56 return null;
57 }
58 StringBuilder buf = new StringBuilder(ID_SUB_PATTERN);
59 if (!url.asText().contains(buf)) {
60 log.error("Error in url, missing {}", ID_SUB_PATTERN);
61 return null;
62 }
63 return new URL(url.asText());
64 }
65
66 /**
67 * Returns Cache Maximum Size.
68 *
69 * @return configured cache max size or -1
70 */
71 public final int getCacheMaxSize() {
72 final JsonNode integration = this.object.path(INTEGRATION);
73 if (integration.isMissingNode()) {
74 return -1;
75 }
76
77 final JsonNode cache = integration.path(CACHE);
78 if (cache.isMissingNode()) {
79 return -1;
80 }
81
82 return cache.path(CACHE_SIZE).asInt(-1);
83 }
84
85 public final Duration getCacheTtl() {
86 final JsonNode integration = this.object.path(INTEGRATION);
87 if (integration.isMissingNode()) {
88 return Duration.parse(DEFAULT_CACHE_TTL);
89 }
90
91 final JsonNode cache = integration.path(CACHE);
92 if (cache.isMissingNode()) {
93 return Duration.parse(DEFAULT_CACHE_TTL);
94 }
95
96 return Duration.parse(cache.path(CACHE_TTL).asText(DEFAULT_CACHE_TTL));
97 }
98
99 public abstract List<T> getEntries();
100
101}