blob: 5e4ece38056e3bdb8fef5ec30c3515020cd16257 [file] [log] [blame]
Marc De Leenheer1afa2a02015-05-13 09:18:07 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
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.onosproject.net.device.DeviceService;
20import org.onosproject.net.intent.IntentId;
Sho SHIMIZU739873b2016-02-23 17:02:12 -080021import org.onosproject.net.intent.IntentSetMultimap;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070022import org.onosproject.store.serializers.KryoNamespaces;
23import org.onosproject.store.service.ConsistentMap;
24import org.onosproject.store.service.Serializer;
25import org.onosproject.store.service.StorageService;
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -070026import org.onosproject.store.service.Versioned;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070027import org.osgi.service.component.annotations.Activate;
28import org.osgi.service.component.annotations.Component;
29import org.osgi.service.component.annotations.Deactivate;
30import org.osgi.service.component.annotations.Reference;
31import org.osgi.service.component.annotations.ReferenceCardinality;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070032import org.slf4j.Logger;
33
34import java.util.HashSet;
35import java.util.Set;
36
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070037import static org.slf4j.LoggerFactory.getLogger;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070038
39/**
Sho SHIMIZU7d20af12015-10-01 16:03:51 -070040 * A collection that maps Intent IDs as keys to values as Intent IDs,
41 * where each key may associated with multiple values without duplication.
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070042 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070043@Component(immediate = true, service = IntentSetMultimap.class)
Sho SHIMIZUcfe707a2015-10-12 17:45:42 -070044@Beta
Sho SHIMIZU7d20af12015-10-01 16:03:51 -070045public class ConsistentIntentSetMultimap implements IntentSetMultimap {
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070046 private final Logger log = getLogger(getClass());
47
Jon Halldbe4c532016-10-04 11:45:52 -070048 private static final String INTENT_MAPPING = "onos-intent-mapping";
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070049
Sho SHIMIZU200a7392015-07-23 16:28:35 -070050 private static final Serializer SERIALIZER = Serializer.using(KryoNamespaces.API);
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070051
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070052 private ConsistentMap<IntentId, Set<IntentId>> intentMapping;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070053
Ray Milkeyd84f89b2018-08-17 14:54:17 -070054 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070055 protected StorageService storageService;
56
Ray Milkeyd84f89b2018-08-17 14:54:17 -070057 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070058 protected DeviceService deviceService;
59
60 @Activate
61 public void activate() {
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070062 intentMapping = storageService.<IntentId, Set<IntentId>>consistentMapBuilder()
63 .withName(INTENT_MAPPING)
64 .withSerializer(SERIALIZER)
65 .build();
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070066 log.info("Started");
67 }
68
69 @Deactivate
70 public void deactivate() {
71 log.info("Stopped");
72 }
73
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070074 @Override
75 public Set<IntentId> getMapping(IntentId intentId) {
Marc De Leenheer723f5532015-06-03 20:16:17 -070076 Versioned<Set<IntentId>> result = intentMapping.get(intentId);
77
Marc De Leenheerc9733082015-06-04 12:22:38 -070078 if (result != null) {
Marc De Leenheer723f5532015-06-03 20:16:17 -070079 return result.value();
80 }
81
82 return null;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070083 }
84
85 @Override
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070086 public boolean allocateMapping(IntentId keyIntentId, IntentId valIntentId) {
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -070087 Versioned<Set<IntentId>> versionedIntents = intentMapping.get(keyIntentId);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070088
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -070089 if (versionedIntents == null) {
Marc De Leenheer723f5532015-06-03 20:16:17 -070090 Set<IntentId> newSet = new HashSet<>();
91 newSet.add(valIntentId);
92 intentMapping.put(keyIntentId, newSet);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070093 } else {
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -070094 versionedIntents.value().add(valIntentId);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070095 }
96
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070097 return true;
98 }
99
100 @Override
Marc De Leenheer9f7d1892015-05-30 13:22:24 -0700101 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
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700111}