RA: Add option for sequential number assignment from a range 93/108993/1
authorsb5356 <sb5356@att.com>
Tue, 9 Jun 2020 21:11:26 +0000 (17:11 -0400)
committersb5356 <sb5356@att.com>
Tue, 9 Jun 2020 21:11:26 +0000 (17:11 -0400)
Change-Id: Ifc967676d7c78063ba9ba67706bd4b98ba1d9115
Issue-ID: CCSDK-2416
Signed-off-by: Stan Bonev <sb5356@att.com>
resource-assignment/provider/src/main/java/org/onap/ccsdk/sli/adaptors/ra/ResourceAllocator.java
resource-assignment/provider/src/main/java/org/onap/ccsdk/sli/adaptors/ra/alloc/DbAllocationRule.java
resource-assignment/provider/src/main/java/org/onap/ccsdk/sli/adaptors/ra/comp/ResourceRequest.java
resource-assignment/provider/src/main/java/org/onap/ccsdk/sli/adaptors/rm/comp/AllocationFunction.java
resource-assignment/provider/src/main/java/org/onap/ccsdk/sli/adaptors/rm/data/RangeAllocationRequest.java
resource-assignment/provider/src/test/java/jtest/org/onap/ccsdk/sli/adaptors/ra/TestReserve.java

index 19062d5..10f3e02 100644 (file)
@@ -455,6 +455,8 @@ public class ResourceAllocator implements SvcLogicResource {
         rr.rangeMaxOverride = Integer.parseInt(rangeMaxOverrideStr);
         String rangeForceNewNumbersStr = getParam(ctx, "range-force-new-numbers", false, "false");
         rr.rangeForceNewNumbers = Boolean.parseBoolean(rangeForceNewNumbersStr);
+        String rangeNextInSequenceStr = getParam(ctx, "range-next-in-sequence", false, "false");
+        rr.rangeNextInSequence = Boolean.parseBoolean(rangeNextInSequenceStr);
         String replaceStr = getParam(ctx, "replace", false, "true");
         rr.replace = Boolean.parseBoolean(replaceStr);
         rr.applicationId = getParam(ctx, "application-id", false, "SDNC");
index 12c02c8..d15b150 100644 (file)
@@ -242,6 +242,7 @@ public class DbAllocationRule implements AllocationRule {
 
         }
         ar.forceNewNumbers = resourceRequest.rangeForceNewNumbers;
+        ar.nextInSequence = resourceRequest.rangeNextInSequence;
         return ar;
     }
 
