[CCSDK-6] Populate seed code
[ccsdk/sli/adaptors.git] / resource-assignment / provider / src / main / java / org / openecomp / sdnc / ra / rule / comp / AllocationRequestBuilderImpl.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.ra.rule.comp;
23
24 import java.util.ArrayList;
25 import java.util.List;
26
27 import org.openecomp.sdnc.ra.comp.ServiceData;
28 import org.openecomp.sdnc.ra.equip.data.EquipmentData;
29 import org.openecomp.sdnc.ra.rule.dao.RangeRuleDao;
30 import org.openecomp.sdnc.ra.rule.dao.ResourceRuleDao;
31 import org.openecomp.sdnc.ra.rule.data.RangeRule;
32 import org.openecomp.sdnc.ra.rule.data.ResourceRule;
33 import org.openecomp.sdnc.ra.rule.data.ResourceThreshold;
34 import org.openecomp.sdnc.ra.rule.data.ThresholdStatus;
35 import org.openecomp.sdnc.rm.data.AllocationAction;
36 import org.openecomp.sdnc.rm.data.AllocationRequest;
37 import org.openecomp.sdnc.rm.data.LimitAllocationOutcome;
38 import org.openecomp.sdnc.rm.data.LimitAllocationRequest;
39 import org.openecomp.sdnc.rm.data.MultiResourceAllocationRequest;
40 import org.openecomp.sdnc.rm.data.RangeAllocationRequest;
41 import org.openecomp.sdnc.util.expr.ExpressionEvaluator;
42 import org.openecomp.sdnc.util.str.StrUtil;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46 public class AllocationRequestBuilderImpl implements AllocationRequestBuilder {
47
48         private static final Logger log = LoggerFactory.getLogger(AllocationRequestBuilderImpl.class);
49
50         private ResourceRuleDao resourceRuleDao;
51         private RangeRuleDao rangeRuleDao;
52
53         @Override
54         public AllocationRequest buildAllocationRequest(
55                 ServiceData serviceData,
56                 EquipmentData equipmentData,
57                 boolean checkOnly,
58                 boolean change) {
59                 List<ResourceRule> resourceRuleList = resourceRuleDao.getResourceRules(serviceData.serviceModel,
60                         serviceData.endPointPosition, equipmentData.equipmentLevel);
61                 List<RangeRule> rangeRuleList = rangeRuleDao.getRangeRules(serviceData.serviceModel,
62                         serviceData.endPointPosition, equipmentData.equipmentLevel);
63                 if (resourceRuleList.isEmpty() && rangeRuleList.isEmpty())
64                         return null;
65                 if (resourceRuleList.size() == 1 && rangeRuleList.isEmpty())
66                         return buildAllocationRequest(resourceRuleList.get(0), serviceData, equipmentData, checkOnly, change);
67
68                 if (resourceRuleList.isEmpty() && rangeRuleList.size() == 1)
69                         return buildAllocationRequest(rangeRuleList.get(0), serviceData, equipmentData, checkOnly, change);
70
71                 MultiResourceAllocationRequest ar = new MultiResourceAllocationRequest();
72                 ar.stopOnFirstFailure = false;
73                 ar.allocationRequestList = new ArrayList<AllocationRequest>();
74                 for (ResourceRule rr : resourceRuleList) {
75                         AllocationRequest ar1 = buildAllocationRequest(rr, serviceData, equipmentData, checkOnly, change);
76                         ar.allocationRequestList.add(ar1);
77                 }
78                 for (RangeRule rr : rangeRuleList) {
79                         AllocationRequest ar1 = buildAllocationRequest(rr, serviceData, equipmentData, checkOnly, change);
80                         ar.allocationRequestList.add(ar1);
81                 }
82                 return ar;
83         }
84
85         private AllocationRequest buildAllocationRequest(
86                 ResourceRule resourceRule,
87                 ServiceData serviceData,
88                 EquipmentData equipmentData,
89                 boolean checkOnly,
90                 boolean change) {
91                 StrUtil.info(log, resourceRule);
92
93                 LimitAllocationRequest ar = new LimitAllocationRequest();
94                 ar.resourceSetId = serviceData.resourceSetId;
95                 ar.resourceUnionId = serviceData.resourceUnionId;
96                 ar.resourceName = resourceRule.resourceName;
97                 ar.assetId = equipmentData.equipmentId;
98                 ar.missingResourceAction = AllocationAction.Succeed_Allocate;
99                 ar.expiredResourceAction = AllocationAction.Succeed_Allocate;
100                 ar.replace = true;
101                 ar.strict = false;
102                 ar.checkLimit = ExpressionEvaluator.evalLong(
103                         change ? resourceRule.hardLimitExpression : resourceRule.softLimitExpression, equipmentData.data);
104                 ar.checkCount = ExpressionEvaluator.evalLong(resourceRule.allocationExpression, serviceData.data);
105                 ar.allocateCount = checkOnly ? 0 : ar.checkCount;
106                 return ar;
107         }
108
109         private AllocationRequest buildAllocationRequest(
110                 RangeRule rangeRule,
111                 ServiceData serviceData,
112                 EquipmentData equipmentData,
113                 boolean checkOnly,
114                 boolean change) {
115                 StrUtil.info(log, rangeRule);
116
117                 RangeAllocationRequest ar = new RangeAllocationRequest();
118                 ar.resourceSetId = serviceData.resourceSetId;
119                 ar.resourceUnionId = serviceData.resourceUnionId;
120                 ar.resourceName = rangeRule.rangeName;
121                 ar.assetId = equipmentData.equipmentId;
122                 ar.missingResourceAction = AllocationAction.Succeed_Allocate;
123                 ar.expiredResourceAction = AllocationAction.Succeed_Allocate;
124                 ar.replace = true;
125                 ar.check = true;
126                 ar.allocate = !checkOnly;
127                 ar.checkMin = rangeRule.minValue;
128                 ar.checkMax = rangeRule.maxValue;
129                 return ar;
130         }
131
132         @Override
133         public ThresholdStatus getThresholdStatus(
134                 ServiceData serviceData,
135                 EquipmentData equipmentData,
136                 LimitAllocationOutcome limitAllocationOutcome) {
137                 ResourceRule rr = resourceRuleDao.getResourceRule(serviceData.serviceModel, serviceData.endPointPosition,
138                         equipmentData.equipmentLevel, limitAllocationOutcome.request.resourceName);
139                 if (rr == null || rr.thresholdList == null || rr.thresholdList.isEmpty())
140                         return null;
141
142                 ThresholdStatus thresholdStatus = null;
143                 long maxThresholdValue = 0;
144                 for (ResourceThreshold th : rr.thresholdList) {
145                         long thresholdValue = ExpressionEvaluator.evalLong(th.expression, equipmentData.data);
146
147                         if (thresholdValue > maxThresholdValue) {
148                                 maxThresholdValue = thresholdValue;
149
150                                 if (limitAllocationOutcome.used >= thresholdValue) {
151                                         thresholdStatus = new ThresholdStatus();
152                                         thresholdStatus.resourceRule = rr;
153                                         thresholdStatus.resourceThreshold = th;
154                                         thresholdStatus.limitValue = limitAllocationOutcome.limit;
155                                         thresholdStatus.thresholdValue = thresholdValue;
156                                         thresholdStatus.used = limitAllocationOutcome.used;
157                                         thresholdStatus.lastAdded = limitAllocationOutcome.allocatedCount;
158                                 }
159                         }
160                 }
161
162                 return thresholdStatus;
163         }
164
165         public void setResourceRuleDao(ResourceRuleDao resourceRuleDao) {
166                 this.resourceRuleDao = resourceRuleDao;
167         }
168
169         public void setRangeRuleDao(RangeRuleDao rangeRuleDao) {
170                 this.rangeRuleDao = rangeRuleDao;
171         }
172 }