blob: 851dfd9699c747adca7c1547c08bb0a09018f769 [file] [log] [blame]
Marc De Leenheer1afa2a02015-05-13 09:18:07 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Marc De Leenheer1afa2a02015-05-13 09:18:07 -07003 *
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 */
Sho SHIMIZU739873b2016-02-23 17:02:12 -080016package org.onosproject.store.intent.impl;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070017
Sho SHIMIZUcfe707a2015-10-12 17:45:42 -070018import com.google.common.annotations.Beta;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070019import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
22import org.apache.felix.scr.annotations.Reference;
23import org.apache.felix.scr.annotations.ReferenceCardinality;
24import org.apache.felix.scr.annotations.Service;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070025import org.onosproject.net.device.DeviceService;
26import org.onosproject.net.intent.IntentId;
Sho SHIMIZU739873b2016-02-23 17:02:12 -080027import org.onosproject.net.intent.IntentSetMultimap;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070028import org.onosproject.store.serializers.KryoNamespaces;
29import org.onosproject.store.service.ConsistentMap;
30import org.onosproject.store.service.Serializer;
31import org.onosproject.store.service.StorageService;
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -070032import org.onosproject.store.service.Versioned;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070033import org.slf4j.Logger;
34
35import java.util.HashSet;
36import java.util.Set;
37
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070038import static org.slf4j.LoggerFactory.getLogger;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070039
40/**
Sho SHIMIZU7d20af12015-10-01 16:03:51 -070041 * A collection that maps Intent IDs as keys to values as Intent IDs,
42 * where each key may associated with multiple values without duplication.
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070043 */
Sho SHIMIZU5c396e32016-08-12 15:19:12 -070044@Component(immediate = true)
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070045@Service
Sho SHIMIZUcfe707a2015-10-12 17:45:42 -070046@Beta
Sho SHIMIZU7d20af12015-10-01 16:03:51 -070047public class ConsistentIntentSetMultimap implements IntentSetMultimap {
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070048 private final Logger log = getLogger(getClass());
49
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070050 private static final String INTENT_MAPPING = "IntentMapping";
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070051
Sho SHIMIZU200a7392015-07-23 16:28:35 -070052 private static final Serializer SERIALIZER = Serializer.using(KryoNamespaces.API);
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070053
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070054 private ConsistentMap<IntentId, Set<IntentId>> intentMapping;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070055
56 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
57 protected StorageService storageService;
58
59 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
60 protected DeviceService deviceService;
61
62 @Activate
63 public void activate() {
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070064 intentMapping = storageService.<IntentId, Set<IntentId>>consistentMapBuilder()
65 .withName(INTENT_MAPPING)
66 .withSerializer(SERIALIZER)
67 .build();
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070068 log.info("Started");
69 }
70
71 @Deactivate
72 public void deactivate() {
73 log.info("Stopped");
74 }
75
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070076 @Override
77 public Set<IntentId> getMapping(IntentId intentId) {
Marc De Leenheer723f5532015-06-03 20:16:17 -070078 Versioned<Set<IntentId>> result = intentMapping.get(intentId);
79
Marc De Leenheerc9733082015-06-04 12:22:38 -070080 if (result != null) {
Marc De Leenheer723f5532015-06-03 20:16:17 -070081 return result.value();
82 }
83
84 return null;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070085 }
86
87 @Override
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070088 public boolean allocateMapping(IntentId keyIntentId, IntentId valIntentId) {
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -070089 Versioned<Set<IntentId>> versionedIntents = intentMapping.get(keyIntentId);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070090
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -070091 if (versionedIntents == null) {
Marc De Leenheer723f5532015-06-03 20:16:17 -070092 Set<IntentId> newSet = new HashSet<>();
93 newSet.add(valIntentId);
94 intentMapping.put(keyIntentId, newSet);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070095 } else {
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -070096 versionedIntents.value().add(valIntentId);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070097 }
98
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070099 return true;
100 }
101
102 @Override
Marc De Leenheer9f7d1892015-05-30 13:22:24 -0700103 public void releaseMapping(IntentId intentId) {
104 for (IntentId intent : intentMapping.keySet()) {
105 // TODO: optimize by checking for identical src & dst
106 Set<IntentId> mapping = intentMapping.get(intent).value();
107 if (mapping.remove(intentId)) {
108 return;
109 }
110 }
111 }
112
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700113}