[CCSDK-245] RA: Refactor RA to make it generic
[ccsdk/sli/adaptors.git] / resource-assignment / provider / src / main / java / org / onap / ccsdk / sli / adaptors / ra / alloc / DbAllocationRule.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.ra.alloc;
23
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.List;
27 import org.onap.ccsdk.sli.adaptors.ra.comp.AllocationRule;
28 import org.onap.ccsdk.sli.adaptors.ra.comp.ResourceEntity;
29 import org.onap.ccsdk.sli.adaptors.ra.comp.ResourceRequest;
30 import org.onap.ccsdk.sli.adaptors.ra.comp.ResourceTarget;
31 import org.onap.ccsdk.sli.adaptors.ra.rule.dao.RangeRuleDao;
32 import org.onap.ccsdk.sli.adaptors.ra.rule.dao.ResourceRuleDao;
33 import org.onap.ccsdk.sli.adaptors.ra.rule.data.RangeRule;
34 import org.onap.ccsdk.sli.adaptors.ra.rule.data.ResourceRule;
35 import org.onap.ccsdk.sli.adaptors.rm.data.AllocationAction;
36 import org.onap.ccsdk.sli.adaptors.rm.data.AllocationRequest;
37 import org.onap.ccsdk.sli.adaptors.rm.data.LimitAllocationRequest;
38 import org.onap.ccsdk.sli.adaptors.rm.data.MultiResourceAllocationRequest;
39 import org.onap.ccsdk.sli.adaptors.rm.data.RangeAllocationRequest;
40 import org.onap.ccsdk.sli.adaptors.util.expr.ExpressionEvaluator;
41 import org.onap.ccsdk.sli.adaptors.util.str.StrUtil;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 public class DbAllocationRule implements AllocationRule {
46
47         private static final Logger log = LoggerFactory.getLogger(DbAllocationRule.class);
48
49         private ResourceRuleDao resourceRuleDao;
50         private RangeRuleDao rangeRuleDao;
51
52         @Override
53         public AllocationRequest buildAllocationRequest(String serviceModel, ResourceEntity resourceEntity,
54                         ResourceTarget resourceTarget, ResourceRequest resourceRequest, boolean checkOnly, boolean change) {
55                 List<ResourceRule> resourceRuleList = resourceRuleDao.getResourceRules(serviceModel,
56                                 resourceTarget.resourceTargetType);
57                 List<RangeRule> rangeRuleList = rangeRuleDao.getRangeRules(serviceModel, resourceTarget.resourceTargetType);
58
59                 List<AllocationRequest> arlist = new ArrayList<>();
60
61                 for (ResourceRule rr : resourceRuleList) {
62                         if (resourceRequest.resourceName != null && !resourceRequest.resourceName.equals(rr.resourceName)) {
63                                 continue;
64                         }
65
66                         boolean matches = ExpressionEvaluator.evalBoolean(rr.serviceExpression, resourceEntity.data);
67                         matches = matches && ExpressionEvaluator.evalBoolean(rr.equipmentExpression, resourceTarget.data);
68
69                         if (matches) {
70                                 AllocationRequest ar1 = buildAllocationRequest(rr, resourceEntity, resourceTarget, resourceRequest,
71                                                 checkOnly, change);
72                                 arlist.add(ar1);
73                         }
74                 }
75
76                 for (RangeRule rr : rangeRuleList) {
77                         if (resourceRequest.resourceName != null && !resourceRequest.resourceName.equals(rr.rangeName)) {
78                                 continue;
79                         }
80                         if (resourceRequest.endPointPosition != null
81                                         && !resourceRequest.endPointPosition.equals(rr.endPointPosition)) {
82                                 continue;
83                         }
84
85                         AllocationRequest ar1 = buildAllocationRequest(rr, resourceEntity, resourceTarget, resourceRequest,
86                                         checkOnly, change);
87                         arlist.add(ar1);
88                 }
89
90                 if (arlist.isEmpty()) {
91                         return null;
92                 }
93
94                 if (arlist.size() == 1) {
95                         return arlist.get(0);
96                 }
97
98                 MultiResourceAllocationRequest ar = new MultiResourceAllocationRequest();
99                 ar.stopOnFirstFailure = false;
100                 ar.allocationRequestList = arlist;
101                 return ar;
102         }
103
104         private AllocationRequest buildAllocationRequest(ResourceRule resourceRule, ResourceEntity resourceEntity,
105                         ResourceTarget resourceTarget, ResourceRequest resourceRequest, boolean checkOnly, boolean change) {
106                 StrUtil.info(log, resourceRule);
107
108                 LimitAllocationRequest ar = new LimitAllocationRequest();
109                 ar.applicationId = resourceRequest.applicationId;
110                 ar.resourceUnionId = resourceEntity.resourceEntityType + "::" + resourceEntity.resourceEntityId;
111                 ar.resourceSetId = ar.resourceUnionId + "::" + resourceEntity.resourceEntityVersion;
112                 ar.resourceName = resourceRule.resourceName;
113                 if (resourceRequest.resourceShareGroup != null) {
114                         ar.resourceShareGroupList = Collections.singleton(resourceRequest.resourceShareGroup);
115                 }
116                 ar.assetId = resourceTarget.resourceTargetType + "::" + resourceTarget.resourceTargetId;
117                 ar.missingResourceAction = AllocationAction.Succeed_Allocate;
118                 ar.expiredResourceAction = AllocationAction.Succeed_Allocate;
119                 ar.replace = resourceRequest.replace;
120                 ar.strict = false;
121                 ar.checkLimit = ExpressionEvaluator.evalLong(
122                                 change ? resourceRule.hardLimitExpression : resourceRule.softLimitExpression, resourceTarget.data);
123                 ar.checkCount = ExpressionEvaluator.evalLong(resourceRule.allocationExpression, resourceEntity.data);
124                 ar.allocateCount = checkOnly ? 0 : ar.checkCount;
125                 return ar;
126         }
127
128         private AllocationRequest buildAllocationRequest(RangeRule rangeRule, ResourceEntity resourceEntity,
129                         ResourceTarget resourceTarget, ResourceRequest resourceRequest, boolean checkOnly, boolean change) {
130                 StrUtil.info(log, rangeRule);
131
132                 RangeAllocationRequest ar = new RangeAllocationRequest();
133                 ar.applicationId = resourceRequest.applicationId;
134                 if (resourceRequest.endPointPosition != null) {
135                         ar.resourceUnionId = resourceEntity.resourceEntityType + "::" + resourceEntity.resourceEntityId + "::"
136                                         + resourceRequest.endPointPosition;
137                         ar.endPointPosition = resourceRequest.endPointPosition;
138                 }else
139                         ar.resourceUnionId = resourceEntity.resourceEntityType + "::" + resourceEntity.resourceEntityId;
140                 ar.resourceSetId = ar.resourceUnionId + "::" + resourceEntity.resourceEntityVersion;
141                 ar.resourceName = rangeRule.rangeName;
142                 if (resourceRequest.resourceShareGroup != null) {
143                         ar.resourceShareGroupList = Collections.singleton(resourceRequest.resourceShareGroup);
144                 }
145                 ar.assetId = resourceTarget.resourceTargetType + "::" + resourceTarget.resourceTargetId;
146                 ar.requestedNumbers = StrUtil.listInt(resourceRequest.rangeRequestedNumbers,
147                                 "Invalid value for requested-numbers");
148                 if (ar.requestedNumbers != null) {
149                         ar.requestedCount = ar.requestedNumbers.size();
150                 }
151                 ar.excludeNumbers = StrUtil.listInt(resourceRequest.rangeExcludeNumbers, "Invalid value for exclude-numbers");
152                 ar.reverseOrder = resourceRequest.rangeReverseOrder;
153                 ar.missingResourceAction = AllocationAction.Succeed_Allocate;
154                 ar.expiredResourceAction = AllocationAction.Succeed_Allocate;
155                 ar.replace = resourceRequest.replace;
156                 ar.check = true;
157                 ar.allocate = !checkOnly;
158                 ar.checkMin = resourceRequest.rangeMinOverride >= 0 ? resourceRequest.rangeMinOverride : rangeRule.minValue;
159                 ar.checkMax = resourceRequest.rangeMaxOverride >= 0 ? resourceRequest.rangeMaxOverride : rangeRule.maxValue;
160                 return ar;
161         }
162
163         public void setResourceRuleDao(ResourceRuleDao resourceRuleDao) {
164                 this.resourceRuleDao = resourceRuleDao;
165         }
166
167         public void setRangeRuleDao(RangeRuleDao rangeRuleDao) {
168                 this.rangeRuleDao = rangeRuleDao;
169         }
170 }