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