78c6468e9e414c032344196fc685b9f6c94af226
[onosfw.git] /
1 /*
2  * Copyright 2014-2015 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 package org.onosproject.incubator.store.tunnel.impl;
17
18 import static org.slf4j.LoggerFactory.getLogger;
19
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.Collections;
23 import java.util.HashSet;
24 import java.util.List;
25 import java.util.Objects;
26 import java.util.Set;
27
28 import org.apache.felix.scr.annotations.Activate;
29 import org.apache.felix.scr.annotations.Component;
30 import org.apache.felix.scr.annotations.Deactivate;
31 import org.apache.felix.scr.annotations.Reference;
32 import org.apache.felix.scr.annotations.ReferenceCardinality;
33 import org.apache.felix.scr.annotations.Service;
34 import org.onlab.util.KryoNamespace;
35 import org.onosproject.cluster.ClusterService;
36 import org.onosproject.core.ApplicationId;
37 import org.onosproject.core.CoreService;
38 import org.onosproject.core.IdGenerator;
39 import org.onosproject.incubator.net.tunnel.DefaultTunnel;
40 import org.onosproject.incubator.net.tunnel.Tunnel;
41 import org.onosproject.incubator.net.tunnel.Tunnel.Type;
42 import org.onosproject.incubator.net.tunnel.TunnelEndPoint;
43 import org.onosproject.incubator.net.tunnel.TunnelEvent;
44 import org.onosproject.incubator.net.tunnel.TunnelId;
45 import org.onosproject.incubator.net.tunnel.TunnelName;
46 import org.onosproject.incubator.net.tunnel.TunnelStore;
47 import org.onosproject.incubator.net.tunnel.TunnelStoreDelegate;
48 import org.onosproject.incubator.net.tunnel.TunnelSubscription;
49 import org.onosproject.net.Annotations;
50 import org.onosproject.net.DefaultAnnotations;
51 import org.onosproject.net.SparseAnnotations;
52 import org.onosproject.net.provider.ProviderId;
53 import org.onosproject.store.AbstractStore;
54 import org.onosproject.store.app.GossipApplicationStore.InternalState;
55 import org.onosproject.store.cluster.messaging.ClusterCommunicationService;
56 import org.onosproject.store.serializers.KryoNamespaces;
57 import org.onosproject.store.service.EventuallyConsistentMap;
58 import org.onosproject.store.service.MultiValuedTimestamp;
59 import org.onosproject.store.service.StorageService;
60 import org.onosproject.store.service.WallClockTimestamp;
61 import org.slf4j.Logger;
62
63 import com.google.common.base.MoreObjects;
64 import com.google.common.collect.ImmutableSet;
65
66 /**
67  * Manages inventory of tunnel in distributed data store that uses optimistic
68  * replication and gossip based techniques.
69  */
70 @Component(immediate = true)
71 @Service
72 public class DistributedTunnelStore
73         extends AbstractStore<TunnelEvent, TunnelStoreDelegate>
74         implements TunnelStore {
75
76     private final Logger log = getLogger(getClass());
77
78     /**
79      * The topic used for obtaining globally unique ids.
80      */
81     private String runnelOpTopoic = "tunnel-ops-ids";
82
83     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
84     protected ClusterCommunicationService clusterCommunicator;
85
86     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
87     protected ClusterService clusterService;
88
89     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
90     protected CoreService coreService;
91
92     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
93     protected StorageService storageService;
94
95     // tunnel identity as map key in the store.
96     private EventuallyConsistentMap<TunnelId, Tunnel> tunnelIdAsKeyStore;
97     // tunnel name as map key in the store.
98     private EventuallyConsistentMap<TunnelName, Set<TunnelId>> tunnelNameAsKeyStore;
99     // maintains all the tunnels between source and destination.
100     private EventuallyConsistentMap<TunnelKey, Set<TunnelId>> srcAndDstKeyStore;
101     // maintains all the tunnels by tunnel type.
102     private EventuallyConsistentMap<Tunnel.Type, Set<TunnelId>> typeKeyStore;
103     // maintains records that app subscribes tunnel.
104     private EventuallyConsistentMap<ApplicationId, Set<TunnelSubscription>> orderRelationship;
105
106     private IdGenerator idGenerator;
107
108     @Activate
109     public void activate() {
110         KryoNamespace.Builder serializer = KryoNamespace.newBuilder()
111                 .register(KryoNamespaces.API)
112                 .register(MultiValuedTimestamp.class)
113                 .register(InternalState.class);
114         tunnelIdAsKeyStore = storageService
115                 .<TunnelId, Tunnel>eventuallyConsistentMapBuilder()
116                 .withName("all_tunnel").withSerializer(serializer)
117                 .withTimestampProvider((k, v) -> new WallClockTimestamp()).build();
118         tunnelNameAsKeyStore = storageService
119                 .<TunnelName, Set<TunnelId>>eventuallyConsistentMapBuilder()
120                 .withName("tunnel_name_tunnel").withSerializer(serializer)
121                 .withTimestampProvider((k, v) -> new WallClockTimestamp()).build();
122         srcAndDstKeyStore = storageService
123                 .<TunnelKey, Set<TunnelId>>eventuallyConsistentMapBuilder()
124                 .withName("src_dst_tunnel").withSerializer(serializer)
125                 .withTimestampProvider((k, v) -> new WallClockTimestamp()).build();
126         typeKeyStore = storageService
127                 .<Tunnel.Type, Set<TunnelId>>eventuallyConsistentMapBuilder()
128                 .withName("type_tunnel").withSerializer(serializer)
129                 .withTimestampProvider((k, v) -> new WallClockTimestamp()).build();
130         orderRelationship = storageService
131                 .<ApplicationId, Set<TunnelSubscription>>eventuallyConsistentMapBuilder()
132                 .withName("type_tunnel").withSerializer(serializer)
133                 .withTimestampProvider((k, v) -> new WallClockTimestamp()).build();
134         idGenerator = coreService.getIdGenerator(runnelOpTopoic);
135         log.info("Started");
136     }
137
138     @Deactivate
139     public void deactivate() {
140         orderRelationship.destroy();
141         tunnelIdAsKeyStore.destroy();
142         srcAndDstKeyStore.destroy();
143         typeKeyStore.destroy();
144         tunnelNameAsKeyStore.destroy();
145         log.info("Stopped");
146     }
147
148     @Override
149     public TunnelId createOrUpdateTunnel(Tunnel tunnel) {
150         // tunnelIdAsKeyStore.
151         if (tunnel.tunnelId() != null && !"".equals(tunnel.tunnelId())) {
152             Tunnel old = tunnelIdAsKeyStore.get(tunnel.tunnelId());
153             if (old == null) {
154                 log.info("This tunnel[" + tunnel.tunnelId() + "] is not available.");
155                 return tunnel.tunnelId();
156             }
157             DefaultAnnotations oldAnno = (DefaultAnnotations) old.annotations();
158             SparseAnnotations newAnno = (SparseAnnotations) tunnel.annotations();
159             Tunnel newT = new DefaultTunnel(old.providerId(), old.src(),
160                                             old.dst(), old.type(),
161                                             old.state(), old.groupId(),
162                                             old.tunnelId(),
163                                             old.tunnelName(),
164                                             old.path(),
165                                             DefaultAnnotations.merge(oldAnno, newAnno));
166             tunnelIdAsKeyStore.put(tunnel.tunnelId(), newT);
167             TunnelEvent event = new TunnelEvent(
168                                                 TunnelEvent.Type.TUNNEL_UPDATED,
169                                                 tunnel);
170             notifyDelegate(event);
171             return tunnel.tunnelId();
172         } else {
173             TunnelId tunnelId = TunnelId.valueOf(idGenerator.getNewId());
174             Tunnel newT = new DefaultTunnel(tunnel.providerId(), tunnel.src(),
175                                             tunnel.dst(), tunnel.type(),
176                                             tunnel.state(), tunnel.groupId(),
177                                             tunnelId,
178                                             tunnel.tunnelName(),
179                                             tunnel.path(),
180                                             tunnel.annotations());
181             TunnelKey key = TunnelKey.tunnelKey(tunnel.src(), tunnel.dst());
182             tunnelIdAsKeyStore.put(tunnelId, newT);
183             Set<TunnelId> tunnelnameSet = tunnelNameAsKeyStore.get(tunnel
184                     .tunnelName());
185             if (tunnelnameSet == null) {
186                 tunnelnameSet = new HashSet<TunnelId>();
187             }
188             tunnelnameSet.add(tunnelId);
189             tunnelNameAsKeyStore.put(tunnel
190                     .tunnelName(), tunnelnameSet);
191             Set<TunnelId> srcAndDstKeySet = srcAndDstKeyStore.get(key);
192             if (srcAndDstKeySet == null) {
193                 srcAndDstKeySet = new HashSet<TunnelId>();
194             }
195             srcAndDstKeySet.add(tunnelId);
196             srcAndDstKeyStore.put(key, srcAndDstKeySet);
197             Set<TunnelId> typeKeySet = typeKeyStore.get(tunnel.type());
198             if (typeKeySet == null) {
199                 typeKeySet = new HashSet<TunnelId>();
200             }
201             typeKeySet.add(tunnelId);
202             typeKeyStore.put(tunnel.type(), typeKeySet);
203             TunnelEvent event = new TunnelEvent(TunnelEvent.Type.TUNNEL_ADDED,
204                                                 tunnel);
205             notifyDelegate(event);
206             return tunnelId;
207         }
208     }
209
210     @Override
211     public void deleteTunnel(TunnelId tunnelId) {
212         Tunnel deletedTunnel = tunnelIdAsKeyStore.get(tunnelId);
213         if (deletedTunnel == null) {
214             return;
215         }
216         tunnelNameAsKeyStore.get(deletedTunnel.tunnelName()).remove(tunnelId);
217         tunnelIdAsKeyStore.remove(tunnelId);
218         TunnelKey key = new TunnelKey(deletedTunnel.src(), deletedTunnel.dst());
219         srcAndDstKeyStore.get(key).remove(tunnelId);
220         typeKeyStore.get(deletedTunnel.type()).remove(tunnelId);
221         TunnelEvent event = new TunnelEvent(TunnelEvent.Type.TUNNEL_REMOVED,
222                                             deletedTunnel);
223         notifyDelegate(event);
224     }
225
226     @Override
227     public void deleteTunnel(TunnelEndPoint src, TunnelEndPoint dst,
228                              ProviderId producerName) {
229         TunnelKey key = TunnelKey.tunnelKey(src, dst);
230         Set<TunnelId> idSet = srcAndDstKeyStore.get(key);
231         if (idSet == null) {
232             return;
233         }
234         Tunnel deletedTunnel = null;
235         TunnelEvent event = null;
236         List<TunnelEvent> ls = new ArrayList<TunnelEvent>();
237         for (TunnelId id : idSet) {
238             deletedTunnel = tunnelIdAsKeyStore.get(id);
239             event = new TunnelEvent(TunnelEvent.Type.TUNNEL_REMOVED,
240                                     deletedTunnel);
241             ls.add(event);
242             if (producerName.equals(deletedTunnel.providerId())) {
243                 tunnelIdAsKeyStore.remove(deletedTunnel.tunnelId());
244                 tunnelNameAsKeyStore.get(deletedTunnel.tunnelName())
245                         .remove(deletedTunnel.tunnelId());
246                 srcAndDstKeyStore.get(key).remove(deletedTunnel.tunnelId());
247                 typeKeyStore.get(deletedTunnel.type())
248                         .remove(deletedTunnel.tunnelId());
249             }
250         }
251         notifyDelegate(ls);
252     }
253
254     @Override
255     public void deleteTunnel(TunnelEndPoint src, TunnelEndPoint dst, Type type,
256                              ProviderId producerName) {
257         TunnelKey key = TunnelKey.tunnelKey(src, dst);
258         Set<TunnelId> idSet = srcAndDstKeyStore.get(key);
259         if (idSet == null) {
260             return;
261         }
262         Tunnel deletedTunnel = null;
263         TunnelEvent event = null;
264         List<TunnelEvent> ls = new ArrayList<TunnelEvent>();
265         for (TunnelId id : idSet) {
266             deletedTunnel = tunnelIdAsKeyStore.get(id);
267             event = new TunnelEvent(TunnelEvent.Type.TUNNEL_REMOVED,
268                                     deletedTunnel);
269             ls.add(event);
270             if (producerName.equals(deletedTunnel.providerId())
271                     && type.equals(deletedTunnel.type())) {
272                 tunnelIdAsKeyStore.remove(deletedTunnel.tunnelId());
273                 tunnelNameAsKeyStore.get(deletedTunnel.tunnelName())
274                         .remove(deletedTunnel.tunnelId());
275                 srcAndDstKeyStore.get(key).remove(deletedTunnel.tunnelId());
276                 typeKeyStore.get(deletedTunnel.type())
277                         .remove(deletedTunnel.tunnelId());
278             }
279         }
280         notifyDelegate(ls);
281     }
282
283     @Override
284     public Tunnel borrowTunnel(ApplicationId appId, TunnelId tunnelId,
285                                Annotations... annotations) {
286         Set<TunnelSubscription> orderSet = orderRelationship.get(appId);
287         if (orderSet == null) {
288             orderSet = new HashSet<TunnelSubscription>();
289         }
290         TunnelSubscription order = new TunnelSubscription(appId, null, null, tunnelId, null, null,
291                                 annotations);
292         Tunnel result = tunnelIdAsKeyStore.get(tunnelId);
293         if (result != null || Tunnel.State.INACTIVE.equals(result.state())) {
294             return null;
295         }
296         orderSet.add(order);
297         orderRelationship.put(appId, orderSet);
298         return result;
299     }
300
301     @Override
302     public Collection<Tunnel> borrowTunnel(ApplicationId appId,
303                                            TunnelEndPoint src,
304                                            TunnelEndPoint dst,
305                                            Annotations... annotations) {
306         Set<TunnelSubscription> orderSet = orderRelationship.get(appId);
307         if (orderSet == null) {
308             orderSet = new HashSet<TunnelSubscription>();
309         }
310         TunnelSubscription order = new TunnelSubscription(appId, src, dst, null, null, null, annotations);
311         boolean isExist = orderSet.contains(order);
312         if (!isExist) {
313             orderSet.add(order);
314         }
315         orderRelationship.put(appId, orderSet);
316         TunnelKey key = TunnelKey.tunnelKey(src, dst);
317         Set<TunnelId> idSet = srcAndDstKeyStore.get(key);
318         if (idSet == null || idSet.size() == 0) {
319             return Collections.emptySet();
320         }
321         Collection<Tunnel> tunnelSet = new HashSet<Tunnel>();
322         for (TunnelId tunnelId : idSet) {
323             Tunnel result = tunnelIdAsKeyStore.get(tunnelId);
324             if (Tunnel.State.ACTIVE.equals(result.state())) {
325                 tunnelSet.add(result);
326             }
327         }
328         return tunnelSet;
329     }
330
331     @Override
332     public Collection<Tunnel> borrowTunnel(ApplicationId appId,
333                                            TunnelEndPoint src,
334                                            TunnelEndPoint dst, Type type,
335                                            Annotations... annotations) {
336         Set<TunnelSubscription> orderSet = orderRelationship.get(appId);
337         if (orderSet == null) {
338             orderSet = new HashSet<TunnelSubscription>();
339         }
340         TunnelSubscription order = new TunnelSubscription(appId, src, dst, null, type, null, annotations);
341         boolean isExist = orderSet.contains(order);
342         if (!isExist) {
343             orderSet.add(order);
344         }
345         orderRelationship.put(appId, orderSet);
346         TunnelKey key = TunnelKey.tunnelKey(src, dst);
347         Set<TunnelId> idSet = srcAndDstKeyStore.get(key);
348         if (idSet == null || idSet.size() == 0) {
349             return Collections.emptySet();
350         }
351         Collection<Tunnel> tunnelSet = new HashSet<Tunnel>();
352         for (TunnelId tunnelId : idSet) {
353             Tunnel result = tunnelIdAsKeyStore.get(tunnelId);
354             if (type.equals(result.type())
355                     && Tunnel.State.ACTIVE.equals(result.state())) {
356                 tunnelSet.add(result);
357             }
358         }
359         return tunnelSet;
360     }
361
362     @Override
363     public Collection<Tunnel> borrowTunnel(ApplicationId appId,
364                                            TunnelName tunnelName,
365                                            Annotations... annotations) {
366         Set<TunnelSubscription> orderSet = orderRelationship.get(appId);
367         if (orderSet == null) {
368             orderSet = new HashSet<TunnelSubscription>();
369         }
370         TunnelSubscription order = new TunnelSubscription(appId, null, null, null, null, tunnelName,
371                                 annotations);
372         boolean isExist = orderSet.contains(order);
373         if (!isExist) {
374             orderSet.add(order);
375         }
376         orderRelationship.put(appId, orderSet);
377         Set<TunnelId> idSet = tunnelNameAsKeyStore.get(tunnelName);
378         if (idSet == null || idSet.size() == 0) {
379             return Collections.emptySet();
380         }
381         Collection<Tunnel> tunnelSet = new HashSet<Tunnel>();
382         for (TunnelId tunnelId : idSet) {
383             Tunnel result = tunnelIdAsKeyStore.get(tunnelId);
384             if (Tunnel.State.ACTIVE.equals(result.state())) {
385                 tunnelSet.add(result);
386             }
387         }
388         return tunnelSet;
389     }
390
391     @Override
392     public boolean returnTunnel(ApplicationId appId, TunnelName tunnelName,
393                                 Annotations... annotations) {
394         TunnelSubscription order = new TunnelSubscription(appId, null, null, null, null, tunnelName,
395                                 annotations);
396         return deleteOrder(order);
397     }
398
399     @Override
400     public boolean returnTunnel(ApplicationId appId, TunnelId tunnelId,
401                                 Annotations... annotations) {
402         TunnelSubscription order = new TunnelSubscription(appId, null, null, tunnelId, null, null,
403                                 annotations);
404         return deleteOrder(order);
405     }
406
407     @Override
408     public boolean returnTunnel(ApplicationId appId, TunnelEndPoint src,
409                                 TunnelEndPoint dst, Type type,
410                                 Annotations... annotations) {
411         TunnelSubscription order = new TunnelSubscription(appId, src, dst, null, type, null, annotations);
412         return deleteOrder(order);
413     }
414
415     @Override
416     public boolean returnTunnel(ApplicationId appId, TunnelEndPoint src,
417                                 TunnelEndPoint dst, Annotations... annotations) {
418         TunnelSubscription order = new TunnelSubscription(appId, src, dst, null, null, null, annotations);
419         return deleteOrder(order);
420     }
421
422     private boolean deleteOrder(TunnelSubscription order) {
423         Set<TunnelSubscription> orderSet = orderRelationship.get(order.consumerId());
424         if (orderSet == null) {
425             return true;
426         }
427         if (orderSet.contains(order)) {
428             orderSet.remove(order);
429             return true;
430         }
431         return false;
432     }
433
434     @Override
435     public Tunnel queryTunnel(TunnelId tunnelId) {
436         return tunnelIdAsKeyStore.get(tunnelId);
437     }
438
439     @Override
440     public Collection<TunnelSubscription> queryTunnelSubscription(ApplicationId appId) {
441         return orderRelationship.get(appId) != null ? ImmutableSet.copyOf(orderRelationship
442                 .get(appId)) : Collections.emptySet();
443     }
444
445     @Override
446     public Collection<Tunnel> queryTunnel(Type type) {
447         Collection<Tunnel> result = new HashSet<Tunnel>();
448         Set<TunnelId> tunnelIds = typeKeyStore.get(type);
449         if (tunnelIds == null) {
450             return Collections.emptySet();
451         }
452         for (TunnelId id : tunnelIds) {
453             result.add(tunnelIdAsKeyStore.get(id));
454         }
455         return result.size() == 0 ? Collections.emptySet() : ImmutableSet
456                 .copyOf(result);
457     }
458
459     @Override
460     public Collection<Tunnel> queryTunnel(TunnelEndPoint src, TunnelEndPoint dst) {
461         Collection<Tunnel> result = new HashSet<Tunnel>();
462         TunnelKey key = TunnelKey.tunnelKey(src, dst);
463         Set<TunnelId> tunnelIds = srcAndDstKeyStore.get(key);
464         if (tunnelIds == null) {
465             return Collections.emptySet();
466         }
467         for (TunnelId id : tunnelIds) {
468             result.add(tunnelIdAsKeyStore.get(id));
469         }
470         return result.size() == 0 ? Collections.emptySet() : ImmutableSet
471                 .copyOf(result);
472     }
473
474     @Override
475     public Collection<Tunnel> queryAllTunnels() {
476         return tunnelIdAsKeyStore.values();
477     }
478
479     @Override
480     public int tunnelCount() {
481         return tunnelIdAsKeyStore.size();
482     }
483
484     /**
485      * Uses source TunnelPoint and destination TunnelPoint as map key.
486      */
487     private static final class TunnelKey {
488         private final TunnelEndPoint src;
489         private final TunnelEndPoint dst;
490
491         private TunnelKey(TunnelEndPoint src, TunnelEndPoint dst) {
492             this.src = src;
493             this.dst = dst;
494
495         }
496
497         /**
498          * create a map key.
499          *
500          * @param src
501          * @param dst
502          * @return a key using source ip and destination ip
503          */
504         static TunnelKey tunnelKey(TunnelEndPoint src, TunnelEndPoint dst) {
505             return new TunnelKey(src, dst);
506         }
507
508         @Override
509         public int hashCode() {
510             return Objects.hash(src, dst);
511         }
512
513         @Override
514         public boolean equals(Object obj) {
515             if (this == obj) {
516                 return true;
517             }
518             if (obj instanceof TunnelKey) {
519                 final TunnelKey other = (TunnelKey) obj;
520                 return Objects.equals(this.src, other.src)
521                         && Objects.equals(this.dst, other.dst);
522             }
523             return false;
524         }
525
526         @Override
527         public String toString() {
528             return MoreObjects.toStringHelper(getClass()).add("src", src)
529                     .add("dst", dst).toString();
530         }
531     }
532 }