Support for defining multiple ranges in RANGE_RULE
[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                     }
80                 }
81                 
82                 if (!found) {
83                     good = true;
84                     break;
85                 }
86             }
87             
88             return good;
89         }
90
91         return true;
92     }
93
94     public static SortedSet<Integer> getUsed(RangeResource r, String resourceUnionId) {
95         SortedSet<Integer> used = new TreeSet<>();
96         if (r.allocationItems != null) {
97             for (AllocationItem ai : r.allocationItems) {
98                 RangeAllocationItem rai = (RangeAllocationItem) ai;
99                 if (eq(resourceUnionId, rai.resourceUnionId) && rai.used != null) {
100                     used.addAll(rai.used);
101                 }
102             }
103         }
104         return used;
105     }
106
107     public static void allocateRange(RangeResource rr, SortedSet<Integer> requestedNumbers,
108             RangeAllocationRequest req) {
109         if (!req.allocate) {
110             return;
111         }
112
113         RangeAllocationItem rai = (RangeAllocationItem) ResourceUtil.getAllocationItem(rr, req.resourceSetId);
114         if (rai == null) {
115             rai = new RangeAllocationItem();
116             rai.resourceType = ResourceType.Range;
117             rai.resourceKey = new ResourceKey();
118             rai.resourceKey.assetId = req.assetId;
119             rai.resourceKey.resourceName = req.resourceName;
120             rai.applicationId = req.applicationId;
121             rai.resourceSetId = req.resourceSetId;
122             rai.resourceUnionId = req.resourceUnionId;
123             rai.resourceShareGroupList = req.resourceShareGroupList;
124             rai.used = requestedNumbers;
125
126             if (rr.allocationItems == null) {
127                 rr.allocationItems = new ArrayList<>();
128             }
129             rr.allocationItems.add(rai);
130         } else if (req.replace) {
131             rai.used = requestedNumbers;
132         } else {
133             rai.used.addAll(requestedNumbers);
134         }
135
136         rai.allocationTime = new Date();
137
138         recalculate(rr);
139     }
140
141     private static boolean eq(Object o1, Object o2) {
142         return o1 == null ? o2 == null : o1.equals(o2);
143     }
144
145     private static boolean overlap(Set<String> s1, Set<String> s2) {
146         if (s1 == null || s1.isEmpty() || s2 == null || s2.isEmpty()) {
147             return false;
148         }
149         for (String ss1 : s1) {
150             if (s2.contains(ss1)) {
151                 return true;
152             }
153         }
154         return false;
155     }
156 }