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 | |
| 19 | import java.util.ArrayList; |
| 20 | import java.util.List; |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 21 | import java.util.Map; |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame] | 22 | import java.util.NoSuchElementException; |
| 23 | import java.util.concurrent.CopyOnWriteArrayList; |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 24 | import java.util.stream.Collectors; |
| 25 | import java.util.stream.Stream; |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame] | 26 | import org.apache.felix.scr.annotations.Activate; |
| 27 | import org.apache.felix.scr.annotations.Component; |
| 28 | import org.apache.felix.scr.annotations.Deactivate; |
| 29 | import org.apache.felix.scr.annotations.Service; |
| 30 | import org.onosproject.net.ConnectPoint; |
| 31 | import org.onosproject.net.ElementId; |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 32 | import org.onosproject.odtn.TapiResolver; |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame] | 33 | import org.onosproject.odtn.utils.tapi.TapiNepRef; |
| 34 | import org.onosproject.odtn.utils.tapi.TapiNodeRef; |
| 35 | import org.slf4j.Logger; |
| 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 | */ |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame] | 42 | @Component(immediate = true) |
| 43 | @Service |
| 44 | public 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 |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 56 | * <p> |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame] | 57 | * 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 |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 65 | * <p> |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame] | 66 | * 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 |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 104 | 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 |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame] | 119 | public TapiNodeRef getNodeRef(ElementId deviceId) throws NoSuchElementException { |
| 120 | updateCache(); |
| 121 | TapiNodeRef ret = null; |
| 122 | try { |
| 123 | ret = tapiNodeRefList.stream() |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 124 | .filter(node -> node.getDeviceId() != null && node.getDeviceId().equals(deviceId)) |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame] | 125 | .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 |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 140 | 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 |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame] | 165 | public TapiNepRef getNepRef(ConnectPoint cp) throws NoSuchElementException { |
| 166 | updateCache(); |
| 167 | TapiNepRef ret = null; |
| 168 | try { |
| 169 | ret = tapiNepRefList.stream() |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 170 | .filter(nep -> nep.getConnectPoint() != null && nep.getConnectPoint().equals(cp)) |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame] | 171 | .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() |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 185 | .filter(nep -> nep.getSipId() != null && nep.getSipId().equals(sipId)) |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame] | 186 | .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 |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 201 | 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 |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame] | 211 | 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 | } |