[CCSDK-245] RA: Refactor RA to make it generic
[ccsdk/sli/adaptors.git] / resource-assignment / provider / src / main / java / org / onap / ccsdk / sli / adaptors / rm / util / LabelUtil.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 org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem;
27 import org.onap.ccsdk.sli.adaptors.rm.data.LabelAllocationItem;
28 import org.onap.ccsdk.sli.adaptors.rm.data.LabelAllocationRequest;
29 import org.onap.ccsdk.sli.adaptors.rm.data.LabelResource;
30 import org.onap.ccsdk.sli.adaptors.rm.data.ResourceKey;
31 import org.onap.ccsdk.sli.adaptors.rm.data.ResourceType;
32
33 public class LabelUtil {
34
35     public static boolean checkLabel(LabelResource l, LabelAllocationRequest req) {
36         if (req.check && req.label != null && l.allocationItems != null && !l.allocationItems.isEmpty()) {
37             for (AllocationItem ai : l.allocationItems) {
38                 LabelAllocationItem lai = (LabelAllocationItem) ai;
39                 if (!eq(req.resourceUnionId, lai.resourceUnionId) && !eq(req.label, lai.label)) {
40                     return false;
41                 }
42             }
43         }
44         return true;
45     }
46
47     public static String allocateLabel(LabelResource l, LabelAllocationRequest req) {
48         if (!req.allocate) {
49             return null;
50         }
51
52         LabelAllocationItem lai = (LabelAllocationItem) ResourceUtil.getAllocationItem(l, req.resourceSetId);
53         if (lai == null) {
54             lai = new LabelAllocationItem();
55             lai.resourceType = ResourceType.Label;
56             lai.resourceKey = new ResourceKey();
57             lai.resourceKey.assetId = req.assetId;
58             lai.resourceKey.resourceName = req.resourceName;
59             lai.applicationId = req.applicationId;
60             lai.resourceSetId = req.resourceSetId;
61             lai.resourceUnionId = req.resourceUnionId;
62             lai.resourceShareGroupList = req.resourceShareGroupList;
63
64             if (l.allocationItems == null) {
65                 l.allocationItems = new ArrayList<>();
66             }
67             l.allocationItems.add(lai);
68         }
69
70         lai.label = req.label;
71         lai.allocationTime = new Date();
72
73         recalculate(l);
74
75         return lai.label;
76     }
77
78     public static void recalculate(LabelResource l) {
79         l.label = null;
80         l.referenceCount = 0;
81         if (l.allocationItems != null) {
82             for (AllocationItem ai : l.allocationItems) {
83                 LabelAllocationItem lai = (LabelAllocationItem) ai;
84                 if (lai.label != null) {
85                     l.referenceCount++;
86                     if (l.label == null) {
87                         l.label = lai.label;
88                     } else if (!l.label.equals(lai.label)) {
89                         l.label = "__BLOCKED__";
90                     }
91                 }
92             }
93         }
94     }
95
96     private static boolean eq(Object o1, Object o2) {
97         return o1 == null ? o2 == null : o1.equals(o2);
98     }
99 }