blob: 282400752e91137de8c441903d0bd2ade3a1e896 [file] [log] [blame]
tony-liuc3f9b652016-11-10 15:28:17 +08001/*
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
17package org.onosproject.tetunnel.impl;
18
19import com.google.common.collect.ImmutableList;
20import org.apache.felix.scr.annotations.Activate;
21import org.apache.felix.scr.annotations.Component;
22import org.apache.felix.scr.annotations.Deactivate;
23import org.apache.felix.scr.annotations.Reference;
24import org.apache.felix.scr.annotations.ReferenceCardinality;
25import org.apache.felix.scr.annotations.Service;
26import org.onlab.util.KryoNamespace;
27import org.onosproject.incubator.net.tunnel.TunnelId;
28import org.onosproject.store.serializers.KryoNamespaces;
29import org.onosproject.store.service.EventuallyConsistentMap;
30import org.onosproject.store.service.LogicalClockService;
31import org.onosproject.store.service.StorageService;
32import org.onosproject.tetopology.management.api.TeTopologyKey;
33import org.onosproject.tetunnel.api.tunnel.TeTunnel;
34import org.onosproject.tetunnel.api.TeTunnelStore;
35import org.onosproject.tetunnel.api.tunnel.TeTunnelKey;
36import org.slf4j.Logger;
37
38import java.util.Collection;
39import java.util.stream.Collectors;
40
41import 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
48public 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}