[CCSDK-245] RA: Refactor RA to make it generic
[ccsdk/sli/adaptors.git] / resource-assignment / provider / src / main / java / org / onap / ccsdk / sli / adaptors / rm / comp / AllocationFunction.java
index bda496a..3544075 100644 (file)
@@ -8,9 +8,9 @@
  * 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.
@@ -29,7 +29,6 @@ import java.util.List;
 import java.util.Set;
 import java.util.SortedSet;
 import java.util.TreeSet;
-
 import org.onap.ccsdk.sli.adaptors.lock.comp.LockHelper;
 import org.onap.ccsdk.sli.adaptors.lock.comp.ResourceLockedException;
 import org.onap.ccsdk.sli.adaptors.lock.comp.SynchronizedFunction;
@@ -66,22 +65,20 @@ class AllocationFunction extends SynchronizedFunction {
 
     private ResourceDao resourceDao;
 
-    private String applicationId;
     private AllocationRequest request;
     private AllocationOutcome outcome;
 
-    private List<Resource> updateList = new ArrayList<Resource>();
+    private List<Resource> updateList = new ArrayList<>();
 
-    public AllocationFunction(LockHelper lockHelper, ResourceDao resourceDao, String applicationId,
-            AllocationRequest request, int lockTimeout) {
+    public AllocationFunction(LockHelper lockHelper, ResourceDao resourceDao, AllocationRequest request,
+            int lockTimeout) {
         super(lockHelper, getLockNames(request), lockTimeout);
-        this.applicationId = applicationId;
         this.resourceDao = resourceDao;
         this.request = request;
     }
 
     private static Collection<String> getLockNames(AllocationRequest request) {
-        Set<String> lockResourceNames = new HashSet<String>();
+        Set<String> lockResourceNames = new HashSet<>();
         addLockNames(lockResourceNames, request);
         return lockResourceNames;
     }
@@ -89,36 +86,47 @@ class AllocationFunction extends SynchronizedFunction {
     private static void addLockNames(Set<String> lockResourceNames, AllocationRequest request) {
         if (request instanceof MultiAssetAllocationRequest) {
             MultiAssetAllocationRequest req = (MultiAssetAllocationRequest) request;
-            if (req.assetIdList != null)
+            if (req.assetIdList != null) {
                 lockResourceNames.addAll(req.assetIdList);
+            }
         } else if (request instanceof MultiResourceAllocationRequest) {
             MultiResourceAllocationRequest req = (MultiResourceAllocationRequest) request;
-            if (req.allocationRequestList != null)
-                for (AllocationRequest request1 : req.allocationRequestList)
+            if (req.allocationRequestList != null) {
+                for (AllocationRequest request1 : req.allocationRequestList) {
                     addLockNames(lockResourceNames, request1);
-        } else if (request.assetId != null)
+                }
+            }
+        } else if (request.assetId != null) {
             lockResourceNames.add(request.assetId);
+        }
     }
 
     @Override
     public void _exec() throws ResourceLockedException {
         outcome = allocate(request);
-        if (outcome.status == AllocationStatus.Success)
-            for (Resource r : updateList)
+        if (outcome.status == AllocationStatus.Success) {
+            for (Resource r : updateList) {
                 resourceDao.saveResource(r);
+            }
+        }
     }
 
     private AllocationOutcome allocate(AllocationRequest allocationRequest) throws ResourceLockedException {
-        if (allocationRequest instanceof MultiAssetAllocationRequest)
+        if (allocationRequest instanceof MultiAssetAllocationRequest) {
             return allocateMultiAsset((MultiAssetAllocationRequest) allocationRequest);
-        if (allocationRequest instanceof MultiResourceAllocationRequest)
+        }
+        if (allocationRequest instanceof MultiResourceAllocationRequest) {
             return allocateMultiResource((MultiResourceAllocationRequest) allocationRequest);
-        if (allocationRequest instanceof LimitAllocationRequest)
+        }
+        if (allocationRequest instanceof LimitAllocationRequest) {
             return allocateLimit((LimitAllocationRequest) allocationRequest);
-        if (allocationRequest instanceof LabelAllocationRequest)
+        }
+        if (allocationRequest instanceof LabelAllocationRequest) {
             return allocateLabel((LabelAllocationRequest) allocationRequest);
-        if (allocationRequest instanceof RangeAllocationRequest)
+        }
+        if (allocationRequest instanceof RangeAllocationRequest) {
             return allocateRange((RangeAllocationRequest) allocationRequest);
+        }
         return null;
     }
 
@@ -130,16 +138,18 @@ class AllocationFunction extends SynchronizedFunction {
     private MultiResourceAllocationOutcome allocateMultiResource(MultiResourceAllocationRequest req) {
         MultiResourceAllocationOutcome out = new MultiResourceAllocationOutcome();
         out.request = req;
-        out.allocationOutcomeList = new ArrayList<AllocationOutcome>();
+        out.allocationOutcomeList = new ArrayList<>();
         out.status = AllocationStatus.Success;
 
-        if (req.allocationRequestList != null)
+        if (req.allocationRequestList != null) {
             for (AllocationRequest req1 : req.allocationRequestList) {
                 AllocationOutcome out1 = allocate(req1);
                 out.allocationOutcomeList.add(out1);
-                if (out1.status != AllocationStatus.Success)
+                if (out1.status != AllocationStatus.Success) {
                     out.status = AllocationStatus.Failure;
+                }
             }
+        }
 
         return out;
     }
@@ -167,11 +177,12 @@ class AllocationFunction extends SynchronizedFunction {
         if (LimitUtil.checkLimit(l, req)) {
             out.status = AllocationStatus.Success;
             if (req.allocateCount > 0) {
-                out.allocatedCount = LimitUtil.allocateLimit(l, req, applicationId);
+                out.allocatedCount = LimitUtil.allocateLimit(l, req);
                 updateList.add(l);
             }
-        } else
+        } else {
             out.status = AllocationStatus.Failure;
+        }
 
         out.used = l.used;
         out.limit = req.checkLimit;
@@ -204,11 +215,12 @@ class AllocationFunction extends SynchronizedFunction {
             out.status = AllocationStatus.Success;
             out.currentLabel = l.label;
             if (req.allocate) {
-                out.allocatedLabel = LabelUtil.allocateLabel(l, req, applicationId);
+                out.allocatedLabel = LabelUtil.allocateLabel(l, req);
                 updateList.add(l);
             }
-        } else
+        } else {
             out.status = AllocationStatus.Failure;
+        }
 
         return out;
     }
@@ -242,18 +254,19 @@ class AllocationFunction extends SynchronizedFunction {
             if (req.requestedNumbers != null && req.requestedNumbers.size() > 0) {
                 foundNumbers = req.requestedNumbers;
                 out.status = AllocationStatus.Success;
-                for (int n : foundNumbers)
+                for (int n : foundNumbers) {
                     if (!RangeUtil.checkRange(rr, req, n)) {
                         out.status = AllocationStatus.Failure;
                         break;
                     }
+                }
             } else {
-                foundNumbers = new TreeSet<Integer>();
+                foundNumbers = new TreeSet<>();
                 int foundCount = 0;
 
                 // First try to reuse the numbers already taken by the same resource union
                 SortedSet<Integer> uu = RangeUtil.getUsed(rr, req.resourceUnionId);
-                if (uu != null && !uu.isEmpty()) {
+                if (uu != null && !uu.isEmpty() && req.replace) {
                     if (uu.size() >= req.requestedCount) {
                         // Just take the first req.requestedCount numbers from uu
                         Iterator<Integer> i = uu.iterator();
@@ -278,33 +291,48 @@ class AllocationFunction extends SynchronizedFunction {
                             if (RangeUtil.checkRange(rr, req, n)) {
                                 foundNumbers.add(n);
                                 foundCount++;
-                            } else if (req.sequential)
+                            } else if (req.sequential) {
                                 break;
+                            }
                         }
                         for (int n = uumax; foundCount < req.requestedCount && n <= req.checkMax; n++) {
                             if (RangeUtil.checkRange(rr, req, n)) {
                                 foundNumbers.add(n);
                                 foundCount++;
-                            } else if (req.sequential)
+                            } else if (req.sequential) {
                                 break;
+                            }
                         }
 
                         // If we could not find enough numbers trying to reuse currently
                         // allocated, reset foundNumbers and foundCount, continue with
                         // the normal allocation of new numbers.
                         if (foundCount < req.requestedCount) {
-                            foundNumbers = new TreeSet<Integer>();
+                            foundNumbers = new TreeSet<>();
                             foundCount = 0;
                         }
                     }
                 }
 
-                for (int n = req.checkMin; foundCount < req.requestedCount && n <= req.checkMax; n++)
-                    if (RangeUtil.checkRange(rr, req, n)) {
-                        foundNumbers.add(n);
-                        foundCount++;
-                    } else if (req.sequential)
-                        foundCount = 0;
+                if (req.reverseOrder) {
+                    for (int n = req.checkMax; foundCount < req.requestedCount && n >= req.checkMin; n--) {
+                        if (RangeUtil.checkRange(rr, req, n)) {
+                            foundNumbers.add(n);
+                            foundCount++;
+                        } else if (req.sequential) {
+                            foundCount = 0;
+                        }
+                    }
+                } else {
+                    for (int n = req.checkMin; foundCount < req.requestedCount && n <= req.checkMax; n++) {
+                        if (RangeUtil.checkRange(rr, req, n)) {
+                            foundNumbers.add(n);
+                            foundCount++;
+                        } else if (req.sequential) {
+                            foundCount = 0;
+                        }
+                    }
+                }
 
                 out.status = foundCount == req.requestedCount ? AllocationStatus.Success : AllocationStatus.Failure;
             }
@@ -313,11 +341,12 @@ class AllocationFunction extends SynchronizedFunction {
         if (out.status == AllocationStatus.Success) {
             out.allocated = foundNumbers;
             if (req.allocate) {
-                RangeUtil.allocateRange(rr, out.allocated, req, applicationId);
+                RangeUtil.allocateRange(rr, out.allocated, req);
                 updateList.add(rr);
             }
-        } else
-            out.allocated = new TreeSet<Integer>();
+        } else {
+            out.allocated = new TreeSet<>();
+        }
 
         out.used = rr.used;