2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.ccsdk.sli.adaptors.rm.util;
24 import java.util.ArrayList;
25 import java.util.Date;
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;
37 public class RangeUtil {
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);
51 public static boolean checkRange(RangeResource r, RangeAllocationRequest req, int num) {
52 if (req.excludeNumbers != null && req.excludeNumbers.contains(num)) {
56 if (req.rangeList != null && !req.rangeList.isEmpty()) {
58 for (Range range : req.rangeList) {
59 if (num < range.min || num > range.min) {
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)) {
74 if (!req.replace && eq(req.resourceSetId, rai.resourceSetId) && rai.used != null
75 && rai.used.contains(num)) {
79 if (req.forceNewNumbers && rai.used.contains(num)) {
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);
111 public static void allocateRange(RangeResource rr, SortedSet<Integer> requestedNumbers,
112 RangeAllocationRequest req) {
117 RangeAllocationItem rai = (RangeAllocationItem) ResourceUtil.getAllocationItem(rr, req.resourceSetId);
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;
130 if (rr.allocationItems == null) {
131 rr.allocationItems = new ArrayList<>();
133 rr.allocationItems.add(rai);
134 } else if (req.replace) {
135 rai.used = requestedNumbers;
137 rai.used.addAll(requestedNumbers);
140 rai.allocationTime = new Date();
145 private static boolean eq(Object o1, Object o2) {
146 return o1 == null ? o2 == null : o1.equals(o2);
149 private static boolean overlap(Set<String> s1, Set<String> s2) {
150 if (s1 == null || s1.isEmpty() || s2 == null || s2.isEmpty()) {
153 for (String ss1 : s1) {
154 if (s2.contains(ss1)) {