tony-liu | c3f9b65 | 2016-11-10 15:28:17 +0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
tony-liu | c3f9b65 | 2016-11-10 15:28:17 +0800 | [diff] [blame] | 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.tetunnel.impl; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableList; |
tony-liu | c3f9b65 | 2016-11-10 15:28:17 +0800 | [diff] [blame] | 20 | import org.onlab.util.KryoNamespace; |
| 21 | import org.onosproject.incubator.net.tunnel.TunnelId; |
| 22 | import org.onosproject.store.serializers.KryoNamespaces; |
| 23 | import org.onosproject.store.service.EventuallyConsistentMap; |
| 24 | import org.onosproject.store.service.LogicalClockService; |
| 25 | import org.onosproject.store.service.StorageService; |
| 26 | import org.onosproject.tetopology.management.api.TeTopologyKey; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 27 | import org.onosproject.tetunnel.api.TeTunnelStore; |
chenqinghui | 86320ae | 2016-12-01 15:40:39 +0800 | [diff] [blame] | 28 | import org.onosproject.tetunnel.api.lsp.TeLsp; |
| 29 | import org.onosproject.tetunnel.api.lsp.TeLspKey; |
tony-liu | c3f9b65 | 2016-11-10 15:28:17 +0800 | [diff] [blame] | 30 | import org.onosproject.tetunnel.api.tunnel.TeTunnel; |
tony-liu | c3f9b65 | 2016-11-10 15:28:17 +0800 | [diff] [blame] | 31 | import org.onosproject.tetunnel.api.tunnel.TeTunnelKey; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 32 | import org.osgi.service.component.annotations.Activate; |
| 33 | import org.osgi.service.component.annotations.Component; |
| 34 | import org.osgi.service.component.annotations.Deactivate; |
| 35 | import org.osgi.service.component.annotations.Reference; |
| 36 | import org.osgi.service.component.annotations.ReferenceCardinality; |
tony-liu | c3f9b65 | 2016-11-10 15:28:17 +0800 | [diff] [blame] | 37 | import org.slf4j.Logger; |
| 38 | |
| 39 | import java.util.Collection; |
| 40 | import java.util.stream.Collectors; |
| 41 | |
| 42 | import static org.slf4j.LoggerFactory.getLogger; |
| 43 | |
| 44 | /** |
| 45 | * Manages the TE tunnel attributes using an eventually consistent map. |
| 46 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 47 | @Component(immediate = true, service = TeTunnelStore.class) |
tony-liu | c3f9b65 | 2016-11-10 15:28:17 +0800 | [diff] [blame] | 48 | public class DistributedTeTunnelStore implements TeTunnelStore { |
| 49 | |
| 50 | private final Logger log = getLogger(getClass()); |
| 51 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 52 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
tony-liu | c3f9b65 | 2016-11-10 15:28:17 +0800 | [diff] [blame] | 53 | protected StorageService storageService; |
| 54 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 55 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
tony-liu | c3f9b65 | 2016-11-10 15:28:17 +0800 | [diff] [blame] | 56 | protected LogicalClockService clockService; |
| 57 | |
| 58 | private EventuallyConsistentMap<TeTunnelKey, TeTunnel> teTunnels; |
| 59 | private EventuallyConsistentMap<TeTunnelKey, TunnelId> tunnelIds; |
chenqinghui | 86320ae | 2016-12-01 15:40:39 +0800 | [diff] [blame] | 60 | private EventuallyConsistentMap<TeLspKey, TeLsp> lsps; |
tony-liu | c3f9b65 | 2016-11-10 15:28:17 +0800 | [diff] [blame] | 61 | |
| 62 | @Activate |
| 63 | public void activate() { |
| 64 | KryoNamespace.Builder serializer = KryoNamespace.newBuilder() |
| 65 | .register(KryoNamespaces.API) |
| 66 | .nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID) |
| 67 | .register(TeTunnel.class); |
| 68 | |
| 69 | teTunnels = storageService.<TeTunnelKey, TeTunnel>eventuallyConsistentMapBuilder() |
| 70 | .withName("TeTunnelStore") |
| 71 | .withSerializer(serializer) |
| 72 | .withTimestampProvider((k, v) -> clockService.getTimestamp()) |
| 73 | .build(); |
| 74 | |
| 75 | tunnelIds = storageService.<TeTunnelKey, TunnelId>eventuallyConsistentMapBuilder() |
| 76 | .withName("TeTunnelIdStore") |
| 77 | .withSerializer(serializer) |
| 78 | .withTimestampProvider((k, v) -> clockService.getTimestamp()) |
| 79 | .build(); |
| 80 | |
chenqinghui | 86320ae | 2016-12-01 15:40:39 +0800 | [diff] [blame] | 81 | lsps = storageService.<TeLspKey, TeLsp>eventuallyConsistentMapBuilder() |
| 82 | .withName("TeLspStore") |
| 83 | .withSerializer(serializer) |
| 84 | .withTimestampProvider((k, v) -> clockService.getTimestamp()) |
| 85 | .build(); |
| 86 | |
tony-liu | c3f9b65 | 2016-11-10 15:28:17 +0800 | [diff] [blame] | 87 | log.info("Started"); |
| 88 | } |
| 89 | |
| 90 | @Deactivate |
| 91 | public void deactivate() { |
| 92 | teTunnels.destroy(); |
| 93 | tunnelIds.destroy(); |
chenqinghui | 86320ae | 2016-12-01 15:40:39 +0800 | [diff] [blame] | 94 | lsps.destroy(); |
tony-liu | c3f9b65 | 2016-11-10 15:28:17 +0800 | [diff] [blame] | 95 | |
| 96 | log.info("Stopped"); |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public boolean addTeTunnel(TeTunnel teTunnel) { |
| 101 | if (teTunnel == null) { |
| 102 | log.warn("teTunnel is null"); |
| 103 | return false; |
| 104 | } |
| 105 | if (teTunnels.containsKey(teTunnel.teTunnelKey())) { |
| 106 | log.warn("teTunnel already exist"); |
| 107 | return false; |
| 108 | } |
| 109 | teTunnels.put(teTunnel.teTunnelKey(), teTunnel); |
| 110 | return true; |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public void setTunnelId(TeTunnelKey teTunnelKey, TunnelId tunnelId) { |
| 115 | tunnelIds.put(teTunnelKey, tunnelId); |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public TunnelId getTunnelId(TeTunnelKey teTunnelKey) { |
| 120 | return tunnelIds.get(teTunnelKey); |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public void updateTeTunnel(TeTunnel teTunnel) { |
| 125 | if (teTunnel == null) { |
| 126 | log.warn("TeTunnel is null"); |
| 127 | return; |
| 128 | } |
| 129 | teTunnels.put(teTunnel.teTunnelKey(), teTunnel); |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public void removeTeTunnel(TeTunnelKey teTunnelKey) { |
| 134 | teTunnels.remove(teTunnelKey); |
| 135 | tunnelIds.remove(teTunnelKey); |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public TeTunnel getTeTunnel(TeTunnelKey teTunnelKey) { |
| 140 | return teTunnels.get(teTunnelKey); |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public TeTunnel getTeTunnel(TunnelId tunnelId) { |
| 145 | if (tunnelIds.containsValue(tunnelId)) { |
| 146 | for (TeTunnel teTunnel : teTunnels.values()) { |
| 147 | if (tunnelIds.get(teTunnel.teTunnelKey()).equals(tunnelId)) { |
| 148 | return teTunnel; |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | return null; |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public Collection<TeTunnel> getTeTunnels() { |
| 157 | return ImmutableList.copyOf(teTunnels.values()); |
| 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public Collection<TeTunnel> getTeTunnels(TeTunnel.Type type) { |
| 162 | return ImmutableList.copyOf(teTunnels.values() |
| 163 | .stream() |
| 164 | .filter(teTunnel -> teTunnel.type().equals(type)) |
| 165 | .collect(Collectors.toList())); |
| 166 | } |
| 167 | |
| 168 | @Override |
| 169 | public Collection<TeTunnel> getTeTunnels(TeTopologyKey teTopologyKey) { |
| 170 | return ImmutableList.copyOf(teTunnels.values() |
| 171 | .stream() |
| 172 | .filter(teTunnel -> teTunnel.teTunnelKey() |
| 173 | .teTopologyKey() |
| 174 | .equals(teTopologyKey)) |
| 175 | .collect(Collectors.toList())); |
| 176 | } |
chenqinghui | 86320ae | 2016-12-01 15:40:39 +0800 | [diff] [blame] | 177 | |
| 178 | @Override |
| 179 | public boolean addTeLsp(TeLsp lsp) { |
| 180 | if (lsp == null) { |
| 181 | log.warn("TeLsp is null"); |
| 182 | return false; |
| 183 | } |
| 184 | if (lsps.containsKey(lsp.teLspKey())) { |
| 185 | log.error("TeLsp exist {}", lsp.teLspKey()); |
| 186 | return false; |
| 187 | } |
| 188 | lsps.put(lsp.teLspKey(), lsp); |
| 189 | return true; |
| 190 | } |
| 191 | |
| 192 | @Override |
| 193 | public void updateTeLsp(TeLsp lsp) { |
| 194 | if (lsp == null) { |
| 195 | log.warn("TeLsp is null"); |
| 196 | return; |
| 197 | } |
| 198 | lsps.put(lsp.teLspKey(), lsp); |
| 199 | } |
| 200 | |
| 201 | @Override |
| 202 | public void removeTeLsp(TeLspKey key) { |
| 203 | lsps.remove(key); |
| 204 | } |
| 205 | |
| 206 | @Override |
| 207 | public TeLsp getTeLsp(TeLspKey key) { |
| 208 | return lsps.get(key); |
| 209 | } |
| 210 | |
| 211 | @Override |
| 212 | public Collection<TeLsp> getTeLsps() { |
| 213 | return ImmutableList.copyOf(lsps.values()); |
| 214 | } |
tony-liu | c3f9b65 | 2016-11-10 15:28:17 +0800 | [diff] [blame] | 215 | } |