blob: dcc942c90426fc162b270f9773252056675475f3 [file] [log] [blame]
Marc De Leenheer1afa2a02015-05-13 09:18:07 -07001/*
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 */
16package org.onosproject.store.resource.impl;
17
18import org.apache.felix.scr.annotations.Activate;
19import org.apache.felix.scr.annotations.Component;
20import org.apache.felix.scr.annotations.Deactivate;
21import org.apache.felix.scr.annotations.Reference;
22import org.apache.felix.scr.annotations.ReferenceCardinality;
23import org.apache.felix.scr.annotations.Service;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070024import org.onosproject.net.Port;
25import org.onosproject.net.device.DeviceService;
26import org.onosproject.net.intent.IntentId;
Brian O'Connor6de2e202015-05-21 14:30:41 -070027import org.onosproject.net.resource.device.DeviceResourceStore;
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/**
41 * Store that manages device resources using Copycat-backed TransactionalMaps.
42 */
43@Component(immediate = true, enabled = true)
44@Service
45public class ConsistentDeviceResourceStore implements DeviceResourceStore {
46 private final Logger log = getLogger(getClass());
47
48 private static final String PORT_ALLOCATIONS = "PortAllocations";
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070049 private static final String INTENT_MAPPING = "IntentMapping";
Ayaka Koshibebcb02372015-06-01 10:56:42 -070050 private static final String INTENT_ALLOCATIONS = "PortIntentAllocations";
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
54 private ConsistentMap<Port, IntentId> portAllocMap;
55 private ConsistentMap<IntentId, Set<Port>> intentAllocMap;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070056 private ConsistentMap<IntentId, Set<IntentId>> intentMapping;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070057
58 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
59 protected StorageService storageService;
60
61 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
62 protected DeviceService deviceService;
63
64 @Activate
65 public void activate() {
66 portAllocMap = storageService.<Port, IntentId>consistentMapBuilder()
67 .withName(PORT_ALLOCATIONS)
68 .withSerializer(SERIALIZER)
69 .build();
70 intentAllocMap = storageService.<IntentId, Set<Port>>consistentMapBuilder()
71 .withName(INTENT_ALLOCATIONS)
72 .withSerializer(SERIALIZER)
73 .build();
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070074 intentMapping = storageService.<IntentId, Set<IntentId>>consistentMapBuilder()
75 .withName(INTENT_MAPPING)
76 .withSerializer(SERIALIZER)
77 .build();
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070078 log.info("Started");
79 }
80
81 @Deactivate
82 public void deactivate() {
83 log.info("Stopped");
84 }
85
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070086 @Override
87 public Set<IntentId> getMapping(IntentId intentId) {
Marc De Leenheer723f5532015-06-03 20:16:17 -070088 Versioned<Set<IntentId>> result = intentMapping.get(intentId);
89
Marc De Leenheerc9733082015-06-04 12:22:38 -070090 if (result != null) {
Marc De Leenheer723f5532015-06-03 20:16:17 -070091 return result.value();
92 }
93
94 return null;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070095 }
96
97 @Override
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070098 public boolean allocateMapping(IntentId keyIntentId, IntentId valIntentId) {
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -070099 Versioned<Set<IntentId>> versionedIntents = intentMapping.get(keyIntentId);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700100
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700101 if (versionedIntents == null) {
Marc De Leenheer723f5532015-06-03 20:16:17 -0700102 Set<IntentId> newSet = new HashSet<>();
103 newSet.add(valIntentId);
104 intentMapping.put(keyIntentId, newSet);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700105 } else {
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700106 versionedIntents.value().add(valIntentId);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700107 }
108
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700109 return true;
110 }
111
112 @Override
Marc De Leenheer9f7d1892015-05-30 13:22:24 -0700113 public void releaseMapping(IntentId intentId) {
114 for (IntentId intent : intentMapping.keySet()) {
115 // TODO: optimize by checking for identical src & dst
116 Set<IntentId> mapping = intentMapping.get(intent).value();
117 if (mapping.remove(intentId)) {
118 return;
119 }
120 }
121 }
122
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700123}