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;
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;
34 public class LabelUtil {
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))
47 public static String allocateLabel(LabelResource l, LabelAllocationRequest req, String applicationId) {
51 LabelAllocationItem lai = (LabelAllocationItem) ResourceUtil.getAllocationItem(l, req.resourceSetId);
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;
63 if (l.allocationItems == null)
64 l.allocationItems = new ArrayList<AllocationItem>();
65 l.allocationItems.add(lai);
68 lai.label = req.label;
69 lai.allocationTime = new Date();
76 public static void recalculate(LabelResource l) {
79 if (l.allocationItems != null)
80 for (AllocationItem ai : l.allocationItems) {
81 LabelAllocationItem lai = (LabelAllocationItem) ai;
82 if (lai.label != null) {
86 else if (!l.label.equals(lai.label))
87 l.label = "__BLOCKED__";
92 private static boolean eq(Object o1, Object o2) {
93 return o1 == null ? o2 == null : o1.equals(o2);