blob: 81098b4de09750c76d7fd216639fd7f90379edd4 [file] [log] [blame]
hiroki684aa2f2018-05-19 20:48:49 -07001/*
2 * Copyright 2018-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 */
16
17package org.onosproject.odtn.internal;
18
Ray Milkeyd84f89b2018-08-17 14:54:17 -070019import org.onosproject.net.ConnectPoint;
20import org.onosproject.net.ElementId;
21import org.onosproject.odtn.TapiResolver;
22import org.onosproject.odtn.utils.tapi.TapiNepRef;
23import org.onosproject.odtn.utils.tapi.TapiNodeRef;
24import org.osgi.service.component.annotations.Activate;
25import org.osgi.service.component.annotations.Component;
26import org.osgi.service.component.annotations.Deactivate;
27import org.slf4j.Logger;
28
hiroki684aa2f2018-05-19 20:48:49 -070029import java.util.ArrayList;
30import java.util.List;
hirokif4ed5212018-05-26 22:39:38 -070031import java.util.Map;
hiroki684aa2f2018-05-19 20:48:49 -070032import java.util.NoSuchElementException;
33import java.util.concurrent.CopyOnWriteArrayList;
hirokif4ed5212018-05-26 22:39:38 -070034import java.util.stream.Collectors;
35import java.util.stream.Stream;
hiroki684aa2f2018-05-19 20:48:49 -070036
37import static org.slf4j.LoggerFactory.getLogger;
38
hirokif4ed5212018-05-26 22:39:38 -070039/**
40 * OSGi Component for ODTN TAPI resolver application.
41 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070042@Component(immediate = true, service = TapiResolver.class)
hiroki684aa2f2018-05-19 20:48:49 -070043public class DefaultTapiResolver implements TapiResolver {
44
45 private final Logger log = getLogger(getClass());
46
47 protected TapiDataProducer dataProvider = new DcsBasedTapiDataProducer();
48
49 private List<TapiNodeRef> tapiNodeRefList = new CopyOnWriteArrayList<>();
50 private List<TapiNepRef> tapiNepRefList = new CopyOnWriteArrayList<>();
51
52 /**
53 * When source (e.g. DCS) is updated, set true
54 * When cache update completed successfully, set false
hirokif4ed5212018-05-26 22:39:38 -070055 * <p>
hiroki684aa2f2018-05-19 20:48:49 -070056 * This flag takes effect when cache update failed with exception,
57 * this remains to be true so the cache update process conducts again
58 */
59 private Boolean isDirty = false;
60
61 /**
62 * When source (e.g. DCS) is updated, set true
63 * When cache update started, set false
hirokif4ed5212018-05-26 22:39:38 -070064 * <p>
hiroki684aa2f2018-05-19 20:48:49 -070065 * This flag takes effect when source updated during cache updating
66 * this forces cache update again at the next request
67 */
68 private Boolean sourceUpdated = false;
69
70 @Activate
71 public void activate() {
72 log.info("Started");
73 dataProvider.init();
74 }
75
76 @Deactivate
77 public void deactivate() {
78 log.info("Stopped");
79 }
80
81 @Override
82 public boolean hasNodeRef(ElementId deviceId) {
83 updateCache();
84 return tapiNodeRefList.stream()
85 .anyMatch(node -> node.getDeviceId().equals(deviceId));
86 }
87
88 @Override
89 public boolean hasNepRef(ConnectPoint cp) {
90 updateCache();
91 return tapiNepRefList.stream()
92 .anyMatch(nep -> nep.getConnectPoint().equals(cp));
93 }
94
95 @Override
96 public boolean hasNepRef(String sipId) {
97 updateCache();
98 return tapiNepRefList.stream()
99 .anyMatch(nep -> nep.getSipId().equals(sipId));
100 }
101
102 @Override
hirokif4ed5212018-05-26 22:39:38 -0700103 public TapiNodeRef getNodeRef(TapiNodeRef nodeRef) throws NoSuchElementException {
104 updateCache();
105 TapiNodeRef ret = null;
106 try {
107 ret = tapiNodeRefList.stream()
108 .filter(nodeRef::equals)
109 .findFirst().get();
110 } catch (NoSuchElementException e) {
111 log.error("Node not found of {}", nodeRef);
112 throw e;
113 }
114 return ret;
115 }
116
117 @Override
hiroki684aa2f2018-05-19 20:48:49 -0700118 public TapiNodeRef getNodeRef(ElementId deviceId) throws NoSuchElementException {
119 updateCache();
120 TapiNodeRef ret = null;
121 try {
122 ret = tapiNodeRefList.stream()
hirokif4ed5212018-05-26 22:39:38 -0700123 .filter(node -> node.getDeviceId() != null && node.getDeviceId().equals(deviceId))
hiroki684aa2f2018-05-19 20:48:49 -0700124 .findFirst().get();
125 } catch (NoSuchElementException e) {
126 log.error("Node not found associated with {}", deviceId);
127 throw e;
128 }
129 return ret;
130 }
131
132 @Override
133 public List<TapiNodeRef> getNodeRefs() {
134 updateCache();
135 return new ArrayList<>(tapiNodeRefList);
136 }
137
138 @Override
hirokif4ed5212018-05-26 22:39:38 -0700139 public List<TapiNodeRef> getNodeRefs(Map<String, String> filter) {
140 updateCache();
141 Stream<TapiNodeRef> filterStream = tapiNodeRefList.stream();
142 for (String key : filter.keySet()) {
143 filterStream = filterStream.filter(nodeRef -> nodeRef.is(key, filter.get(key)));
144 }
145 return filterStream.collect(Collectors.toList());
146 }
147
148 @Override
149 public TapiNepRef getNepRef(TapiNepRef nepRef) throws NoSuchElementException {
150 updateCache();
151 TapiNepRef ret = null;
152 try {
153 ret = tapiNepRefList.stream()
154 .filter(nepRef::equals)
155 .findFirst().get();
156 } catch (NoSuchElementException e) {
157 log.error("Nep not found of {}", nepRef);
158 throw e;
159 }
160 return ret;
161 }
162
163 @Override
hiroki684aa2f2018-05-19 20:48:49 -0700164 public TapiNepRef getNepRef(ConnectPoint cp) throws NoSuchElementException {
165 updateCache();
166 TapiNepRef ret = null;
167 try {
168 ret = tapiNepRefList.stream()
hirokif4ed5212018-05-26 22:39:38 -0700169 .filter(nep -> nep.getConnectPoint() != null && nep.getConnectPoint().equals(cp))
hiroki684aa2f2018-05-19 20:48:49 -0700170 .findFirst().get();
171 } catch (NoSuchElementException e) {
172 log.error("Nep not found associated with {}", cp);
173 throw e;
174 }
175 return ret;
176 }
177
178 @Override
179 public TapiNepRef getNepRef(String sipId) throws NoSuchElementException {
180 updateCache();
181 TapiNepRef ret = null;
182 try {
183 ret = tapiNepRefList.stream()
hirokif4ed5212018-05-26 22:39:38 -0700184 .filter(nep -> nep.getSipId() != null && nep.getSipId().equals(sipId))
hiroki684aa2f2018-05-19 20:48:49 -0700185 .findFirst().get();
186 } catch (NoSuchElementException e) {
187 log.error("Nep not found associated with {}", sipId);
188 throw e;
189 }
190 return ret;
191 }
192
193 @Override
194 public List<TapiNepRef> getNepRefs() {
195 updateCache();
196 return new ArrayList<>(tapiNepRefList);
197 }
198
199 @Override
hirokif4ed5212018-05-26 22:39:38 -0700200 public List<TapiNepRef> getNepRefs(Map<String, String> filter) {
201 updateCache();
202 Stream<TapiNepRef> filterStream = tapiNepRefList.stream();
203 for (String key : filter.keySet()) {
204 filterStream = filterStream.filter(nepRef -> nepRef.is(key, filter.get(key)));
205 }
206 return filterStream.collect(Collectors.toList());
207 }
208
209 @Override
hiroki684aa2f2018-05-19 20:48:49 -0700210 public void makeDirty() {
211 sourceUpdated = true;
212 isDirty = true;
213 }
214
215 protected void addNodeRef(TapiNodeRef nodeRef) {
216 tapiNodeRefList.add(nodeRef);
217 log.info("Nodes: {}", tapiNodeRefList);
218 }
219
220 protected void addNepRef(TapiNepRef nepRef) {
221 tapiNepRefList.add(nepRef);
222 log.info("Neps: {}", tapiNepRefList);
223 }
224
225 protected void addNodeRefList(List<TapiNodeRef> nodes) {
226 tapiNodeRefList = nodes;
227 log.info("Nodes: {}", tapiNodeRefList);
228 }
229
230 protected void addNepRefList(List<TapiNepRef> neps) {
231 tapiNepRefList = neps;
232 log.info("Neps: {}", tapiNepRefList);
233 }
234
235 private void updateCache() {
236 log.info("Dirty: {}, Source updated: {}", isDirty, sourceUpdated);
237 if (isDirty || sourceUpdated) {
238 sourceUpdated = false;
239 clearCache();
240 dataProvider.updateCacheRequest(this);
241 log.info("Update completed: {}", tapiNodeRefList);
242 isDirty = false;
243 }
244 }
245
246 private void clearCache() {
247 tapiNodeRefList.clear();
248 tapiNepRefList.clear();
249 }
250}