Remove LinkResourceStore and its implementations

They were deprecated in Emu

Change-Id: I4272c7788bcd74ad7f272392a67b07bde6b09df4
diff --git a/core/api/src/main/java/org/onosproject/net/resource/link/LinkResourceStore.java b/core/api/src/main/java/org/onosproject/net/resource/link/LinkResourceStore.java
deleted file mode 100644
index e8a295c..0000000
--- a/core/api/src/main/java/org/onosproject/net/resource/link/LinkResourceStore.java
+++ /dev/null
@@ -1,76 +0,0 @@
-/*
- * Copyright 2014 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.net.resource.link;
-
-import java.util.Set;
-
-import org.onosproject.net.Link;
-import org.onosproject.net.intent.IntentId;
-import org.onosproject.net.resource.ResourceAllocation;
-
-/**
- * Manages link resources.
- *
- * @deprecated in Emu Release.
- */
-@Deprecated
-public interface LinkResourceStore {
-    /**
-     * Returns free resources for given link.
-     *
-     * @param link a target link
-     * @return free resources for given link
-     */
-    Set<ResourceAllocation> getFreeResources(Link link);
-
-    /**
-     * Allocates resources.
-     *
-     * @param allocations resources to be allocated
-     */
-    void allocateResources(LinkResourceAllocations allocations);
-
-    /**
-     * Releases resources.
-     *
-     * @param allocations resources to be released
-     * @return the link resource event
-     */
-    LinkResourceEvent releaseResources(LinkResourceAllocations allocations);
-
-    /**
-     * Returns resources allocated for an Intent.
-     *
-     * @param intentId the target Intent's ID
-     * @return allocated resources or null if no resource is allocated
-     */
-    LinkResourceAllocations getAllocations(IntentId intentId);
-
-    /**
-     * Returns resources allocated for a link.
-     *
-     * @param link the target link
-     * @return allocated resources
-     */
-    Iterable<LinkResourceAllocations> getAllocations(Link link);
-
-    /**
-     * Returns all allocated resources.
-     *
-     * @return allocated resources
-     */
-    Iterable<LinkResourceAllocations> getAllocations();
-}
diff --git a/core/common/src/test/java/org/onosproject/store/trivial/SimpleLinkResourceStore.java b/core/common/src/test/java/org/onosproject/store/trivial/SimpleLinkResourceStore.java
deleted file mode 100644
index 22df937..0000000
--- a/core/common/src/test/java/org/onosproject/store/trivial/SimpleLinkResourceStore.java
+++ /dev/null
@@ -1,289 +0,0 @@
-/*
- * Copyright 2014-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.trivial;
-
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-
-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.Service;
-import org.onlab.util.Bandwidth;
-import org.onlab.util.PositionalParameterStringFormatter;
-import org.onosproject.net.AnnotationKeys;
-import org.onosproject.net.Annotations;
-import org.onosproject.net.Link;
-import org.onosproject.net.intent.IntentId;
-import org.onosproject.net.resource.link.BandwidthResource;
-import org.onosproject.net.resource.link.BandwidthResourceAllocation;
-import org.onosproject.net.resource.link.LambdaResource;
-import org.onosproject.net.resource.link.LambdaResourceAllocation;
-import org.onosproject.net.resource.link.LinkResourceAllocations;
-import org.onosproject.net.resource.link.LinkResourceEvent;
-import org.onosproject.net.resource.link.LinkResourceStore;
-import org.onosproject.net.resource.ResourceAllocation;
-import org.onosproject.net.resource.ResourceAllocationException;
-import org.onosproject.net.resource.ResourceType;
-import org.slf4j.Logger;
-
-import com.google.common.collect.ImmutableList;
-
-import static com.google.common.base.Preconditions.checkNotNull;
-import static com.google.common.base.Preconditions.checkState;
-import static org.slf4j.LoggerFactory.getLogger;
-
-/**
- * Manages link resources using trivial in-memory structures implementation.
- *
- * @deprecated in Emu Release
- */
-@Deprecated
-@Component(immediate = true)
-@Service
-public class SimpleLinkResourceStore implements LinkResourceStore {
-    private static final BandwidthResource DEFAULT_BANDWIDTH = new BandwidthResource(Bandwidth.mbps(1_000));
-    private final Logger log = getLogger(getClass());
-
-    private Map<IntentId, LinkResourceAllocations> linkResourceAllocationsMap;
-    private Map<Link, Set<LinkResourceAllocations>> allocatedResources;
-    private Map<Link, Set<ResourceAllocation>> freeResources;
-
-    @Activate
-    public void activate() {
-        linkResourceAllocationsMap = new HashMap<>();
-        allocatedResources = new HashMap<>();
-        freeResources = new HashMap<>();
-
-        log.info("Started");
-    }
-
-    @Deactivate
-    public void deactivate() {
-        log.info("Stopped");
-    }
-
-    /**
-     * Returns free resources for a given link obtaining from topology
-     * information.
-     *
-     * @param link the target link
-     * @return free resources
-     */
-    private synchronized Set<ResourceAllocation> readOriginalFreeResources(Link link) {
-        Annotations annotations = link.annotations();
-        Set<ResourceAllocation> allocations = new HashSet<>();
-
-        try {
-            int waves = Integer.parseInt(annotations.value(AnnotationKeys.OPTICAL_WAVES));
-            for (int i = 1; i <= waves; i++) {
-                allocations.add(new LambdaResourceAllocation(LambdaResource.valueOf(i)));
-            }
-        } catch (NumberFormatException e) {
-            log.debug("No optical.wave annotation on link %s", link);
-        }
-
-        BandwidthResource bandwidth = DEFAULT_BANDWIDTH;
-        try {
-            bandwidth = new BandwidthResource(
-                    Bandwidth.mbps((Double.parseDouble(annotations.value(AnnotationKeys.BANDWIDTH)))));
-        } catch (NumberFormatException e) {
-            log.debug("No bandwidth annotation on link %s", link);
-        }
-        allocations.add(
-                new BandwidthResourceAllocation(bandwidth));
-        return allocations;
-    }
-
-    /**
-     * Finds and returns {@link BandwidthResourceAllocation} object from a given
-     * set.
-     *
-     * @param freeRes a set of ResourceAllocation object.
-     * @return {@link BandwidthResourceAllocation} object if found, otherwise
-     *         {@link BandwidthResourceAllocation} object with 0 bandwidth
-     *
-     */
-    private synchronized BandwidthResourceAllocation getBandwidth(
-            Set<ResourceAllocation> freeRes) {
-        for (ResourceAllocation res : freeRes) {
-            if (res.type() == ResourceType.BANDWIDTH) {
-                return (BandwidthResourceAllocation) res;
-            }
-        }
-        return new BandwidthResourceAllocation(new BandwidthResource(Bandwidth.bps(0)));
-    }
-
-    /**
-     * Subtracts given resources from free resources for given link.
-     *
-     * @param link the target link
-     * @param allocations the resources to be subtracted
-     */
-    private synchronized void subtractFreeResources(Link link,
-            LinkResourceAllocations allocations) {
-        // TODO Use lock or version for updating freeResources.
-        checkNotNull(link);
-        Set<ResourceAllocation> freeRes = new HashSet<>(getFreeResources(link));
-        Set<ResourceAllocation> subRes = allocations.getResourceAllocation(link);
-        for (ResourceAllocation res : subRes) {
-            switch (res.type()) {
-            case BANDWIDTH:
-                BandwidthResourceAllocation ba = getBandwidth(freeRes);
-                double requestedBandwidth =
-                        ((BandwidthResourceAllocation) res).bandwidth().toDouble();
-                double newBandwidth = ba.bandwidth().toDouble() - requestedBandwidth;
-                if (newBandwidth < 0.0) {
-                    throw new ResourceAllocationException(
-                            PositionalParameterStringFormatter.format(
-                            "Unable to allocate bandwidth for link {} "
-                            + "requested amount is {} current allocation is {}",
-                                    link,
-                                    requestedBandwidth,
-                                    ba));
-                }
-                freeRes.remove(ba);
-                freeRes.add(new BandwidthResourceAllocation(
-                        new BandwidthResource(Bandwidth.bps(newBandwidth))));
-                break;
-            case LAMBDA:
-                final boolean lambdaAvailable = freeRes.remove(res);
-                if (!lambdaAvailable) {
-                    int requestedLambda =
-                            ((LambdaResourceAllocation) res).lambda().toInt();
-                    throw new ResourceAllocationException(
-                            PositionalParameterStringFormatter.format(
-                                    "Unable to allocate lambda for link {} lambda is {}",
-                                    link,
-                                    requestedLambda));
-                }
-                break;
-            default:
-                break;
-            }
-        }
-        freeResources.put(link, freeRes);
-
-    }
-
-    /**
-     * Adds given resources to free resources for given link.
-     *
-     * @param link the target link
-     * @param allocations the resources to be added
-     */
-    private synchronized void addFreeResources(Link link,
-            LinkResourceAllocations allocations) {
-        // TODO Use lock or version for updating freeResources.
-        Set<ResourceAllocation> freeRes = new HashSet<>(getFreeResources(link));
-        Set<ResourceAllocation> addRes = allocations.getResourceAllocation(link);
-        for (ResourceAllocation res : addRes) {
-            switch (res.type()) {
-            case BANDWIDTH:
-                BandwidthResourceAllocation ba = getBandwidth(freeRes);
-                double requestedBandwidth =
-                        ((BandwidthResourceAllocation) res).bandwidth().toDouble();
-                double newBandwidth = ba.bandwidth().toDouble() + requestedBandwidth;
-                freeRes.remove(ba);
-                freeRes.add(new BandwidthResourceAllocation(
-                        new BandwidthResource(Bandwidth.bps(newBandwidth))));
-                break;
-            case LAMBDA:
-                checkState(freeRes.add(res));
-                break;
-            default:
-                break;
-            }
-        }
-        freeResources.put(link, freeRes);
-    }
-
-    @Override
-    public synchronized Set<ResourceAllocation> getFreeResources(Link link) {
-        checkNotNull(link);
-        Set<ResourceAllocation> freeRes = freeResources.get(link);
-        if (freeRes == null) {
-            freeRes = readOriginalFreeResources(link);
-        }
-
-        return freeRes;
-    }
-
-    @Override
-    public synchronized void allocateResources(LinkResourceAllocations allocations) {
-        checkNotNull(allocations);
-        linkResourceAllocationsMap.put(allocations.intentId(), allocations);
-        for (Link link : allocations.links()) {
-            subtractFreeResources(link, allocations);
-            Set<LinkResourceAllocations> linkAllocs = allocatedResources.get(link);
-            if (linkAllocs == null) {
-                linkAllocs = new HashSet<>();
-            }
-            linkAllocs.add(allocations);
-            allocatedResources.put(link, linkAllocs);
-        }
-    }
-
-    @Override
-    public synchronized LinkResourceEvent releaseResources(LinkResourceAllocations allocations) {
-        checkNotNull(allocations);
-        linkResourceAllocationsMap.remove(allocations.intentId());
-        for (Link link : allocations.links()) {
-            addFreeResources(link, allocations);
-            Set<LinkResourceAllocations> linkAllocs = allocatedResources.get(link);
-            if (linkAllocs == null) {
-                log.error("Missing resource allocation.");
-            } else {
-                linkAllocs.remove(allocations);
-            }
-            allocatedResources.put(link, linkAllocs);
-        }
-
-        final List<LinkResourceAllocations> releasedResources =
-                ImmutableList.of(allocations);
-
-        return new LinkResourceEvent(
-                LinkResourceEvent.Type.ADDITIONAL_RESOURCES_AVAILABLE,
-                releasedResources);
-    }
-
-    @Override
-    public synchronized LinkResourceAllocations getAllocations(IntentId intentId) {
-        checkNotNull(intentId);
-        return linkResourceAllocationsMap.get(intentId);
-    }
-
-    @Override
-    public synchronized Iterable<LinkResourceAllocations> getAllocations(Link link) {
-        checkNotNull(link);
-        Set<LinkResourceAllocations> result = allocatedResources.get(link);
-        if (result == null) {
-            result = Collections.emptySet();
-        }
-        return Collections.unmodifiableSet(result);
-    }
-
-    @Override
-    public synchronized Iterable<LinkResourceAllocations> getAllocations() {
-        return Collections.unmodifiableCollection(linkResourceAllocationsMap.values());
-    }
-
-
-}
diff --git a/core/common/src/test/java/org/onosproject/store/trivial/SimpleLinkResourceStoreTest.java b/core/common/src/test/java/org/onosproject/store/trivial/SimpleLinkResourceStoreTest.java
deleted file mode 100644
index 6f6d853..0000000
--- a/core/common/src/test/java/org/onosproject/store/trivial/SimpleLinkResourceStoreTest.java
+++ /dev/null
@@ -1,309 +0,0 @@
-/*
- * Copyright 2014-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.trivial;
-
-import java.util.Collection;
-import java.util.HashSet;
-import java.util.Set;
-
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Test;
-import org.onlab.util.Bandwidth;
-import org.onosproject.net.AnnotationKeys;
-import org.onosproject.net.Annotations;
-import org.onosproject.net.ConnectPoint;
-import org.onosproject.net.DefaultAnnotations;
-import org.onosproject.net.DefaultLink;
-import org.onosproject.net.Link;
-import org.onosproject.net.intent.IntentId;
-import org.onosproject.net.provider.ProviderId;
-import org.onosproject.net.resource.link.BandwidthResource;
-import org.onosproject.net.resource.link.BandwidthResourceAllocation;
-import org.onosproject.net.resource.link.LambdaResource;
-import org.onosproject.net.resource.link.LambdaResourceAllocation;
-import org.onosproject.net.resource.link.LinkResourceAllocations;
-import org.onosproject.net.resource.link.LinkResourceStore;
-import org.onosproject.net.resource.ResourceAllocation;
-import org.onosproject.net.resource.ResourceAllocationException;
-import org.onosproject.net.resource.ResourceRequest;
-import org.onosproject.net.resource.ResourceType;
-
-import com.google.common.collect.ImmutableSet;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNotNull;
-import static org.onosproject.net.DeviceId.deviceId;
-import static org.onosproject.net.Link.Type.DIRECT;
-import static org.onosproject.net.PortNumber.portNumber;
-
-/**
- * Test of the simple LinkResourceStore implementation.
- */
-public class SimpleLinkResourceStoreTest {
-
-    private LinkResourceStore store;
-    private SimpleLinkResourceStore simpleStore;
-    private Link link1;
-    private Link link2;
-    private Link link3;
-
-    /**
-     * Returns {@link Link} object.
-     *
-     * @param dev1 source device
-     * @param port1 source port
-     * @param dev2 destination device
-     * @param port2 destination port
-     * @return created {@link Link} object
-     */
-    private static Link newLink(String dev1, int port1, String dev2, int port2) {
-        Annotations annotations = DefaultAnnotations.builder()
-                .set(AnnotationKeys.OPTICAL_WAVES, "80")
-                .set(AnnotationKeys.BANDWIDTH, "1000")
-                .build();
-        return DefaultLink.builder()
-                .providerId(new ProviderId("of", "foo"))
-                .src(new ConnectPoint(deviceId(dev1), portNumber(port1)))
-                .dst(new ConnectPoint(deviceId(dev2), portNumber(port2)))
-                .type(DIRECT)
-                .annotations(annotations)
-                .build();
-    }
-
-    @Before
-    public void setUp() throws Exception {
-        simpleStore = new SimpleLinkResourceStore();
-        simpleStore.activate();
-        store = simpleStore;
-
-        link1 = newLink("of:1", 1, "of:2", 2);
-        link2 = newLink("of:2", 1, "of:3", 2);
-        link3 = newLink("of:3", 1, "of:4", 2);
-    }
-
-    @After
-    public void tearDown() throws Exception {
-        simpleStore.deactivate();
-    }
-
-    /**
-     * Tests constructor and activate method.
-     */
-    @Test
-    public void testConstructorAndActivate() {
-        final Iterable<LinkResourceAllocations> allAllocations = store.getAllocations();
-        assertNotNull(allAllocations);
-        assertFalse(allAllocations.iterator().hasNext());
-
-        final Iterable<LinkResourceAllocations> linkAllocations =
-                store.getAllocations(link1);
-        assertNotNull(linkAllocations);
-        assertFalse(linkAllocations.iterator().hasNext());
-
-        final Set<ResourceAllocation> res = store.getFreeResources(link2);
-        assertNotNull(res);
-    }
-
-    /**
-     * Picks up and returns one of bandwidth allocations from a given set.
-     *
-     * @param resources the set of {@link ResourceAllocation}s
-     * @return {@link BandwidthResourceAllocation} object if found, null
-     *         otherwise
-     */
-    private BandwidthResourceAllocation getBandwidthObj(Set<ResourceAllocation> resources) {
-        for (ResourceAllocation res : resources) {
-            if (res.type() == ResourceType.BANDWIDTH) {
-                return ((BandwidthResourceAllocation) res);
-            }
-        }
-        return null;
-    }
-
-    /**
-     * Returns all lambda allocations from a given set.
-     *
-     * @param resources the set of {@link ResourceAllocation}s
-     * @return a set of {@link LambdaResourceAllocation} objects
-     */
-    private Set<LambdaResourceAllocation> getLambdaObjs(Set<ResourceAllocation> resources) {
-        Set<LambdaResourceAllocation> lambdaResources = new HashSet<>();
-        for (ResourceAllocation res : resources) {
-            if (res.type() == ResourceType.LAMBDA) {
-                lambdaResources.add((LambdaResourceAllocation) res);
-            }
-        }
-        return lambdaResources;
-    }
-
-    /**
-     * Tests initial free bandwidth for a link.
-     */
-    @Test
-    public void testInitialBandwidth() {
-        final Set<ResourceAllocation> freeRes = store.getFreeResources(link1);
-        assertNotNull(freeRes);
-
-        final BandwidthResourceAllocation alloc = getBandwidthObj(freeRes);
-        assertNotNull(alloc);
-
-        assertEquals(new BandwidthResource(Bandwidth.mbps(1000.0)), alloc.bandwidth());
-    }
-
-    /**
-     * Tests initial free lambda for a link.
-     */
-    @Test
-    public void testInitialLambdas() {
-        final Set<ResourceAllocation> freeRes = store.getFreeResources(link3);
-        assertNotNull(freeRes);
-
-        final Set<LambdaResourceAllocation> res = getLambdaObjs(freeRes);
-        assertNotNull(res);
-        assertEquals(80, res.size());
-    }
-
-    public static class MockLinkResourceBandwidthAllocations implements LinkResourceAllocations {
-        final double allocationAmount;
-
-        MockLinkResourceBandwidthAllocations(Double allocationAmount) {
-            this.allocationAmount = allocationAmount;
-        }
-        @Override
-        public Set<ResourceAllocation> getResourceAllocation(Link link) {
-            final ResourceAllocation allocation =
-                    new BandwidthResourceAllocation(new BandwidthResource(Bandwidth.bps(allocationAmount)));
-            final Set<ResourceAllocation> allocations = new HashSet<>();
-            allocations.add(allocation);
-            return allocations;
-        }
-
-        @Override
-        public IntentId intentId() {
-            return null;
-        }
-
-        @Override
-        public Collection<Link> links() {
-            return ImmutableSet.of(newLink("of:1", 1, "of:2", 2));
-        }
-
-        @Override
-        public Set<ResourceRequest> resources() {
-            return null;
-        }
-
-        @Override
-        public ResourceType type() {
-            return null;
-        }
-    }
-
-    public static class MockLinkResourceLambdaAllocations implements LinkResourceAllocations {
-        final int allocatedLambda;
-
-        MockLinkResourceLambdaAllocations(int allocatedLambda) {
-            this.allocatedLambda = allocatedLambda;
-        }
-        @Override
-        public Set<ResourceAllocation> getResourceAllocation(Link link) {
-            final ResourceAllocation allocation =
-                    new LambdaResourceAllocation(LambdaResource.valueOf(allocatedLambda));
-            final Set<ResourceAllocation> allocations = new HashSet<>();
-            allocations.add(allocation);
-            return allocations;
-        }
-
-        @Override
-        public IntentId intentId() {
-            return null;
-        }
-
-        @Override
-        public Collection<Link> links() {
-            return ImmutableSet.of(newLink("of:1", 1, "of:2", 2));
-        }
-
-        @Override
-        public Set<ResourceRequest> resources() {
-            return null;
-        }
-
-        @Override
-        public ResourceType type() {
-            return null;
-        }
-    }
-
-    /**
-     * Tests a successful bandwidth allocation.
-     */
-    @Test
-    public void testSuccessfulBandwidthAllocation() {
-        final LinkResourceAllocations allocations =
-                new MockLinkResourceBandwidthAllocations(900.0);
-        store.allocateResources(allocations);
-    }
-
-    /**
-     * Tests an unsuccessful bandwidth allocation.
-     */
-    @Test
-    public void testUnsuccessfulBandwidthAllocation() {
-        final LinkResourceAllocations allocations =
-                new MockLinkResourceBandwidthAllocations(2000000000.0);
-        boolean gotException = false;
-        try {
-            store.allocateResources(allocations);
-        } catch (ResourceAllocationException rae) {
-            assertEquals(true, rae.getMessage().contains("Unable to allocate bandwidth for link"));
-            gotException = true;
-        }
-        assertEquals(true, gotException);
-    }
-
-    /**
-     * Tests a successful lambda allocation.
-     */
-    @Test
-    public void testSuccessfulLambdaAllocation() {
-        final LinkResourceAllocations allocations =
-                new MockLinkResourceLambdaAllocations(1);
-        store.allocateResources(allocations);
-    }
-
-    /**
-     * Tests an unsuccessful lambda allocation.
-     */
-    @Test
-    public void testUnsuccessfulLambdaAllocation() {
-        final LinkResourceAllocations allocations =
-                new MockLinkResourceLambdaAllocations(1);
-        store.allocateResources(allocations);
-
-        boolean gotException = false;
-
-        try {
-            store.allocateResources(allocations);
-        } catch (ResourceAllocationException rae) {
-            assertEquals(true, rae.getMessage().contains("Unable to allocate lambda for link"));
-            gotException = true;
-        }
-        assertEquals(true, gotException);
-    }
-}
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
deleted file mode 100644
index 351c7a5..0000000
--- a/core/store/dist/src/main/java/org/onosproject/store/resource/impl/ConsistentLinkResourceStore.java
+++ /dev/null
@@ -1,485 +0,0 @@
-/*
- * 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 java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-import java.util.stream.Collectors;
-
-import org.apache.felix.scr.annotations.Component;
-import org.apache.felix.scr.annotations.Reference;
-import org.apache.felix.scr.annotations.ReferenceCardinality;
-import org.apache.felix.scr.annotations.Service;
-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.PositionalParameterStringFormatter;
-import org.onosproject.net.Link;
-import org.onosproject.net.LinkKey;
-import org.onosproject.net.Port;
-import org.onosproject.net.intent.IntentId;
-import org.onosproject.net.resource.link.BandwidthResource;
-import org.onosproject.net.resource.link.BandwidthResourceAllocation;
-import org.onosproject.net.resource.link.LambdaResource;
-import org.onosproject.net.resource.link.LambdaResourceAllocation;
-import org.onosproject.net.resource.link.LinkResourceAllocations;
-import org.onosproject.net.resource.link.LinkResourceEvent;
-import org.onosproject.net.resource.link.LinkResourceStore;
-import org.onosproject.net.resource.link.LinkResourceStoreDelegate;
-import org.onosproject.net.resource.link.MplsLabel;
-import org.onosproject.net.resource.link.MplsLabelResourceAllocation;
-import org.onosproject.net.resource.ResourceAllocation;
-import org.onosproject.net.resource.ResourceAllocationException;
-import org.onosproject.net.resource.ResourceType;
-import org.onosproject.store.AbstractStore;
-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.onosproject.store.service.Versioned;
-
-import com.google.common.collect.ImmutableList;
-import com.google.common.collect.ImmutableSet;
-import com.google.common.collect.Sets;
-
-import static com.google.common.base.Preconditions.checkNotNull;
-import static org.slf4j.LoggerFactory.getLogger;
-import static org.onosproject.net.AnnotationKeys.BANDWIDTH;
-
-/**
- * Store that manages link resources using Copycat-backed TransactionalMaps.
- *
- * @deprecated in Emu Release
- */
-@Deprecated
-@Component(immediate = true, enabled = true)
-@Service
-public class ConsistentLinkResourceStore extends
-        AbstractStore<LinkResourceEvent, LinkResourceStoreDelegate> implements
-        LinkResourceStore {
-
-    private final Logger log = getLogger(getClass());
-
-    private static final BandwidthResource DEFAULT_BANDWIDTH = new BandwidthResource(Bandwidth.mbps(1_000));
-    private static final BandwidthResource EMPTY_BW = new BandwidthResource(Bandwidth.bps(0));
-
-    // Smallest non-reserved MPLS label
-    private static final int MIN_UNRESERVED_LABEL = 0x10;
-    // Max non-reserved MPLS label = 239
-    private static final int MAX_UNRESERVED_LABEL = 0xEF;
-
-    // table to store current allocations
-    /** LinkKey -> List<LinkResourceAllocations>. */
-    private static final String LINK_RESOURCE_ALLOCATIONS = "LinkAllocations";
-
-    /** IntentId -> LinkResourceAllocations. */
-    private static final String INTENT_ALLOCATIONS = "LinkIntentAllocations";
-
-    private static final Serializer SERIALIZER = Serializer.using(KryoNamespaces.API);
-
-    // for reading committed values.
-    private ConsistentMap<IntentId, LinkResourceAllocations> intentAllocMap;
-
-    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
-    protected StorageService storageService;
-
-    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
-    protected DeviceService deviceService;
-
-    @Activate
-    public void activate() {
-        intentAllocMap = storageService.<IntentId, LinkResourceAllocations>consistentMapBuilder()
-                .withName(INTENT_ALLOCATIONS)
-                .withSerializer(SERIALIZER)
-                .build();
-        log.info("Started");
-    }
-
-    @Deactivate
-    public void deactivate() {
-        log.info("Stopped");
-    }
-
-    private TransactionalMap<IntentId, LinkResourceAllocations> getIntentAllocs(TransactionContext tx) {
-        return tx.getTransactionalMap(INTENT_ALLOCATIONS, SERIALIZER);
-    }
-
-    private TransactionalMap<LinkKey, List<LinkResourceAllocations>> getLinkAllocs(TransactionContext tx) {
-        return tx.getTransactionalMap(LINK_RESOURCE_ALLOCATIONS, SERIALIZER);
-    }
-
-    private TransactionContext getTxContext() {
-        return storageService.transactionContextBuilder().build();
-    }
-
-    private Set<ResourceAllocation> getResourceCapacity(ResourceType type, Link link) {
-        switch (type) {
-            case BANDWIDTH:
-                return ImmutableSet.of(getBandwidthResourceCapacity(link));
-            case LAMBDA:
-                return getLambdaResourceCapacity(link);
-            case MPLS_LABEL:
-                return getMplsResourceCapacity();
-            default:
-                return ImmutableSet.of();
-        }
-    }
-
-    private Set<ResourceAllocation> getLambdaResourceCapacity(Link link) {
-        Port port = deviceService.getPort(link.src().deviceId(), link.src().port());
-        if (!(port instanceof OmsPort)) {
-            return Collections.emptySet();
-        }
-
-        OmsPort omsPort = (OmsPort) port;
-        Set<ResourceAllocation> allocations = new HashSet<>();
-        // Assume fixed grid for now
-        for (int i = 0; i < omsPort.totalChannels(); i++) {
-            allocations.add(new LambdaResourceAllocation(LambdaResource.valueOf(i)));
-        }
-        return allocations;
-    }
-
-    private BandwidthResourceAllocation getBandwidthResourceCapacity(Link link) {
-
-        // if Link annotation exist, use them
-        // if all fails, use DEFAULT_BANDWIDTH
-        BandwidthResource bandwidth = DEFAULT_BANDWIDTH;
-        String strBw = link.annotations().value(BANDWIDTH);
-        if (strBw == null) {
-            return new BandwidthResourceAllocation(bandwidth);
-        }
-
-        try {
-            bandwidth = new BandwidthResource(Bandwidth.mbps(Double.parseDouble(strBw)));
-        } catch (NumberFormatException e) {
-            // do nothings, use default bandwidth
-            bandwidth = DEFAULT_BANDWIDTH;
-        }
-        return new BandwidthResourceAllocation(bandwidth);
-    }
-
-    private Set<ResourceAllocation> getMplsResourceCapacity() {
-        Set<ResourceAllocation> allocations = new HashSet<>();
-        //Ignoring reserved labels of 0 through 15
-        for (int i = MIN_UNRESERVED_LABEL; i <= MAX_UNRESERVED_LABEL; i++) {
-            allocations.add(new MplsLabelResourceAllocation(MplsLabel
-                    .valueOf(i)));
-
-        }
-        return allocations;
-    }
-
-    private Map<ResourceType, Set<ResourceAllocation>> getResourceCapacity(Link link) {
-        Map<ResourceType, Set<ResourceAllocation>> caps = new HashMap<>();
-        for (ResourceType type : ResourceType.values()) {
-            Set<ResourceAllocation> cap = getResourceCapacity(type, link);
-            caps.put(type, cap);
-        }
-        return caps;
-    }
-
-    @Override
-    public Set<ResourceAllocation> getFreeResources(Link link) {
-        TransactionContext tx = getTxContext();
-
-        tx.begin();
-        try {
-            Map<ResourceType, Set<ResourceAllocation>> freeResources = getFreeResourcesEx(tx, link);
-            return freeResources.values().stream()
-                    .flatMap(Collection::stream)
-                    .collect(Collectors.toSet());
-        } finally {
-            tx.abort();
-        }
-    }
-
-    private Map<ResourceType, Set<ResourceAllocation>> getFreeResourcesEx(TransactionContext tx, Link link) {
-        checkNotNull(tx);
-        checkNotNull(link);
-
-        Map<ResourceType, Set<ResourceAllocation>> free = new HashMap<>();
-        final Map<ResourceType, Set<ResourceAllocation>> caps = getResourceCapacity(link);
-        final List<LinkResourceAllocations> allocations = ImmutableList.copyOf(getAllocations(tx, link));
-
-        Set<ResourceAllocation> bw = caps.get(ResourceType.BANDWIDTH);
-        Set<ResourceAllocation> value = getFreeBandwidthResources(link, bw, allocations);
-        free.put(ResourceType.BANDWIDTH, value);
-
-        Set<ResourceAllocation> lmd = caps.get(ResourceType.LAMBDA);
-        Set<ResourceAllocation> freeL = getFreeResources(link, lmd, allocations,
-                LambdaResourceAllocation.class);
-        free.put(ResourceType.LAMBDA, freeL);
-
-        Set<ResourceAllocation> mpls = caps.get(ResourceType.MPLS_LABEL);
-        Set<ResourceAllocation> freeLabel = getFreeResources(link, mpls, allocations,
-                MplsLabelResourceAllocation.class);
-        free.put(ResourceType.MPLS_LABEL, freeLabel);
-
-        return free;
-    }
-
-    private Set<ResourceAllocation> getFreeBandwidthResources(Link link, Set<ResourceAllocation> bw,
-                                                              List<LinkResourceAllocations> allocations) {
-        if (bw == null || bw.isEmpty()) {
-            bw = Sets.newHashSet(new BandwidthResourceAllocation(EMPTY_BW));
-        }
-
-        BandwidthResourceAllocation cap = (BandwidthResourceAllocation) bw.iterator().next();
-        double freeBw = cap.bandwidth().toDouble();
-
-        // enumerate current allocations, subtracting resources
-        double allocatedBw = allocations.stream()
-                .flatMap(x -> x.getResourceAllocation(link).stream())
-                .filter(x -> x instanceof BandwidthResourceAllocation)
-                .map(x -> (BandwidthResourceAllocation) x)
-                .mapToDouble(x -> x.bandwidth().toDouble())
-                .sum();
-        freeBw -= allocatedBw;
-        return Sets.newHashSet(
-                new BandwidthResourceAllocation(new BandwidthResource(Bandwidth.bps(freeBw))));
-    }
-
-    private Set<ResourceAllocation> getFreeResources(Link link,
-                                                     Set<ResourceAllocation> resources,
-                                                     List<LinkResourceAllocations> allocations,
-                                                     Class<? extends ResourceAllocation> cls) {
-        if (resources == null || resources.isEmpty()) {
-            // nothing left
-            return Collections.emptySet();
-        }
-        Set<ResourceAllocation> freeL = resources.stream()
-                .filter(cls::isInstance)
-                .collect(Collectors.toSet());
-
-        // enumerate current allocations, removing resources
-        List<ResourceAllocation> allocated = allocations.stream()
-                .flatMap(x -> x.getResourceAllocation(link).stream())
-                .filter(cls::isInstance)
-                .collect(Collectors.toList());
-        freeL.removeAll(allocated);
-        return freeL;
-    }
-
-    @Override
-    public void allocateResources(LinkResourceAllocations allocations) {
-        checkNotNull(allocations);
-        TransactionContext tx = getTxContext();
-
-        tx.begin();
-        try {
-            TransactionalMap<IntentId, LinkResourceAllocations> intentAllocs = getIntentAllocs(tx);
-            intentAllocs.put(allocations.intentId(), allocations);
-            allocations.links().forEach(link -> allocateLinkResource(tx, link, allocations));
-            tx.commit();
-        } catch (ResourceAllocationException e) {
-            log.error("Exception thrown, rolling back", e);
-            tx.abort();
-        } catch (Exception e) {
-            log.error("Exception thrown, rolling back", e);
-            tx.abort();
-            throw e;
-        }
-    }
-
-    private void allocateLinkResource(TransactionContext tx, Link link,
-            LinkResourceAllocations allocations) {
-        // requested resources
-        Set<ResourceAllocation> reqs = allocations.getResourceAllocation(link);
-        Map<ResourceType, Set<ResourceAllocation>> available = getFreeResourcesEx(tx, link);
-        for (ResourceAllocation req : reqs) {
-            Set<ResourceAllocation> avail = available.get(req.type());
-            if (req instanceof BandwidthResourceAllocation) {
-                // check if allocation should be accepted
-                if (avail.isEmpty()) {
-                    throw new ResourceAllocationException(String.format("There's no Bandwidth resource on %s?", link));
-                }
-                BandwidthResourceAllocation bw = (BandwidthResourceAllocation) avail.iterator().next();
-                double bwLeft = bw.bandwidth().toDouble();
-                BandwidthResourceAllocation bwReq = ((BandwidthResourceAllocation) req);
-                bwLeft -= bwReq.bandwidth().toDouble();
-                if (bwLeft < 0) {
-                    throw new ResourceAllocationException(
-                            PositionalParameterStringFormatter.format(
-                                    "Unable to allocate bandwidth for link {} "
-                                        + " requested amount is {} current allocation is {}",
-                                    link,
-                                    bwReq.bandwidth().toDouble(),
-                                    bw));
-                }
-            } else if (req instanceof LambdaResourceAllocation) {
-                LambdaResourceAllocation lambdaAllocation = (LambdaResourceAllocation) req;
-                // check if allocation should be accepted
-                if (!avail.contains(req)) {
-                    // requested lambda was not available
-                    throw new ResourceAllocationException(
-                            PositionalParameterStringFormatter.format(
-                                "Unable to allocate lambda for link {} lambda is {}",
-                                    link,
-                                    lambdaAllocation.lambda().toInt()));
-                }
-            } else if (req instanceof MplsLabelResourceAllocation) {
-                MplsLabelResourceAllocation mplsAllocation = (MplsLabelResourceAllocation) req;
-                if (!avail.contains(req)) {
-                    throw new ResourceAllocationException(
-                                                          PositionalParameterStringFormatter
-                                                                  .format("Unable to allocate MPLS label for link "
-                                                                          + "{} MPLS label is {}",
-                                                                          link,
-                                                                          mplsAllocation
-                                                                                  .mplsLabel()
-                                                                                  .toString()));
-                }
-            }
-        }
-        // all requests allocatable => add allocation
-        final LinkKey linkKey = LinkKey.linkKey(link);
-        TransactionalMap<LinkKey, List<LinkResourceAllocations>> linkAllocs = getLinkAllocs(tx);
-        List<LinkResourceAllocations> before = linkAllocs.get(linkKey);
-        if (before == null) {
-            List<LinkResourceAllocations> after = new ArrayList<>();
-            after.add(allocations);
-            linkAllocs.putIfAbsent(linkKey, after);
-        } else {
-            boolean overlapped = before.stream()
-                    .flatMap(x -> x.getResourceAllocation(link).stream())
-                    .anyMatch(x -> allocations.getResourceAllocation(link).contains(x));
-            if (overlapped) {
-                throw new ResourceAllocationException(
-                        String.format("Resource allocations are overlapped between %s and %s",
-                        before, allocations)
-                );
-            }
-            List<LinkResourceAllocations> after = new ArrayList<>(before.size() + 1);
-            after.addAll(before);
-            after.add(allocations);
-            linkAllocs.replace(linkKey, before, after);
-        }
-    }
-
-    @Override
-    public LinkResourceEvent releaseResources(LinkResourceAllocations allocations) {
-        checkNotNull(allocations);
-
-        final IntentId intentId = allocations.intentId();
-        final Collection<Link> links = allocations.links();
-        boolean success = false;
-        do {
-            TransactionContext tx = getTxContext();
-            tx.begin();
-            try {
-                TransactionalMap<IntentId, LinkResourceAllocations> intentAllocs = getIntentAllocs(tx);
-                intentAllocs.remove(intentId);
-
-                TransactionalMap<LinkKey, List<LinkResourceAllocations>> linkAllocs = getLinkAllocs(tx);
-                links.forEach(link -> {
-                    final LinkKey linkId = LinkKey.linkKey(link);
-
-                    List<LinkResourceAllocations> before = linkAllocs.get(linkId);
-                    if (before == null || before.isEmpty()) {
-                        // something is wrong, but it is already freed
-                        log.warn("There was no resource left to release on {}", linkId);
-                        return;
-                    }
-                    List<LinkResourceAllocations> after = new ArrayList<>(before);
-                    after.remove(allocations);
-                    linkAllocs.replace(linkId, before, after);
-                });
-                success = tx.commit();
-            }  catch (Exception e) {
-                log.error("Exception thrown during releaseResource {}", allocations, e);
-                tx.abort();
-                throw e;
-            }
-        } while (!success);
-
-        // Issue events to force recompilation of intents.
-        final List<LinkResourceAllocations> releasedResources = ImmutableList.of(allocations);
-        return new LinkResourceEvent(
-                LinkResourceEvent.Type.ADDITIONAL_RESOURCES_AVAILABLE,
-                releasedResources);
-
-    }
-
-    @Override
-    public LinkResourceAllocations getAllocations(IntentId intentId) {
-        checkNotNull(intentId);
-        Versioned<LinkResourceAllocations> alloc = null;
-        try {
-            alloc = intentAllocMap.get(intentId);
-        } catch (Exception e) {
-            log.warn("Could not read resource allocation information", e);
-        }
-        return alloc == null ? null : alloc.value();
-    }
-
-    @Override
-    public Iterable<LinkResourceAllocations> getAllocations(Link link) {
-        checkNotNull(link);
-        TransactionContext tx = getTxContext();
-        Iterable<LinkResourceAllocations> res = null;
-        tx.begin();
-        try {
-            res = getAllocations(tx, link);
-        } finally {
-            tx.abort();
-        }
-        return res == null ? Collections.emptyList() : res;
-    }
-
-    @Override
-    public Iterable<LinkResourceAllocations> getAllocations() {
-        try {
-            Set<LinkResourceAllocations> allocs =
-                    intentAllocMap.values().stream().map(Versioned::value).collect(Collectors.toSet());
-            return ImmutableSet.copyOf(allocs);
-        } catch (Exception e) {
-            log.warn("Could not read resource allocation information", e);
-        }
-        return ImmutableSet.of();
-    }
-
-    private Iterable<LinkResourceAllocations> getAllocations(TransactionContext tx, Link link) {
-        checkNotNull(tx);
-        checkNotNull(link);
-        final LinkKey key = LinkKey.linkKey(link);
-        TransactionalMap<LinkKey, List<LinkResourceAllocations>> linkAllocs = getLinkAllocs(tx);
-
-        List<LinkResourceAllocations> res = linkAllocs.get(key);
-        if (res != null) {
-            return res;
-        }
-
-        res = linkAllocs.putIfAbsent(key, new ArrayList<>());
-        if (res == null) {
-            return Collections.emptyList();
-        } else {
-            return res;
-        }
-    }
-
-}