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