Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.store.resource.impl; |
| 17 | |
| 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.onlab.util.KryoNamespace; |
| 25 | import org.onosproject.net.DeviceId; |
| 26 | import org.onosproject.net.Port; |
| 27 | import org.onosproject.net.device.DeviceService; |
| 28 | import org.onosproject.net.intent.IntentId; |
Brian O'Connor | 6de2e20 | 2015-05-21 14:30:41 -0700 | [diff] [blame] | 29 | import org.onosproject.net.resource.device.DeviceResourceStore; |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 30 | import org.onosproject.store.serializers.KryoNamespaces; |
| 31 | import org.onosproject.store.service.ConsistentMap; |
| 32 | import org.onosproject.store.service.Serializer; |
| 33 | import org.onosproject.store.service.StorageService; |
| 34 | import org.onosproject.store.service.TransactionContext; |
| 35 | import org.onosproject.store.service.TransactionalMap; |
Marc De Leenheer | 4a1c1fa | 2015-06-01 18:08:56 -0700 | [diff] [blame] | 36 | import org.onosproject.store.service.Versioned; |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 37 | import org.slf4j.Logger; |
| 38 | |
Marc De Leenheer | 8c2caac | 2015-05-28 16:37:33 -0700 | [diff] [blame] | 39 | import java.util.Collections; |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 40 | import java.util.HashSet; |
| 41 | import java.util.Set; |
| 42 | |
| 43 | import static com.google.common.base.Preconditions.checkArgument; |
| 44 | import static org.slf4j.LoggerFactory.getLogger; |
| 45 | import static com.google.common.base.Preconditions.checkNotNull; |
| 46 | |
| 47 | /** |
| 48 | * Store that manages device resources using Copycat-backed TransactionalMaps. |
| 49 | */ |
| 50 | @Component(immediate = true, enabled = true) |
| 51 | @Service |
| 52 | public class ConsistentDeviceResourceStore implements DeviceResourceStore { |
| 53 | private final Logger log = getLogger(getClass()); |
| 54 | |
| 55 | private static final String PORT_ALLOCATIONS = "PortAllocations"; |
Marc De Leenheer | 8c2caac | 2015-05-28 16:37:33 -0700 | [diff] [blame] | 56 | private static final String INTENT_MAPPING = "IntentMapping"; |
Ayaka Koshibe | bcb0237 | 2015-06-01 10:56:42 -0700 | [diff] [blame] | 57 | private static final String INTENT_ALLOCATIONS = "PortIntentAllocations"; |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 58 | |
| 59 | private static final Serializer SERIALIZER = Serializer.using( |
| 60 | new KryoNamespace.Builder().register(KryoNamespaces.API).build()); |
| 61 | |
| 62 | private ConsistentMap<Port, IntentId> portAllocMap; |
| 63 | private ConsistentMap<IntentId, Set<Port>> intentAllocMap; |
Marc De Leenheer | 8c2caac | 2015-05-28 16:37:33 -0700 | [diff] [blame] | 64 | private ConsistentMap<IntentId, Set<IntentId>> intentMapping; |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 65 | |
| 66 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 67 | protected StorageService storageService; |
| 68 | |
| 69 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 70 | protected DeviceService deviceService; |
| 71 | |
| 72 | @Activate |
| 73 | public void activate() { |
| 74 | portAllocMap = storageService.<Port, IntentId>consistentMapBuilder() |
| 75 | .withName(PORT_ALLOCATIONS) |
| 76 | .withSerializer(SERIALIZER) |
| 77 | .build(); |
| 78 | intentAllocMap = storageService.<IntentId, Set<Port>>consistentMapBuilder() |
| 79 | .withName(INTENT_ALLOCATIONS) |
| 80 | .withSerializer(SERIALIZER) |
| 81 | .build(); |
Marc De Leenheer | 8c2caac | 2015-05-28 16:37:33 -0700 | [diff] [blame] | 82 | intentMapping = storageService.<IntentId, Set<IntentId>>consistentMapBuilder() |
| 83 | .withName(INTENT_MAPPING) |
| 84 | .withSerializer(SERIALIZER) |
| 85 | .build(); |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 86 | log.info("Started"); |
| 87 | } |
| 88 | |
| 89 | @Deactivate |
| 90 | public void deactivate() { |
| 91 | log.info("Stopped"); |
| 92 | } |
| 93 | |
| 94 | private TransactionalMap<Port, IntentId> getPortAllocs(TransactionContext tx) { |
| 95 | return tx.getTransactionalMap(PORT_ALLOCATIONS, SERIALIZER); |
| 96 | } |
| 97 | |
| 98 | private TransactionalMap<IntentId, Set<Port>> getIntentAllocs(TransactionContext tx) { |
| 99 | return tx.getTransactionalMap(INTENT_ALLOCATIONS, SERIALIZER); |
| 100 | } |
| 101 | |
| 102 | private TransactionContext getTxContext() { |
| 103 | return storageService.transactionContextBuilder().build(); |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public Set<Port> getFreePorts(DeviceId deviceId) { |
| 108 | checkNotNull(deviceId); |
| 109 | |
| 110 | Set<Port> freePorts = new HashSet<>(); |
| 111 | for (Port port : deviceService.getPorts(deviceId)) { |
| 112 | if (!portAllocMap.containsKey(port)) { |
| 113 | freePorts.add(port); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | return freePorts; |
| 118 | } |
| 119 | |
| 120 | @Override |
Marc De Leenheer | 8c2caac | 2015-05-28 16:37:33 -0700 | [diff] [blame] | 121 | public boolean allocatePorts(Set<Port> ports, IntentId intentId) { |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 122 | checkNotNull(ports); |
| 123 | checkArgument(ports.size() > 0); |
| 124 | checkNotNull(intentId); |
| 125 | |
| 126 | TransactionContext tx = getTxContext(); |
| 127 | tx.begin(); |
| 128 | try { |
| 129 | TransactionalMap<Port, IntentId> portAllocs = getPortAllocs(tx); |
| 130 | for (Port port : ports) { |
Marc De Leenheer | 8c2caac | 2015-05-28 16:37:33 -0700 | [diff] [blame] | 131 | if (portAllocs.putIfAbsent(port, intentId) != null) { |
| 132 | throw new Exception("Port already allocated " + port.toString()); |
| 133 | } |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 134 | } |
Marc De Leenheer | 8c2caac | 2015-05-28 16:37:33 -0700 | [diff] [blame] | 135 | |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 136 | TransactionalMap<IntentId, Set<Port>> intentAllocs = getIntentAllocs(tx); |
| 137 | intentAllocs.put(intentId, ports); |
| 138 | tx.commit(); |
| 139 | } catch (Exception e) { |
| 140 | log.error("Exception thrown, rolling back", e); |
| 141 | tx.abort(); |
Marc De Leenheer | 8c2caac | 2015-05-28 16:37:33 -0700 | [diff] [blame] | 142 | return false; |
| 143 | } |
| 144 | |
| 145 | return true; |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public Set<Port> getAllocations(IntentId intentId) { |
| 150 | if (!intentAllocMap.containsKey(intentId)) { |
| 151 | Collections.emptySet(); |
| 152 | } |
| 153 | |
| 154 | return intentAllocMap.get(intentId).value(); |
| 155 | } |
| 156 | |
| 157 | @Override |
| 158 | public IntentId getAllocations(Port port) { |
| 159 | if (!portAllocMap.containsKey(port)) { |
| 160 | return null; |
| 161 | } |
| 162 | |
| 163 | return portAllocMap.get(port).value(); |
| 164 | } |
| 165 | |
| 166 | @Override |
| 167 | public Set<IntentId> getMapping(IntentId intentId) { |
Marc De Leenheer | 723f553 | 2015-06-03 20:16:17 -0700 | [diff] [blame^] | 168 | Versioned<Set<IntentId>> result = intentMapping.get(intentId); |
| 169 | |
| 170 | if (result == null) { |
| 171 | return result.value(); |
| 172 | } |
| 173 | |
| 174 | return null; |
Marc De Leenheer | 8c2caac | 2015-05-28 16:37:33 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | @Override |
Marc De Leenheer | 8c2caac | 2015-05-28 16:37:33 -0700 | [diff] [blame] | 178 | public boolean allocateMapping(IntentId keyIntentId, IntentId valIntentId) { |
Marc De Leenheer | 4a1c1fa | 2015-06-01 18:08:56 -0700 | [diff] [blame] | 179 | Versioned<Set<IntentId>> versionedIntents = intentMapping.get(keyIntentId); |
Marc De Leenheer | 8c2caac | 2015-05-28 16:37:33 -0700 | [diff] [blame] | 180 | |
Marc De Leenheer | 4a1c1fa | 2015-06-01 18:08:56 -0700 | [diff] [blame] | 181 | |
| 182 | if (versionedIntents == null) { |
Marc De Leenheer | 723f553 | 2015-06-03 20:16:17 -0700 | [diff] [blame^] | 183 | Set<IntentId> newSet = new HashSet<>(); |
| 184 | newSet.add(valIntentId); |
| 185 | intentMapping.put(keyIntentId, newSet); |
Marc De Leenheer | 8c2caac | 2015-05-28 16:37:33 -0700 | [diff] [blame] | 186 | } else { |
Marc De Leenheer | 4a1c1fa | 2015-06-01 18:08:56 -0700 | [diff] [blame] | 187 | versionedIntents.value().add(valIntentId); |
Marc De Leenheer | 8c2caac | 2015-05-28 16:37:33 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Marc De Leenheer | 8c2caac | 2015-05-28 16:37:33 -0700 | [diff] [blame] | 190 | return true; |
| 191 | } |
| 192 | |
| 193 | @Override |
Marc De Leenheer | 9f7d189 | 2015-05-30 13:22:24 -0700 | [diff] [blame] | 194 | public void releaseMapping(IntentId intentId) { |
| 195 | for (IntentId intent : intentMapping.keySet()) { |
| 196 | // TODO: optimize by checking for identical src & dst |
| 197 | Set<IntentId> mapping = intentMapping.get(intent).value(); |
| 198 | if (mapping.remove(intentId)) { |
| 199 | return; |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | @Override |
Marc De Leenheer | 8c2caac | 2015-05-28 16:37:33 -0700 | [diff] [blame] | 205 | public boolean releasePorts(IntentId intentId) { |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 206 | checkNotNull(intentId); |
| 207 | |
| 208 | TransactionContext tx = getTxContext(); |
| 209 | tx.begin(); |
| 210 | try { |
| 211 | TransactionalMap<IntentId, Set<Port>> intentAllocs = getIntentAllocs(tx); |
| 212 | Set<Port> ports = intentAllocs.get(intentId); |
| 213 | intentAllocs.remove(intentId); |
| 214 | |
| 215 | TransactionalMap<Port, IntentId> portAllocs = getPortAllocs(tx); |
| 216 | for (Port port : ports) { |
| 217 | portAllocs.remove(port); |
| 218 | } |
| 219 | } catch (Exception e) { |
| 220 | log.error("Exception thrown, rolling back", e); |
| 221 | tx.abort(); |
Marc De Leenheer | 8c2caac | 2015-05-28 16:37:33 -0700 | [diff] [blame] | 222 | return false; |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 223 | } |
Marc De Leenheer | 8c2caac | 2015-05-28 16:37:33 -0700 | [diff] [blame] | 224 | |
| 225 | return true; |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 226 | } |
| 227 | } |