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.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;
36 public class RangeUtil {
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);
50 public static boolean checkRange(RangeResource r, RangeAllocationRequest req, int num) {
51 if (num < req.checkMin || num > req.checkMax) {
55 if (req.excludeNumbers != null && req.excludeNumbers.contains(num)) {
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)) {
67 if (!req.replace && eq(req.resourceSetId, rai.resourceSetId) && rai.used != null
68 && rai.used.contains(num)) {
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);
90 public static void allocateRange(RangeResource rr, SortedSet<Integer> requestedNumbers,
91 RangeAllocationRequest req) {
96 RangeAllocationItem rai = (RangeAllocationItem) ResourceUtil.getAllocationItem(rr, req.resourceSetId);
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;
109 if (rr.allocationItems == null) {
110 rr.allocationItems = new ArrayList<>();
112 rr.allocationItems.add(rai);
113 } else if (req.replace) {
114 rai.used = requestedNumbers;
116 rai.used.addAll(requestedNumbers);
119 rai.allocationTime = new Date();
124 private static boolean eq(Object o1, Object o2) {
125 return o1 == null ? o2 == null : o1.equals(o2);
128 private static boolean overlap(Set<String> s1, Set<String> s2) {
129 if (s1 == null || s1.isEmpty() || s2 == null || s2.isEmpty()) {
132 for (String ss1 : s1) {
133 if (s2.contains(ss1)) {