hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.odtn.internal; |
| 18 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 19 | import org.onosproject.net.ConnectPoint; |
| 20 | import org.onosproject.net.ElementId; |
| 21 | import org.onosproject.odtn.TapiResolver; |
| 22 | import org.onosproject.odtn.utils.tapi.TapiNepRef; |
| 23 | import org.onosproject.odtn.utils.tapi.TapiNodeRef; |
| 24 | import org.osgi.service.component.annotations.Activate; |
| 25 | import org.osgi.service.component.annotations.Component; |
| 26 | import org.osgi.service.component.annotations.Deactivate; |
| 27 | import org.slf4j.Logger; |
| 28 | |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame] | 29 | import java.util.ArrayList; |
| 30 | import java.util.List; |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 31 | import java.util.Map; |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame] | 32 | import java.util.NoSuchElementException; |
| 33 | import java.util.concurrent.CopyOnWriteArrayList; |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 34 | import java.util.stream.Collectors; |
| 35 | import java.util.stream.Stream; |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame] | 36 | |
| 37 | import static org.slf4j.LoggerFactory.getLogger; |
| 38 | |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 39 | /** |
| 40 | * OSGi Component for ODTN TAPI resolver application. |
| 41 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 42 | @Component(immediate = true, service = TapiResolver.class) |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame] | 43 | public 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 |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 55 | * <p> |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame] | 56 | * 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 |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 64 | * <p> |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame] | 65 | * 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 |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 103 | 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 |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame] | 118 | public TapiNodeRef getNodeRef(ElementId deviceId) throws NoSuchElementException { |
| 119 | updateCache(); |
| 120 | TapiNodeRef ret = null; |
| 121 | try { |
| 122 | ret = tapiNodeRefList.stream() |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 123 | .filter(node -> node.getDeviceId() != null && node.getDeviceId().equals(deviceId)) |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame] | 124 | .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 |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 139 | 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 |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame] | 164 | public TapiNepRef getNepRef(ConnectPoint cp) throws NoSuchElementException { |
| 165 | updateCache(); |
| 166 | TapiNepRef ret = null; |
| 167 | try { |
| 168 | ret = tapiNepRefList.stream() |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 169 | .filter(nep -> nep.getConnectPoint() != null && nep.getConnectPoint().equals(cp)) |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame] | 170 | .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() |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 184 | .filter(nep -> nep.getSipId() != null && nep.getSipId().equals(sipId)) |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame] | 185 | .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 |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 200 | 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 |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame] | 210 | 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 | } |