[CCSDK-6] Populate seed code
[ccsdk/sli/adaptors.git] / resource-assignment / provider / src / main / java / org / openecomp / sdnc / rm / util / RangeUtil.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
4  * ================================================================================
5  * Copyright (C) 2017 ONAP 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.openecomp.sdnc.rm.util;
23
24 import java.util.ArrayList;
25 import java.util.Date;
26 import java.util.SortedSet;
27 import java.util.TreeSet;
28
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;
35
36 public class RangeUtil {
37
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;
43                                 if (rai.used != null)
44                                         r.used.addAll(rai.used);
45                         }
46         }
47
48         public static boolean checkRange(RangeResource r, RangeAllocationRequest req, int num) {
49                 if (num < req.checkMin || num > req.checkMax)
50                         return false;
51
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))
56                                         return false;
57                         }
58
59                 return true;
60         }
61
62         private static boolean eq(Object o1, Object o2) {
63                 return o1 == null ? o2 == null : o1.equals(o2);
64         }
65
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);
73                         }
74                 return used;
75         }
76
77         public static void allocateRange(
78                 RangeResource rr,
79                 SortedSet<Integer> requestedNumbers,
80                 RangeAllocationRequest req,
81                 String applicationId) {
82                 if (!req.allocate)
83                         return;
84
85                 RangeAllocationItem rai = (RangeAllocationItem) ResourceUtil.getAllocationItem(rr, req.resourceSetId);
86                 if (rai == null) {
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;
97
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;
103                 else
104                         rai.used.addAll(requestedNumbers);
105
106                 rai.allocationTime = new Date();
107
108                 recalculate(rr);
109         }
110 }