2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 ONAP 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.openecomp.sdnc.rm.util;
24 import java.util.ArrayList;
25 import java.util.Date;
26 import java.util.SortedSet;
27 import java.util.TreeSet;
29 import org.openecomp.sdnc.rm.data.AllocationItem;
30 import org.openecomp.sdnc.rm.data.RangeAllocationItem;
31 import org.openecomp.sdnc.rm.data.RangeAllocationRequest;
32 import org.openecomp.sdnc.rm.data.RangeResource;
33 import org.openecomp.sdnc.rm.data.ResourceKey;
34 import org.openecomp.sdnc.rm.data.ResourceType;
36 public class RangeUtil {
38 public static void recalculate(RangeResource r) {
39 r.used = new TreeSet<Integer>();
40 if (r.allocationItems != null)
41 for (AllocationItem ai : r.allocationItems) {
42 RangeAllocationItem rai = (RangeAllocationItem) ai;
44 r.used.addAll(rai.used);
48 public static boolean checkRange(RangeResource r, RangeAllocationRequest req, int num) {
49 if (num < req.checkMin || num > req.checkMax)
52 if (r.allocationItems != null)
53 for (AllocationItem ai : r.allocationItems) {
54 RangeAllocationItem rai = (RangeAllocationItem) ai;
55 if (!eq(req.resourceUnionId, rai.resourceUnionId) && rai.used != null && rai.used.contains(num))
62 private static boolean eq(Object o1, Object o2) {
63 return o1 == null ? o2 == null : o1.equals(o2);
66 public static SortedSet<Integer> getUsed(RangeResource r, String resourceUnionId) {
67 SortedSet<Integer> used = new TreeSet<Integer>();
68 if (r.allocationItems != null)
69 for (AllocationItem ai : r.allocationItems) {
70 RangeAllocationItem rai = (RangeAllocationItem) ai;
71 if (eq(resourceUnionId, rai.resourceUnionId) && rai.used != null)
72 used.addAll(rai.used);
77 public static void allocateRange(
79 SortedSet<Integer> requestedNumbers,
80 RangeAllocationRequest req,
81 String applicationId) {
85 RangeAllocationItem rai = (RangeAllocationItem) ResourceUtil.getAllocationItem(rr, req.resourceSetId);
87 rai = new RangeAllocationItem();
88 rai.resourceType = ResourceType.Range;
89 rai.resourceKey = new ResourceKey();
90 rai.resourceKey.assetId = req.assetId;
91 rai.resourceKey.resourceName = req.resourceName;
92 rai.applicationId = applicationId;
93 rai.resourceSetId = req.resourceSetId;
94 rai.resourceUnionId = req.resourceUnionId;
95 rai.resourceShareGroupList = req.resourceShareGroupList;
96 rai.used = requestedNumbers;
98 if (rr.allocationItems == null)
99 rr.allocationItems = new ArrayList<AllocationItem>();
100 rr.allocationItems.add(rai);
101 } else if (req.replace)
102 rai.used = requestedNumbers;
104 rai.used.addAll(requestedNumbers);
106 rai.allocationTime = new Date();