2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2017 AT&T 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.onap.ccsdk.sli.adaptors.ra.rule.comp;
 
  24 import java.util.ArrayList;
 
  25 import java.util.List;
 
  27 import org.onap.ccsdk.sli.adaptors.ra.comp.ServiceData;
 
  28 import org.onap.ccsdk.sli.adaptors.ra.equip.data.EquipmentData;
 
  29 import org.onap.ccsdk.sli.adaptors.ra.rule.dao.RangeRuleDao;
 
  30 import org.onap.ccsdk.sli.adaptors.ra.rule.dao.ResourceRuleDao;
 
  31 import org.onap.ccsdk.sli.adaptors.ra.rule.data.RangeRule;
 
  32 import org.onap.ccsdk.sli.adaptors.ra.rule.data.ResourceRule;
 
  33 import org.onap.ccsdk.sli.adaptors.ra.rule.data.ResourceThreshold;
 
  34 import org.onap.ccsdk.sli.adaptors.ra.rule.data.ThresholdStatus;
 
  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.LimitAllocationOutcome;
 
  38 import org.onap.ccsdk.sli.adaptors.rm.data.LimitAllocationRequest;
 
  39 import org.onap.ccsdk.sli.adaptors.rm.data.MultiResourceAllocationRequest;
 
  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;
 
  46 public class AllocationRequestBuilderImpl implements AllocationRequestBuilder {
 
  48     private static final Logger log = LoggerFactory.getLogger(AllocationRequestBuilderImpl.class);
 
  50     private ResourceRuleDao resourceRuleDao;
 
  51     private RangeRuleDao rangeRuleDao;
 
  54     public AllocationRequest buildAllocationRequest(
 
  55             ServiceData serviceData,
 
  56             EquipmentData equipmentData,
 
  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())
 
  65         if (resourceRuleList.size() == 1 && rangeRuleList.isEmpty())
 
  66             return buildAllocationRequest(resourceRuleList.get(0), serviceData, equipmentData, checkOnly, change);
 
  68         if (resourceRuleList.isEmpty() && rangeRuleList.size() == 1)
 
  69             return buildAllocationRequest(rangeRuleList.get(0), serviceData, equipmentData, checkOnly, change);
 
  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);
 
  78         for (RangeRule rr : rangeRuleList) {
 
  79             AllocationRequest ar1 = buildAllocationRequest(rr, serviceData, equipmentData, checkOnly, change);
 
  80             ar.allocationRequestList.add(ar1);
 
  85     private AllocationRequest buildAllocationRequest(
 
  86             ResourceRule resourceRule,
 
  87             ServiceData serviceData,
 
  88             EquipmentData equipmentData,
 
  91         StrUtil.info(log, resourceRule);
 
  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;
 
 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;
 
 109     private AllocationRequest buildAllocationRequest(
 
 111             ServiceData serviceData,
 
 112             EquipmentData equipmentData,
 
 115         StrUtil.info(log, rangeRule);
 
 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;
 
 126         ar.allocate = !checkOnly;
 
 127         ar.checkMin = rangeRule.minValue;
 
 128         ar.checkMax = rangeRule.maxValue;
 
 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())
 
 142         ThresholdStatus thresholdStatus = null;
 
 143         long maxThresholdValue = 0;
 
 144         for (ResourceThreshold th : rr.thresholdList) {
 
 145             long thresholdValue = ExpressionEvaluator.evalLong(th.expression, equipmentData.data);
 
 147             if (thresholdValue > maxThresholdValue) {
 
 148                 maxThresholdValue = thresholdValue;
 
 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;
 
 162         return thresholdStatus;
 
 165     public void setResourceRuleDao(ResourceRuleDao resourceRuleDao) {
 
 166         this.resourceRuleDao = resourceRuleDao;
 
 169     public void setRangeRuleDao(RangeRuleDao rangeRuleDao) {
 
 170         this.rangeRuleDao = rangeRuleDao;