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.ra.alloc;
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.List;
28 import org.openecomp.sdnc.ra.comp.AllocationRule;
29 import org.openecomp.sdnc.ra.comp.ServiceData;
30 import org.openecomp.sdnc.ra.equip.data.EquipmentData;
31 import org.openecomp.sdnc.ra.rule.dao.RangeRuleDao;
32 import org.openecomp.sdnc.ra.rule.dao.ResourceRuleDao;
33 import org.openecomp.sdnc.ra.rule.data.RangeRule;
34 import org.openecomp.sdnc.ra.rule.data.ResourceRule;
35 import org.openecomp.sdnc.rm.data.AllocationAction;
36 import org.openecomp.sdnc.rm.data.AllocationRequest;
37 import org.openecomp.sdnc.rm.data.LimitAllocationRequest;
38 import org.openecomp.sdnc.rm.data.MultiResourceAllocationRequest;
39 import org.openecomp.sdnc.rm.data.RangeAllocationRequest;
40 import org.openecomp.sdnc.util.expr.ExpressionEvaluator;
41 import org.openecomp.sdnc.util.str.StrUtil;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
45 public class DbAllocationRule implements AllocationRule {
47 private static final Logger log = LoggerFactory.getLogger(DbAllocationRule.class);
49 private ResourceRuleDao resourceRuleDao;
50 private RangeRuleDao rangeRuleDao;
53 public AllocationRequest buildAllocationRequest(
54 String resourceUnionId,
56 String endPointPosition,
57 ServiceData serviceData,
58 EquipmentData equipmentData,
61 List<ResourceRule> resourceRuleList = resourceRuleDao.getResourceRules(serviceData.serviceModel,
62 endPointPosition, equipmentData.equipmentLevel);
63 List<RangeRule> rangeRuleList =
64 rangeRuleDao.getRangeRules(serviceData.serviceModel, endPointPosition, equipmentData.equipmentLevel);
66 List<AllocationRequest> arlist = new ArrayList<AllocationRequest>();
68 for (ResourceRule rr : resourceRuleList) {
69 if (serviceData.resourceName != null && !serviceData.resourceName.equals(rr.resourceName))
71 AllocationRequest ar1 = buildAllocationRequest(rr, resourceUnionId, resourceSetId, serviceData,
72 equipmentData, checkOnly, change);
75 for (RangeRule rr : rangeRuleList) {
76 if (serviceData.resourceName != null && !serviceData.resourceName.equals(rr.rangeName))
78 AllocationRequest ar1 = buildAllocationRequest(rr, resourceUnionId, resourceSetId, serviceData,
79 equipmentData, checkOnly, change);
86 if (arlist.size() == 1)
89 MultiResourceAllocationRequest ar = new MultiResourceAllocationRequest();
90 ar.stopOnFirstFailure = false;
91 ar.allocationRequestList = arlist;
95 private AllocationRequest buildAllocationRequest(
96 ResourceRule resourceRule,
97 String resourceUnionId,
99 ServiceData serviceData,
100 EquipmentData equipmentData,
103 StrUtil.info(log, resourceRule);
105 LimitAllocationRequest ar = new LimitAllocationRequest();
106 ar.resourceSetId = resourceSetId;
107 ar.resourceUnionId = resourceUnionId;
108 ar.resourceName = resourceRule.resourceName;
109 if (serviceData.resourceShareGroup != null)
110 ar.resourceShareGroupList = Collections.singleton(serviceData.resourceShareGroup);
111 ar.assetId = equipmentData.equipmentId;
112 ar.missingResourceAction = AllocationAction.Succeed_Allocate;
113 ar.expiredResourceAction = AllocationAction.Succeed_Allocate;
116 ar.checkLimit = ExpressionEvaluator.evalLong(
117 change ? resourceRule.hardLimitExpression : resourceRule.softLimitExpression, equipmentData.data);;
118 ar.checkCount = ExpressionEvaluator.evalLong(resourceRule.allocationExpression, serviceData.data);
119 ar.allocateCount = checkOnly ? 0 : ar.checkCount;
123 private AllocationRequest buildAllocationRequest(
125 String resourceUnionId,
126 String resourceSetId,
127 ServiceData serviceData,
128 EquipmentData equipmentData,
131 StrUtil.info(log, rangeRule);
133 RangeAllocationRequest ar = new RangeAllocationRequest();
134 ar.resourceSetId = resourceSetId;
135 ar.resourceUnionId = resourceUnionId;
136 ar.resourceName = rangeRule.rangeName;
137 ar.assetId = equipmentData.equipmentId;
138 ar.missingResourceAction = AllocationAction.Succeed_Allocate;
139 ar.expiredResourceAction = AllocationAction.Succeed_Allocate;
142 ar.allocate = !checkOnly;
143 ar.checkMin = rangeRule.minValue;
144 ar.checkMax = rangeRule.maxValue;
148 public void setResourceRuleDao(ResourceRuleDao resourceRuleDao) {
149 this.resourceRuleDao = resourceRuleDao;
152 public void setRangeRuleDao(RangeRuleDao rangeRuleDao) {
153 this.rangeRuleDao = rangeRuleDao;