index bc05af6..2ebad4c 100644 (file)
@@ -35,6 +35,7 @@ public class ResourceRequest {
     public int rangeMinOverride;
     public int rangeMaxOverride;
     public boolean rangeForceNewNumbers;
+    public boolean rangeNextInSequence;
     public boolean replace;
     public String requestType;
     public String serviceModel;
index 0f53354..5ec7c26 100644 (file)
@@ -108,7 +108,7 @@ class AllocationFunction extends SynchronizedFunction {
     public void _exec() throws ResourceLockedException {
         outcome = allocate(request);
 
-        if ((outcome!=null) && (outcome.status == AllocationStatus.Success)) {
+        if (outcome!=null && outcome.status == AllocationStatus.Success) {
             for (Resource r : updateList) {
                 resourceDao.saveResource(r);
             }
@@ -334,7 +334,56 @@ class AllocationFunction extends SynchronizedFunction {
                     }
                 }
 
-                if (req.rangeList != null) {
+                if (req.rangeList != null && !req.rangeList.isEmpty()) {
+                    if (req.nextInSequence) {
+                        // This means we allocate numbers in ascending sequence, not trying from the beginning
+                        // of the range (leaving possible holes in the sequence of allocated numbers)
+                        // To do that, we go through the ranges from the last towards the first (assuming
+                        // ranges are ordered from smallest to the largest numbers), and within each range, from
+                        // the max towards the min and find the first allocated number. Then we take the next numbers
+                        // in the range (that we already checked are available).
+
+                        int rangeIndex;
+                        Range range = null;
+                        int n = 0;
+                        boolean foundAllocated = false;
+                        for (rangeIndex = req.rangeList.size() - 1; !foundAllocated && rangeIndex >= 0; rangeIndex--) {
+                            range = req.rangeList.get(rangeIndex);
+                            for (n = range.max; n >= range.min; n--) {
+                                if (!RangeUtil.checkRange(rr, req, n)) {
+                                    foundAllocated = true;
+                                    break;
+                                }
+                            }
+                            if (foundAllocated) {
+                                break;
+                            }
+                        }
+                        if (foundAllocated) {
+                            n++;
+                        }
+                        for (; foundCount < req.requestedCount && n <= range.max; n++) {
+                            foundNumbers.add(n);
+                            foundCount++;
+                        }
+                        if (foundCount < req.requestedCount) {
+                            rangeIndex++;
+                            for (; rangeIndex < req.rangeList.size(); rangeIndex++) {
+                                range = req.rangeList.get(rangeIndex);
+                                for (n = range.min; foundCount < req.requestedCount && n <= range.max; n++) {
+                                    foundNumbers.add(n);
+                                    foundCount++;
+                                }
+                            }
+                        }
+                        // If we could not find enough numbers by going up in sequence,
+                        // reset foundNumbers and foundCount, and go back to the holes
+                        if (foundCount < req.requestedCount) {
+                            foundNumbers = new TreeSet<>();
+                            foundCount = 0;
+                        }
+                    }
+
                     if (req.reverseOrder) {
                         for (int i = req.rangeList.size() - 1; i >= 0; i--) {
                             Range range = req.rangeList.get(i);
index 7b60d36..a7948b0 100644 (file)
@@ -36,4 +36,5 @@ public class RangeAllocationRequest extends AllocationRequest {
     public boolean sequential = false;
     public boolean reverseOrder = false;
     public boolean forceNewNumbers = false;
+    public boolean nextInSequence = false;
 }
index b159127..af9508a 100644 (file)
@@ -1,5 +1,6 @@
 package jtest.org.onap.ccsdk.sli.adaptors.ra;
 
+import static org.junit.Assert.assertNotNull;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.HashMap;
@@ -28,7 +29,6 @@ import org.springframework.jdbc.core.JdbcTemplate;
 import org.springframework.test.context.ContextConfiguration;
 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 import jtest.util.org.onap.ccsdk.sli.adaptors.ra.TestTable;
-import static org.junit.Assert.assertNotNull;
 
 @RunWith(SpringJUnit4ClassRunner.class)
 @ContextConfiguration(locations = { "classpath:test-context.xml" })
@@ -804,4 +804,118 @@ public class TestReserve {
         allocationItem.print();
 
     }
+
+    @Test
+    public void test011() throws Exception {
+        String t = "011";
+        log.info("============== reserve " + t + " ================================");
+        log.info("=== Test range-next-in-sequence = true");
+
+        String entityId = "reserve" + t;
+        String targetId = "port-id-1";
+        String resourceName = "vlan-id-filter";
+
+        String assetId = "Site::" + targetId;
+        String resourceUnion = "SI::" + entityId;
+        String resourceSet = resourceUnion + "::1";
+
+        dataSetup.cleanup();
+        dataSetup.setupRangeItem(resourceName, assetId, resourceSet, resourceUnion, "1002,1004,1006,1008");
+
+        SvcLogicContext ctx = new SvcLogicContext();
+        ctx.setAttribute("ra-input.service-model", "MY-SERV-MODEL");
+        ctx.setAttribute("ra-input.endpoint-position", "VPE-Core2");
+        ctx.setAttribute("ra-input.check-only", "false");
+
+        ctx.setAttribute("ra-input.resource-name", resourceName);
+        ctx.setAttribute("ra-input.range-next-in-sequence", "true");
+
+        ctx.setAttribute("ra-input.reservation-entity-type", "SI");
+        ctx.setAttribute("ra-input.reservation-entity-id", entityId + "_test");
+        ctx.setAttribute("ra-input.reservation-entity-version", "1");
+
+        ctx.setAttribute("ra-input.reservation-target-id", targetId);
+        ctx.setAttribute("ra-input.reservation-target-type", "Site");
+
+        QueryStatus st = resourceAllocator.reserve("NetworkCapacity", null, null, null, ctx);
+
+        Assert.assertTrue(st == QueryStatus.SUCCESS);
+        Assert.assertTrue(dataSetup.checkRangeItem(resourceName, assetId, "SI::" + entityId + "_test::VPE-Core2::1", "1009"));
+    }
+
+    @Test
+    public void test012() throws Exception {
+        String t = "012";
+        log.info("============== reserve " + t + " ================================");
+        log.info("=== Test range-next-in-sequence = false");
+
+        String entityId = "reserve" + t;
+        String targetId = "port-id-1";
+        String resourceName = "vlan-id-filter";
+
+        String assetId = "Site::" + targetId;
+        String resourceUnion = "SI::" + entityId;
+        String resourceSet = resourceUnion + "::1";
+
+        dataSetup.cleanup();
+        dataSetup.setupRangeItem(resourceName, assetId, resourceSet, resourceUnion, "1002,1004,1006,1008");
+
+        SvcLogicContext ctx = new SvcLogicContext();
+        ctx.setAttribute("ra-input.service-model", "MY-SERV-MODEL");
+        ctx.setAttribute("ra-input.endpoint-position", "VPE-Core2");
+        ctx.setAttribute("ra-input.check-only", "false");
+
+        ctx.setAttribute("ra-input.resource-name", resourceName);
+        ctx.setAttribute("ra-input.range-next-in-sequence", "false");
+
+        ctx.setAttribute("ra-input.reservation-entity-type", "SI");
+        ctx.setAttribute("ra-input.reservation-entity-id", entityId + "_test");
+        ctx.setAttribute("ra-input.reservation-entity-version", "1");
+
+        ctx.setAttribute("ra-input.reservation-target-id", targetId);
+        ctx.setAttribute("ra-input.reservation-target-type", "Site");
+
+        QueryStatus st = resourceAllocator.reserve("NetworkCapacity", null, null, null, ctx);
+
+        Assert.assertTrue(st == QueryStatus.SUCCESS);
+        Assert.assertTrue(dataSetup.checkRangeItem(resourceName, assetId, "SI::" + entityId + "_test::VPE-Core2::1", "1003"));
+    }
+
+    @Test
+    public void test013() throws Exception {
+        String t = "013";
+        log.info("============== reserve " + t + " ================================");
+        log.info("=== Test range-next-in-sequence = true - multiple ranges");
+
+        String entityId = "reserve" + t;
+        String targetId = "port-id-1";
+        String resourceName = "vlan-id-filter";
+
+        String assetId = "Site::" + targetId;
+        String resourceUnion = "SI::" + entityId;
+        String resourceSet = resourceUnion + "::1";
+
+        dataSetup.cleanup();
+        dataSetup.setupRangeItem(resourceName, assetId, resourceSet, resourceUnion, "1002,1004,1006,1008,2205-2221");
+
+        SvcLogicContext ctx = new SvcLogicContext();
+        ctx.setAttribute("ra-input.service-model", "MY-SERV-MODEL");
+        ctx.setAttribute("ra-input.endpoint-position", "VPE-Core2");
+        ctx.setAttribute("ra-input.check-only", "false");
+
+        ctx.setAttribute("ra-input.resource-name", resourceName);
+        ctx.setAttribute("ra-input.range-next-in-sequence", "true");
+
+        ctx.setAttribute("ra-input.reservation-entity-type", "SI");
+        ctx.setAttribute("ra-input.reservation-entity-id", entityId + "_test");
+        ctx.setAttribute("ra-input.reservation-entity-version", "1");
+
+        ctx.setAttribute("ra-input.reservation-target-id", targetId);
+        ctx.setAttribute("ra-input.reservation-target-type", "Site");
+
+        QueryStatus st = resourceAllocator.reserve("NetworkCapacity", null, null, null, ctx);
+
+        Assert.assertTrue(st == QueryStatus.SUCCESS);
+        Assert.assertTrue(dataSetup.checkRangeItem(resourceName, assetId, "SI::" + entityId + "_test::VPE-Core2::1", "2222"));
+    }
 }