87e672150e5900058ef0bb935ad542603530a8d7
[onosfw.git] /
1 /*
2  * Copyright 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.store.resource.impl;
17
18 import org.apache.felix.scr.annotations.Activate;
19 import org.apache.felix.scr.annotations.Component;
20 import org.apache.felix.scr.annotations.Deactivate;
21 import org.apache.felix.scr.annotations.Reference;
22 import org.apache.felix.scr.annotations.ReferenceCardinality;
23 import org.apache.felix.scr.annotations.Service;
24 import org.onosproject.net.device.DeviceService;
25 import org.onosproject.net.intent.IntentId;
26 import org.onosproject.net.resource.device.IntentSetMultimap;
27 import org.onosproject.store.serializers.KryoNamespaces;
28 import org.onosproject.store.service.ConsistentMap;
29 import org.onosproject.store.service.Serializer;
30 import org.onosproject.store.service.StorageService;
31 import org.onosproject.store.service.Versioned;
32 import org.slf4j.Logger;
33
34 import java.util.HashSet;
35 import java.util.Set;
36
37 import static org.slf4j.LoggerFactory.getLogger;
38
39 /**
40  * A collection that maps Intent IDs as keys to values as Intent IDs,
41  * where each key may associated with multiple values without duplication.
42  */
43 @Component(immediate = true, enabled = true)
44 @Service
45 public class ConsistentIntentSetMultimap implements IntentSetMultimap {
46     private final Logger log = getLogger(getClass());
47
48     private static final String INTENT_MAPPING = "IntentMapping";
49
50     private static final Serializer SERIALIZER = Serializer.using(KryoNamespaces.API);
51
52     private ConsistentMap<IntentId, Set<IntentId>> intentMapping;
53
54     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
55     protected StorageService storageService;
56
57     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
58     protected DeviceService deviceService;
59
60     @Activate
61     public void activate() {
62         intentMapping = storageService.<IntentId, Set<IntentId>>consistentMapBuilder()
63                 .withName(INTENT_MAPPING)
64                 .withSerializer(SERIALIZER)
65                 .build();
66         log.info("Started");
67     }
68
69     @Deactivate
70     public void deactivate() {
71         log.info("Stopped");
72     }
73
74     @Override
75     public Set<IntentId> getMapping(IntentId intentId) {
76         Versioned<Set<IntentId>> result = intentMapping.get(intentId);
77
78         if (result != null) {
79             return result.value();
80         }
81
82         return null;
83     }
84
85     @Override
86     public boolean allocateMapping(IntentId keyIntentId, IntentId valIntentId) {
87         Versioned<Set<IntentId>> versionedIntents = intentMapping.get(keyIntentId);
88
89         if (versionedIntents == null) {
90             Set<IntentId> newSet = new HashSet<>();
91             newSet.add(valIntentId);
92             intentMapping.put(keyIntentId, newSet);
93         } else {
94             versionedIntents.value().add(valIntentId);
95         }
96
97         return true;
98     }
99
100     @Override
101     public void releaseMapping(IntentId intentId) {
102         for (IntentId intent : intentMapping.keySet()) {
103             // TODO: optimize by checking for identical src & dst
104             Set<IntentId> mapping = intentMapping.get(intent).value();
105             if (mapping.remove(intentId)) {
106                 return;
107             }
108         }
109     }
110
111 }