[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 / util / RangeUtil.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                         reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.ccsdk.sli.adaptors.rm.util;
23
24 import java.util.ArrayList;
25 import java.util.Date;
26 import java.util.Set;
27 import java.util.SortedSet;
28 import java.util.TreeSet;
29 import org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem;
30 import org.onap.ccsdk.sli.adaptors.rm.data.RangeAllocationItem;
31 import org.onap.ccsdk.sli.adaptors.rm.data.RangeAllocationRequest;
32 import org.onap.ccsdk.sli.adaptors.rm.data.RangeResource;
33 import org.onap.ccsdk.sli.adaptors.rm.data.ResourceKey;
34 import org.onap.ccsdk.sli.adaptors.rm.data.ResourceType;
35
36 public class RangeUtil {
37
38     public static void recalculate(RangeResource r) {
39         r.used = new TreeSet<>();
40         if (r.allocationItems != null) {
41             for (AllocationItem ai : r.allocationItems) {
42                 RangeAllocationItem rai = (RangeAllocationItem) ai;
43                 if (rai.used != null) {
44                     r.used.addAll(rai.used);
45                 }
46             }
47         }
48     }
49
50     public static boolean checkRange(RangeResource r, RangeAllocationRequest req, int num) {
51         if (num < req.checkMin || num > req.checkMax) {
52             return false;
53         }
54
55         if (req.excludeNumbers != null && req.excludeNumbers.contains(num)) {
56             return false;
57         }
58
59         if (r.allocationItems != null) {
60             for (AllocationItem ai : r.allocationItems) {
61                 RangeAllocationItem rai = (RangeAllocationItem) ai;
62                 if (!eq(req.resourceUnionId, rai.resourceUnionId) && rai.used != null && rai.used.contains(num)) {
63                     if (!overlap(rai.resourceShareGroupList, req.resourceShareGroupList)) {
64                         return false;
65                     }
66                 }
67                 if (!req.replace && eq(req.resourceSetId, rai.resourceSetId) && rai.used != null
68                         && rai.used.contains(num)) {
69                     return false;
70                 }
71             }
72         }
73
74         return true;
75     }
76
77     public static SortedSet<Integer> getUsed(RangeResource r, String resourceUnionId) {
78         SortedSet<Integer> used = new TreeSet<>();
79         if (r.allocationItems != null) {
80             for (AllocationItem ai : r.allocationItems) {
81                 RangeAllocationItem rai = (RangeAllocationItem) ai;
82                 if (eq(resourceUnionId, rai.resourceUnionId) && rai.used != null) {
83                     used.addAll(rai.used);
84                 }
85             }
86         }
87         return used;
88     }
89
90     public static void allocateRange(RangeResource rr, SortedSet<Integer> requestedNumbers,
91             RangeAllocationRequest req) {
92         if (!req.allocate) {
93             return;
94         }
95
96         RangeAllocationItem rai = (RangeAllocationItem) ResourceUtil.getAllocationItem(rr, req.resourceSetId);
97         if (rai == null) {
98             rai = new RangeAllocationItem();
99             rai.resourceType = ResourceType.Range;
100             rai.resourceKey = new ResourceKey();
101             rai.resourceKey.assetId = req.assetId;
102             rai.resourceKey.resourceName = req.resourceName;
103             rai.applicationId = req.applicationId;
104             rai.resourceSetId = req.resourceSetId;
105             rai.resourceUnionId = req.resourceUnionId;
106             rai.resourceShareGroupList = req.resourceShareGroupList;
107             rai.used = requestedNumbers;
108
109             if (rr.allocationItems == null) {
110                 rr.allocationItems = new ArrayList<>();
111             }
112             rr.allocationItems.add(rai);
113         } else if (req.replace) {
114             rai.used = requestedNumbers;
115         } else {
116             rai.used.addAll(requestedNumbers);
117         }
118
119         rai.allocationTime = new Date();
120
121         recalculate(rr);
122     }
123
124     private static boolean eq(Object o1, Object o2) {
125         return o1 == null ? o2 == null : o1.equals(o2);
126     }
127
128     private static boolean overlap(Set<String> s1, Set<String> s2) {
129         if (s1 == null || s1.isEmpty() || s2 == null || s2.isEmpty()) {
130             return false;
131         }
132         for (String ss1 : s1) {
133             if (s2.contains(ss1)) {
134                 return true;
135             }
136         }
137         return false;
138     }
139 }