Rewrote optical connectivity intent compiler
Change-Id: I5acece3c14bed8a23f7bbe0c5a9bc0932a2a0881
diff --git a/core/store/dist/src/main/java/org/onosproject/store/resource/impl/ConsistentDeviceResourceStore.java b/core/store/dist/src/main/java/org/onosproject/store/resource/impl/ConsistentDeviceResourceStore.java
new file mode 100644
index 0000000..b5e1655
--- /dev/null
+++ b/core/store/dist/src/main/java/org/onosproject/store/resource/impl/ConsistentDeviceResourceStore.java
@@ -0,0 +1,156 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.store.resource.impl;
+
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.ReferenceCardinality;
+import org.apache.felix.scr.annotations.Service;
+import org.onlab.util.KryoNamespace;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.Port;
+import org.onosproject.net.device.DeviceService;
+import org.onosproject.net.intent.IntentId;
+import org.onosproject.net.resource.DeviceResourceStore;
+import org.onosproject.store.serializers.KryoNamespaces;
+import org.onosproject.store.service.ConsistentMap;
+import org.onosproject.store.service.Serializer;
+import org.onosproject.store.service.StorageService;
+import org.onosproject.store.service.TransactionContext;
+import org.onosproject.store.service.TransactionalMap;
+import org.slf4j.Logger;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static org.slf4j.LoggerFactory.getLogger;
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * Store that manages device resources using Copycat-backed TransactionalMaps.
+ */
+@Component(immediate = true, enabled = true)
+@Service
+public class ConsistentDeviceResourceStore implements DeviceResourceStore {
+ private final Logger log = getLogger(getClass());
+
+ private static final String PORT_ALLOCATIONS = "PortAllocations";
+ private static final String INTENT_ALLOCATIONS = "IntentAllocations";
+
+ private static final Serializer SERIALIZER = Serializer.using(
+ new KryoNamespace.Builder().register(KryoNamespaces.API).build());
+
+ private ConsistentMap<Port, IntentId> portAllocMap;
+ private ConsistentMap<IntentId, Set<Port>> intentAllocMap;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected StorageService storageService;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected DeviceService deviceService;
+
+ @Activate
+ public void activate() {
+ portAllocMap = storageService.<Port, IntentId>consistentMapBuilder()
+ .withName(PORT_ALLOCATIONS)
+ .withSerializer(SERIALIZER)
+ .build();
+ intentAllocMap = storageService.<IntentId, Set<Port>>consistentMapBuilder()
+ .withName(INTENT_ALLOCATIONS)
+ .withSerializer(SERIALIZER)
+ .build();
+ log.info("Started");
+ }
+
+ @Deactivate
+ public void deactivate() {
+ log.info("Stopped");
+ }
+
+ private TransactionalMap<Port, IntentId> getPortAllocs(TransactionContext tx) {
+ return tx.getTransactionalMap(PORT_ALLOCATIONS, SERIALIZER);
+ }
+
+ private TransactionalMap<IntentId, Set<Port>> getIntentAllocs(TransactionContext tx) {
+ return tx.getTransactionalMap(INTENT_ALLOCATIONS, SERIALIZER);
+ }
+
+ private TransactionContext getTxContext() {
+ return storageService.transactionContextBuilder().build();
+ }
+
+ @Override
+ public Set<Port> getFreePorts(DeviceId deviceId) {
+ checkNotNull(deviceId);
+
+ Set<Port> freePorts = new HashSet<>();
+ for (Port port : deviceService.getPorts(deviceId)) {
+ if (!portAllocMap.containsKey(port)) {
+ freePorts.add(port);
+ }
+ }
+
+ return freePorts;
+ }
+
+ @Override
+ public void allocatePorts(Set<Port> ports, IntentId intentId) {
+ checkNotNull(ports);
+ checkArgument(ports.size() > 0);
+ checkNotNull(intentId);
+
+ TransactionContext tx = getTxContext();
+ tx.begin();
+ try {
+ TransactionalMap<Port, IntentId> portAllocs = getPortAllocs(tx);
+ for (Port port : ports) {
+ portAllocs.put(port, intentId);
+ }
+ TransactionalMap<IntentId, Set<Port>> intentAllocs = getIntentAllocs(tx);
+ intentAllocs.put(intentId, ports);
+ tx.commit();
+ } catch (Exception e) {
+ log.error("Exception thrown, rolling back", e);
+ tx.abort();
+ throw e;
+ }
+ }
+
+ @Override
+ public void releasePorts(IntentId intentId) {
+ checkNotNull(intentId);
+
+ TransactionContext tx = getTxContext();
+ tx.begin();
+ try {
+ TransactionalMap<IntentId, Set<Port>> intentAllocs = getIntentAllocs(tx);
+ Set<Port> ports = intentAllocs.get(intentId);
+ intentAllocs.remove(intentId);
+
+ TransactionalMap<Port, IntentId> portAllocs = getPortAllocs(tx);
+ for (Port port : ports) {
+ portAllocs.remove(port);
+ }
+ } catch (Exception e) {
+ log.error("Exception thrown, rolling back", e);
+ tx.abort();
+ throw e;
+ }
+ }
+}
diff --git a/core/store/dist/src/main/java/org/onosproject/store/resource/impl/ConsistentLinkResourceStore.java b/core/store/dist/src/main/java/org/onosproject/store/resource/impl/ConsistentLinkResourceStore.java
index 6b22b75..454d04c 100644
--- a/core/store/dist/src/main/java/org/onosproject/store/resource/impl/ConsistentLinkResourceStore.java
+++ b/core/store/dist/src/main/java/org/onosproject/store/resource/impl/ConsistentLinkResourceStore.java
@@ -17,6 +17,8 @@
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Deactivate;
import org.onlab.util.Bandwidth;
+import org.onosproject.net.OmsPort;
+import org.onosproject.net.device.DeviceService;
import org.slf4j.Logger;
import org.onlab.util.KryoNamespace;
import org.onlab.util.PositionalParameterStringFormatter;
@@ -55,7 +57,6 @@
import static com.google.common.base.Preconditions.checkState;
import static org.slf4j.LoggerFactory.getLogger;
import static org.onosproject.net.AnnotationKeys.BANDWIDTH;
-import static org.onosproject.net.AnnotationKeys.OPTICAL_WAVES;
/**
* Store that manages link resources using Copycat-backed TransactionalMaps.
@@ -78,7 +79,7 @@
// table to store current allocations
/** LinkKey -> List<LinkResourceAllocations>. */
- private static final String LINK_RESOURCE_ALLOCATIONS = "LinkResourceAllocations";
+ private static final String LINK_RESOURCE_ALLOCATIONS = "o";
/** IntentId -> LinkResourceAllocations. */
private static final String INTENT_ALLOCATIONS = "IntentAllocations";
@@ -95,6 +96,9 @@
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected LinkService linkService;
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected DeviceService deviceService;
+
@Activate
public void activate() {
intentAllocMap = storageService.<IntentId, LinkResourceAllocations>consistentMapBuilder()
@@ -136,14 +140,14 @@
private Set<LambdaResourceAllocation> getLambdaResourceCapacity(Link link) {
Set<LambdaResourceAllocation> allocations = new HashSet<>();
- try {
- final int waves = Integer.parseInt(link.annotations().value(OPTICAL_WAVES));
- for (int i = 1; i <= waves; i++) {
- allocations.add(new LambdaResourceAllocation(LambdaResource.valueOf(i)));
- }
- } catch (NumberFormatException e) {
- log.debug("No {} annotation on link {}", OPTICAL_WAVES, link);
+
+ OmsPort port = (OmsPort) deviceService.getPort(link.src().deviceId(), link.src().port());
+
+ // Assume fixed grid for now
+ for (int i = 0; i < port.totalChannels(); i++) {
+ allocations.add(new LambdaResourceAllocation(LambdaResource.valueOf(i)));
}
+
return allocations;
}