2 * Copyright 2015 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onosproject.store.resource.impl;
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;
34 import java.util.HashSet;
37 import static org.slf4j.LoggerFactory.getLogger;
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.
43 @Component(immediate = true, enabled = true)
45 public class ConsistentIntentSetMultimap implements IntentSetMultimap {
46 private final Logger log = getLogger(getClass());
48 private static final String INTENT_MAPPING = "IntentMapping";
50 private static final Serializer SERIALIZER = Serializer.using(KryoNamespaces.API);
52 private ConsistentMap<IntentId, Set<IntentId>> intentMapping;
54 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
55 protected StorageService storageService;
57 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
58 protected DeviceService deviceService;
61 public void activate() {
62 intentMapping = storageService.<IntentId, Set<IntentId>>consistentMapBuilder()
63 .withName(INTENT_MAPPING)
64 .withSerializer(SERIALIZER)
70 public void deactivate() {
75 public Set<IntentId> getMapping(IntentId intentId) {
76 Versioned<Set<IntentId>> result = intentMapping.get(intentId);
79 return result.value();
86 public boolean allocateMapping(IntentId keyIntentId, IntentId valIntentId) {
87 Versioned<Set<IntentId>> versionedIntents = intentMapping.get(keyIntentId);
89 if (versionedIntents == null) {
90 Set<IntentId> newSet = new HashSet<>();
91 newSet.add(valIntentId);
92 intentMapping.put(keyIntentId, newSet);
94 versionedIntents.value().add(valIntentId);
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)) {