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