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; |
| 33 | import org.onosproject.tetunnel.api.tunnel.TeTunnel; |
| 34 | import org.onosproject.tetunnel.api.TeTunnelStore; |
| 35 | import org.onosproject.tetunnel.api.tunnel.TeTunnelKey; |
| 36 | import org.slf4j.Logger; |
| 37 | |
| 38 | import java.util.Collection; |
| 39 | import java.util.stream.Collectors; |
| 40 | |
| 41 | import static org.slf4j.LoggerFactory.getLogger; |
| 42 | |
| 43 | /** |
| 44 | * Manages the TE tunnel attributes using an eventually consistent map. |
| 45 | */ |
| 46 | @Component(immediate = true) |
| 47 | @Service |
| 48 | public class DistributedTeTunnelStore implements TeTunnelStore { |
| 49 | |
| 50 | private final Logger log = getLogger(getClass()); |
| 51 | |
| 52 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 53 | protected StorageService storageService; |
| 54 | |
| 55 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 56 | protected LogicalClockService clockService; |
| 57 | |
| 58 | private EventuallyConsistentMap<TeTunnelKey, TeTunnel> teTunnels; |
| 59 | private EventuallyConsistentMap<TeTunnelKey, TunnelId> tunnelIds; |
| 60 | |
| 61 | @Activate |
| 62 | public void activate() { |
| 63 | KryoNamespace.Builder serializer = KryoNamespace.newBuilder() |
| 64 | .register(KryoNamespaces.API) |
| 65 | .nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID) |
| 66 | .register(TeTunnel.class); |
| 67 | |
| 68 | teTunnels = storageService.<TeTunnelKey, TeTunnel>eventuallyConsistentMapBuilder() |
| 69 | .withName("TeTunnelStore") |
| 70 | .withSerializer(serializer) |
| 71 | .withTimestampProvider((k, v) -> clockService.getTimestamp()) |
| 72 | .build(); |
| 73 | |
| 74 | tunnelIds = storageService.<TeTunnelKey, TunnelId>eventuallyConsistentMapBuilder() |
| 75 | .withName("TeTunnelIdStore") |
| 76 | .withSerializer(serializer) |
| 77 | .withTimestampProvider((k, v) -> clockService.getTimestamp()) |
| 78 | .build(); |
| 79 | |
| 80 | log.info("Started"); |
| 81 | } |
| 82 | |
| 83 | @Deactivate |
| 84 | public void deactivate() { |
| 85 | teTunnels.destroy(); |
| 86 | tunnelIds.destroy(); |
| 87 | |
| 88 | log.info("Stopped"); |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public boolean addTeTunnel(TeTunnel teTunnel) { |
| 93 | if (teTunnel == null) { |
| 94 | log.warn("teTunnel is null"); |
| 95 | return false; |
| 96 | } |
| 97 | if (teTunnels.containsKey(teTunnel.teTunnelKey())) { |
| 98 | log.warn("teTunnel already exist"); |
| 99 | return false; |
| 100 | } |
| 101 | teTunnels.put(teTunnel.teTunnelKey(), teTunnel); |
| 102 | return true; |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public void setTunnelId(TeTunnelKey teTunnelKey, TunnelId tunnelId) { |
| 107 | tunnelIds.put(teTunnelKey, tunnelId); |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public TunnelId getTunnelId(TeTunnelKey teTunnelKey) { |
| 112 | return tunnelIds.get(teTunnelKey); |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public void updateTeTunnel(TeTunnel teTunnel) { |
| 117 | if (teTunnel == null) { |
| 118 | log.warn("TeTunnel is null"); |
| 119 | return; |
| 120 | } |
| 121 | teTunnels.put(teTunnel.teTunnelKey(), teTunnel); |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public void removeTeTunnel(TeTunnelKey teTunnelKey) { |
| 126 | teTunnels.remove(teTunnelKey); |
| 127 | tunnelIds.remove(teTunnelKey); |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public TeTunnel getTeTunnel(TeTunnelKey teTunnelKey) { |
| 132 | return teTunnels.get(teTunnelKey); |
| 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public TeTunnel getTeTunnel(TunnelId tunnelId) { |
| 137 | if (tunnelIds.containsValue(tunnelId)) { |
| 138 | for (TeTunnel teTunnel : teTunnels.values()) { |
| 139 | if (tunnelIds.get(teTunnel.teTunnelKey()).equals(tunnelId)) { |
| 140 | return teTunnel; |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | return null; |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public Collection<TeTunnel> getTeTunnels() { |
| 149 | return ImmutableList.copyOf(teTunnels.values()); |
| 150 | } |
| 151 | |
| 152 | @Override |
| 153 | public Collection<TeTunnel> getTeTunnels(TeTunnel.Type type) { |
| 154 | return ImmutableList.copyOf(teTunnels.values() |
| 155 | .stream() |
| 156 | .filter(teTunnel -> teTunnel.type().equals(type)) |
| 157 | .collect(Collectors.toList())); |
| 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public Collection<TeTunnel> getTeTunnels(TeTopologyKey teTopologyKey) { |
| 162 | return ImmutableList.copyOf(teTunnels.values() |
| 163 | .stream() |
| 164 | .filter(teTunnel -> teTunnel.teTunnelKey() |
| 165 | .teTopologyKey() |
| 166 | .equals(teTopologyKey)) |
| 167 | .collect(Collectors.toList())); |
| 168 | } |
| 169 | } |