RA: Add capability to assign new numbers for range
[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.Range;
31 import org.onap.ccsdk.sli.adaptors.rm.data.RangeAllocationItem;
32 import org.onap.ccsdk.sli.adaptors.rm.data.RangeAllocationRequest;
33 import org.onap.ccsdk.sli.adaptors.rm.data.RangeResource;
34 import org.onap.ccsdk.sli.adaptors.rm.data.ResourceKey;
35 import org.onap.ccsdk.sli.adaptors.rm.data.ResourceType;
36
37 public class RangeUtil {
38
39     public static void recalculate(RangeResource r) {
40         r.used = new TreeSet<>();
41         if (r.allocationItems != null) {
42             for (AllocationItem ai : r.allocationItems) {
43                 RangeAllocationItem rai = (RangeAllocationItem) ai;
44                 if (rai.used != null) {
45                     r.used.addAll(rai.used);
46                 }
47             }
48         }
49     }
50
51     public static boolean checkRange(RangeResource r, RangeAllocationRequest req, int num) {
52         if (req.excludeNumbers != null && req.excludeNumbers.contains(num)) {
53             return false;
54         }
55
56         if (req.rangeList != null && !req.rangeList.isEmpty()) {
57             boolean good = false;
58             for (Range range : req.rangeList) {
59                 if (num < range.min || num > range.min) {
60                     continue;
61                 }
62
63                 boolean found = false;
64                 if (r.allocationItems != null) {
65                     for (AllocationItem ai : r.allocationItems) {
66                         RangeAllocationItem rai = (RangeAllocationItem) ai;
67                         if (!eq(req.resourceUnionId, rai.resourceUnionId) && rai.used != null
68                                 && rai.used.contains(num)) {
69                             if (!overlap(rai.resourceShareGroupList, req.resourceShareGroupList)) {
70                                 found = true;
71                                 break;
72                             }
73                         }
74                         if (!req.replace && eq(req.resourceSetId, rai.resourceSetId) && rai.used != null
75                                 && rai.used.contains(num)) {
76                             found = true;
77                             break;
78                         }
79                         if (req.forceNewNumbers && rai.used.contains(num)) {
80                             found = true;
81                             break;
82                         }
83                     }
84                 }
85
86                 if (!found) {
87                     good = true;
88                     break;
89                 }
90             }
91
92             return good;
93         }
94
95         return true;
96     }
97
98     public static SortedSet<Integer> getUsed(RangeResource r, String resourceUnionId) {
99         SortedSet<Integer> used = new TreeSet<>();
100         if (r.allocationItems != null) {
101             for (AllocationItem ai : r.allocationItems) {
102                 RangeAllocationItem rai = (RangeAllocationItem) ai;
103                 if (eq(resourceUnionId, rai.resourceUnionId) && rai.used != null) {
104                     used.addAll(rai.used);
105                 }
106             }
107         }
108         return used;
109     }
110
111     public static void allocateRange(RangeResource rr, SortedSet<Integer> requestedNumbers,
112             RangeAllocationRequest req) {
113         if (!req.allocate) {
114             return;
115         }
116
117         RangeAllocationItem rai = (RangeAllocationItem) ResourceUtil.getAllocationItem(rr, req.resourceSetId);
118         if (rai == null) {
119             rai = new RangeAllocationItem();
120             rai.resourceType = ResourceType.Range;
121             rai.resourceKey = new ResourceKey();
122             rai.resourceKey.assetId = req.assetId;
123             rai.resourceKey.resourceName = req.resourceName;
124             rai.applicationId = req.applicationId;
125             rai.resourceSetId = req.resourceSetId;
126             rai.resourceUnionId = req.resourceUnionId;
127             rai.resourceShareGroupList = req.resourceShareGroupList;
128             rai.used = requestedNumbers;
129
130             if (rr.allocationItems == null) {
131                 rr.allocationItems = new ArrayList<>();
132             }
133             rr.allocationItems.add(rai);
134         } else if (req.replace) {
135             rai.used = requestedNumbers;
136         } else {
137             rai.used.addAll(requestedNumbers);
138         }
139
140         rai.allocationTime = new Date();
141
142         recalculate(rr);
143     }
144
145     private static boolean eq(Object o1, Object o2) {
146         return o1 == null ? o2 == null : o1.equals(o2);
147     }
148
149     private static boolean overlap(Set<String> s1, Set<String> s2) {
150         if (s1 == null || s1.isEmpty() || s2 == null || s2.isEmpty()) {
151             return false;
152         }
153         for (String ss1 : s1) {
154             if (s2.contains(ss1)) {
155                 return true;
156             }
157         }
158         return false;
159     }
160 }