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