blob: 3266e96c5b787ab78120054baac5e8c2f2719737 [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.DeviceId;
25import org.onosproject.net.Port;
26import org.onosproject.net.device.DeviceService;
27import org.onosproject.net.intent.IntentId;
Brian O'Connor6de2e202015-05-21 14:30:41 -070028import org.onosproject.net.resource.device.DeviceResourceStore;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070029import org.onosproject.store.serializers.KryoNamespaces;
30import org.onosproject.store.service.ConsistentMap;
31import org.onosproject.store.service.Serializer;
32import org.onosproject.store.service.StorageService;
33import org.onosproject.store.service.TransactionContext;
34import org.onosproject.store.service.TransactionalMap;
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -070035import org.onosproject.store.service.Versioned;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070036import org.slf4j.Logger;
37
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070038import java.util.Collections;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070039import java.util.HashSet;
40import java.util.Set;
41
42import static com.google.common.base.Preconditions.checkArgument;
43import static org.slf4j.LoggerFactory.getLogger;
44import static com.google.common.base.Preconditions.checkNotNull;
45
46/**
47 * Store that manages device resources using Copycat-backed TransactionalMaps.
48 */
49@Component(immediate = true, enabled = true)
50@Service
51public class ConsistentDeviceResourceStore implements DeviceResourceStore {
52 private final Logger log = getLogger(getClass());
53
54 private static final String PORT_ALLOCATIONS = "PortAllocations";
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070055 private static final String INTENT_MAPPING = "IntentMapping";
Ayaka Koshibebcb02372015-06-01 10:56:42 -070056 private static final String INTENT_ALLOCATIONS = "PortIntentAllocations";
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070057
Sho SHIMIZU200a7392015-07-23 16:28:35 -070058 private static final Serializer SERIALIZER = Serializer.using(KryoNamespaces.API);
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070059
60 private ConsistentMap<Port, IntentId> portAllocMap;
61 private ConsistentMap<IntentId, Set<Port>> intentAllocMap;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070062 private ConsistentMap<IntentId, Set<IntentId>> intentMapping;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070063
64 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
65 protected StorageService storageService;
66
67 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
68 protected DeviceService deviceService;
69
70 @Activate
71 public void activate() {
72 portAllocMap = storageService.<Port, IntentId>consistentMapBuilder()
73 .withName(PORT_ALLOCATIONS)
74 .withSerializer(SERIALIZER)
75 .build();
76 intentAllocMap = storageService.<IntentId, Set<Port>>consistentMapBuilder()
77 .withName(INTENT_ALLOCATIONS)
78 .withSerializer(SERIALIZER)
79 .build();
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070080 intentMapping = storageService.<IntentId, Set<IntentId>>consistentMapBuilder()
81 .withName(INTENT_MAPPING)
82 .withSerializer(SERIALIZER)
83 .build();
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070084 log.info("Started");
85 }
86
87 @Deactivate
88 public void deactivate() {
89 log.info("Stopped");
90 }
91
92 private TransactionalMap<Port, IntentId> getPortAllocs(TransactionContext tx) {
93 return tx.getTransactionalMap(PORT_ALLOCATIONS, SERIALIZER);
94 }
95
96 private TransactionalMap<IntentId, Set<Port>> getIntentAllocs(TransactionContext tx) {
97 return tx.getTransactionalMap(INTENT_ALLOCATIONS, SERIALIZER);
98 }
99
100 private TransactionContext getTxContext() {
101 return storageService.transactionContextBuilder().build();
102 }
103
104 @Override
105 public Set<Port> getFreePorts(DeviceId deviceId) {
106 checkNotNull(deviceId);
107
108 Set<Port> freePorts = new HashSet<>();
109 for (Port port : deviceService.getPorts(deviceId)) {
110 if (!portAllocMap.containsKey(port)) {
111 freePorts.add(port);
112 }
113 }
114
115 return freePorts;
116 }
117
118 @Override
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700119 public boolean allocatePorts(Set<Port> ports, IntentId intentId) {
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700120 checkNotNull(ports);
121 checkArgument(ports.size() > 0);
122 checkNotNull(intentId);
123
124 TransactionContext tx = getTxContext();
125 tx.begin();
126 try {
127 TransactionalMap<Port, IntentId> portAllocs = getPortAllocs(tx);
128 for (Port port : ports) {
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700129 if (portAllocs.putIfAbsent(port, intentId) != null) {
130 throw new Exception("Port already allocated " + port.toString());
131 }
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700132 }
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700133
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700134 TransactionalMap<IntentId, Set<Port>> intentAllocs = getIntentAllocs(tx);
135 intentAllocs.put(intentId, ports);
136 tx.commit();
137 } catch (Exception e) {
138 log.error("Exception thrown, rolling back", e);
139 tx.abort();
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700140 return false;
141 }
142
143 return true;
144 }
145
146 @Override
147 public Set<Port> getAllocations(IntentId intentId) {
148 if (!intentAllocMap.containsKey(intentId)) {
149 Collections.emptySet();
150 }
151
152 return intentAllocMap.get(intentId).value();
153 }
154
155 @Override
156 public IntentId getAllocations(Port port) {
157 if (!portAllocMap.containsKey(port)) {
158 return null;
159 }
160
161 return portAllocMap.get(port).value();
162 }
163
164 @Override
165 public Set<IntentId> getMapping(IntentId intentId) {
Marc De Leenheer723f5532015-06-03 20:16:17 -0700166 Versioned<Set<IntentId>> result = intentMapping.get(intentId);
167
Marc De Leenheerc9733082015-06-04 12:22:38 -0700168 if (result != null) {
Marc De Leenheer723f5532015-06-03 20:16:17 -0700169 return result.value();
170 }
171
172 return null;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700173 }
174
175 @Override
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700176 public boolean allocateMapping(IntentId keyIntentId, IntentId valIntentId) {
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700177 Versioned<Set<IntentId>> versionedIntents = intentMapping.get(keyIntentId);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700178
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700179 if (versionedIntents == null) {
Marc De Leenheer723f5532015-06-03 20:16:17 -0700180 Set<IntentId> newSet = new HashSet<>();
181 newSet.add(valIntentId);
182 intentMapping.put(keyIntentId, newSet);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700183 } else {
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700184 versionedIntents.value().add(valIntentId);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700185 }
186
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700187 return true;
188 }
189
190 @Override
Marc De Leenheer9f7d1892015-05-30 13:22:24 -0700191 public void releaseMapping(IntentId intentId) {
192 for (IntentId intent : intentMapping.keySet()) {
193 // TODO: optimize by checking for identical src & dst
194 Set<IntentId> mapping = intentMapping.get(intent).value();
195 if (mapping.remove(intentId)) {
196 return;
197 }
198 }
199 }
200
201 @Override
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700202 public boolean releasePorts(IntentId intentId) {
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700203 checkNotNull(intentId);
204
205 TransactionContext tx = getTxContext();
206 tx.begin();
207 try {
208 TransactionalMap<IntentId, Set<Port>> intentAllocs = getIntentAllocs(tx);
209 Set<Port> ports = intentAllocs.get(intentId);
210 intentAllocs.remove(intentId);
211
212 TransactionalMap<Port, IntentId> portAllocs = getPortAllocs(tx);
213 for (Port port : ports) {
214 portAllocs.remove(port);
215 }
Marc De Leenheerc9733082015-06-04 12:22:38 -0700216 tx.commit();
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700217 } catch (Exception e) {
218 log.error("Exception thrown, rolling back", e);
219 tx.abort();
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700220 return false;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700221 }
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700222
223 return true;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700224 }
225}