a0b698d73c8c1ac946ef4b4f0f9e0592c51d2ed2
[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
28 import org.onap.ccsdk.sli.adaptors.ra.comp.AllocationRule;
29 import org.onap.ccsdk.sli.adaptors.ra.comp.ServiceData;
30 import org.onap.ccsdk.sli.adaptors.ra.equip.data.EquipmentData;
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(
54             String resourceUnionId,
55             String resourceSetId,
56             String endPointPosition,
57             ServiceData serviceData,
58             EquipmentData equipmentData,
59             boolean checkOnly,
60             boolean change) {
61         List<ResourceRule> resourceRuleList = resourceRuleDao.getResourceRules(serviceData.serviceModel,
62                 endPointPosition, equipmentData.equipmentLevel);
63         List<RangeRule> rangeRuleList =
64                 rangeRuleDao.getRangeRules(serviceData.serviceModel, endPointPosition, equipmentData.equipmentLevel);
65
66         List<AllocationRequest> arlist = new ArrayList<AllocationRequest>();
67
68         for (ResourceRule rr : resourceRuleList) {
69             if (serviceData.resourceName != null && !serviceData.resourceName.equals(rr.resourceName))
70                 continue;
71             AllocationRequest ar1 = buildAllocationRequest(rr, resourceUnionId, resourceSetId, serviceData,
72                     equipmentData, checkOnly, change);
73             arlist.add(ar1);
74         }
75         for (RangeRule rr : rangeRuleList) {
76             if (serviceData.resourceName != null && !serviceData.resourceName.equals(rr.rangeName))
77                 continue;
78             AllocationRequest ar1 = buildAllocationRequest(rr, resourceUnionId, resourceSetId, serviceData,
79                     equipmentData, checkOnly, change);
80             arlist.add(ar1);
81         }
82
83         if (arlist.isEmpty())
84             return null;
85
86         if (arlist.size() == 1)
87             return arlist.get(0);
88
89         MultiResourceAllocationRequest ar = new MultiResourceAllocationRequest();
90         ar.stopOnFirstFailure = false;
91         ar.allocationRequestList = arlist;
92         return ar;
93     }
94
95     private AllocationRequest buildAllocationRequest(
96             ResourceRule resourceRule,
97             String resourceUnionId,
98             String resourceSetId,
99             ServiceData serviceData,
100             EquipmentData equipmentData,
101             boolean checkOnly,
102             boolean change) {
103         StrUtil.info(log, resourceRule);
104
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;
114         ar.replace = true;
115         ar.strict = false;
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;
120         return ar;
121     }
122
123     private AllocationRequest buildAllocationRequest(
124             RangeRule rangeRule,
125             String resourceUnionId,
126             String resourceSetId,
127             ServiceData serviceData,
128             EquipmentData equipmentData,
129             boolean checkOnly,
130             boolean change) {
131         StrUtil.info(log, rangeRule);
132
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;
140         ar.replace = true;
141         ar.check = true;
142         ar.allocate = !checkOnly;
143         ar.checkMin = rangeRule.minValue;
144         ar.checkMax = rangeRule.maxValue;
145         return ar;
146     }
147
148     public void setResourceRuleDao(ResourceRuleDao resourceRuleDao) {
149         this.resourceRuleDao = resourceRuleDao;
150     }
151
152     public void setRangeRuleDao(RangeRuleDao rangeRuleDao) {
153         this.rangeRuleDao = rangeRuleDao;
154     }
155 }