blob: e9da5e44bd62651d987f77dbb6b902dbe5436125 [file] [log] [blame]
tony-liuc3f9b652016-11-10 15:28:17 +08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
tony-liuc3f9b652016-11-10 15:28:17 +08003 *
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
17package org.onosproject.tetunnel.impl;
18
19import com.google.common.collect.ImmutableList;
tony-liuc3f9b652016-11-10 15:28:17 +080020import org.onlab.util.KryoNamespace;
21import org.onosproject.incubator.net.tunnel.TunnelId;
22import org.onosproject.store.serializers.KryoNamespaces;
23import org.onosproject.store.service.EventuallyConsistentMap;
24import org.onosproject.store.service.LogicalClockService;
25import org.onosproject.store.service.StorageService;
26import org.onosproject.tetopology.management.api.TeTopologyKey;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070027import org.onosproject.tetunnel.api.TeTunnelStore;
chenqinghui86320ae2016-12-01 15:40:39 +080028import org.onosproject.tetunnel.api.lsp.TeLsp;
29import org.onosproject.tetunnel.api.lsp.TeLspKey;
tony-liuc3f9b652016-11-10 15:28:17 +080030import org.onosproject.tetunnel.api.tunnel.TeTunnel;
tony-liuc3f9b652016-11-10 15:28:17 +080031import org.onosproject.tetunnel.api.tunnel.TeTunnelKey;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070032import org.osgi.service.component.annotations.Activate;
33import org.osgi.service.component.annotations.Component;
34import org.osgi.service.component.annotations.Deactivate;
35import org.osgi.service.component.annotations.Reference;
36import org.osgi.service.component.annotations.ReferenceCardinality;
tony-liuc3f9b652016-11-10 15:28:17 +080037import org.slf4j.Logger;
38
39import java.util.Collection;
40import java.util.stream.Collectors;
41
42import static org.slf4j.LoggerFactory.getLogger;
43
44/**
45 * Manages the TE tunnel attributes using an eventually consistent map.
46 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070047@Component(immediate = true, service = TeTunnelStore.class)
tony-liuc3f9b652016-11-10 15:28:17 +080048public class DistributedTeTunnelStore implements TeTunnelStore {
49
50 private final Logger log = getLogger(getClass());
51
Ray Milkeyd84f89b2018-08-17 14:54:17 -070052 @Reference(cardinality = ReferenceCardinality.MANDATORY)
tony-liuc3f9b652016-11-10 15:28:17 +080053 protected StorageService storageService;
54
Ray Milkeyd84f89b2018-08-17 14:54:17 -070055 @Reference(cardinality = ReferenceCardinality.MANDATORY)
tony-liuc3f9b652016-11-10 15:28:17 +080056 protected LogicalClockService clockService;
57
58 private EventuallyConsistentMap<TeTunnelKey, TeTunnel> teTunnels;
59 private EventuallyConsistentMap<TeTunnelKey, TunnelId> tunnelIds;
chenqinghui86320ae2016-12-01 15:40:39 +080060 private EventuallyConsistentMap<TeLspKey, TeLsp> lsps;
tony-liuc3f9b652016-11-10 15:28:17 +080061
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
chenqinghui86320ae2016-12-01 15:40:39 +080081 lsps = storageService.<TeLspKey, TeLsp>eventuallyConsistentMapBuilder()
82 .withName("TeLspStore")
83 .withSerializer(serializer)
84 .withTimestampProvider((k, v) -> clockService.getTimestamp())
85 .build();
86
tony-liuc3f9b652016-11-10 15:28:17 +080087 log.info("Started");
88 }
89
90 @Deactivate
91 public void deactivate() {
92 teTunnels.destroy();
93 tunnelIds.destroy();
chenqinghui86320ae2016-12-01 15:40:39 +080094 lsps.destroy();
tony-liuc3f9b652016-11-10 15:28:17 +080095
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 }
chenqinghui86320ae2016-12-01 15:40:39 +0800177
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-liuc3f9b652016-11-10 15:28:17 +0800215}