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.alloc;
 
  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.rm.data.ResourceType;
 
  42 import org.onap.ccsdk.sli.adaptors.util.expr.ExpressionEvaluator;
 
  43 import org.onap.ccsdk.sli.adaptors.util.str.StrUtil;
 
  44 import org.slf4j.Logger;
 
  45 import org.slf4j.LoggerFactory;
 
  47 public class DbAllocationRule implements AllocationRule {
 
  49     private static final Logger log = LoggerFactory.getLogger(DbAllocationRule.class);
 
  51     private ResourceRuleDao resourceRuleDao;
 
  52     private RangeRuleDao rangeRuleDao;
 
  55     public AllocationRequest buildAllocationRequest(String serviceModel, ResourceEntity resourceEntity,
 
  56             ResourceTarget resourceTarget, ResourceRequest resourceRequest, boolean checkOnly, boolean change) {
 
  57         List<ResourceRule> resourceRuleList = resourceRuleDao.getResourceRules(serviceModel,
 
  58                 resourceTarget.resourceTargetType);
 
  59         List<RangeRule> rangeRuleList = rangeRuleDao.getRangeRules(serviceModel, resourceTarget.resourceTargetType);
 
  61         List<AllocationRequest> arlist = new ArrayList<>();
 
  63         for (ResourceRule rr : resourceRuleList) {
 
  64             if (resourceRequest.resourceName != null && !resourceRequest.resourceName.equals(rr.resourceName)) {
 
  68             boolean matches = ExpressionEvaluator.evalBoolean(rr.serviceExpression, resourceEntity.data);
 
  69             matches = matches && ExpressionEvaluator.evalBoolean(rr.equipmentExpression, resourceTarget.data);
 
  72                 AllocationRequest ar1 = buildAllocationRequest(rr, resourceEntity, resourceTarget, resourceRequest,
 
  78         for (RangeRule rr : rangeRuleList) {
 
  79             if (resourceRequest.resourceName != null && !resourceRequest.resourceName.equals(rr.rangeName)) {
 
  82             if (resourceRequest.endPointPosition != null
 
  83                     && !resourceRequest.endPointPosition.equals(rr.endPointPosition)) {
 
  87             if (!ExpressionEvaluator.evalBoolean(rr.equipmentExpression, resourceTarget.data)) {
 
  91             AllocationRequest ar1 = buildAllocationRequest(rr, resourceEntity, resourceTarget, resourceRequest,
 
  96         if ((rangeRuleList == null || rangeRuleList.isEmpty())
 
  97                 && ResourceType.Range.equals(resourceRequest.resourceType)) {
 
  98             AllocationRequest ar1 = buildAllocationRequest(resourceEntity, resourceTarget, resourceRequest, checkOnly,
 
 103         if (arlist.isEmpty()) {
 
 107         if (arlist.size() == 1) {
 
 108             return arlist.get(0);
 
 111         MultiResourceAllocationRequest ar = new MultiResourceAllocationRequest();
 
 112         ar.stopOnFirstFailure = false;
 
 113         ar.allocationRequestList = arlist;
 
 117     private AllocationRequest buildAllocationRequest(ResourceEntity resourceEntity, ResourceTarget resourceTarget,
 
 118             ResourceRequest resourceRequest, boolean checkOnly, boolean change) {
 
 120         RangeAllocationRequest ar = new RangeAllocationRequest();
 
 121         ar.applicationId = resourceRequest.applicationId;
 
 122         if (resourceRequest.endPointPosition != null) {
 
 123             ar.resourceUnionId = resourceEntity.resourceEntityType + "::" + resourceEntity.resourceEntityId + "::"
 
 124                     + resourceRequest.endPointPosition;
 
 125             ar.endPointPosition = resourceRequest.endPointPosition;
 
 127             ar.resourceUnionId = resourceEntity.resourceEntityType + "::" + resourceEntity.resourceEntityId;
 
 129         ar.resourceSetId = ar.resourceUnionId + "::" + resourceEntity.resourceEntityVersion;
 
 130         ar.resourceName = resourceRequest.resourceName;
 
 131         if (resourceRequest.resourceShareGroup != null) {
 
 132             ar.resourceShareGroupList = Collections.singleton(resourceRequest.resourceShareGroup);
 
 134         ar.assetId = resourceTarget.resourceTargetType + "::" + resourceTarget.resourceTargetId;
 
 135         ar.requestedNumbers = StrUtil.listInt(resourceRequest.rangeRequestedNumbers,
 
 136                 "Invalid value for requested-numbers");
 
 137         if (ar.requestedNumbers != null) {
 
 138             ar.requestedCount = ar.requestedNumbers.size();
 
 140         ar.excludeNumbers = StrUtil.listInt(resourceRequest.rangeExcludeNumbers, "Invalid value for exclude-numbers");
 
 141         ar.reverseOrder = resourceRequest.rangeReverseOrder;
 
 142         ar.missingResourceAction = AllocationAction.Succeed_Allocate;
 
 143         ar.expiredResourceAction = AllocationAction.Succeed_Allocate;
 
 144         ar.replace = resourceRequest.replace;
 
 146         ar.allocate = !checkOnly;
 
 147         ar.rangeList = resourceRequest.rangeOverrideList;
 
 148         if (ar.rangeList == null || ar.rangeList.isEmpty()) {
 
 149             if (resourceRequest.rangeMinOverride >= 0
 
 150                     && resourceRequest.rangeMaxOverride >= resourceRequest.rangeMinOverride) {
 
 151                 ar.rangeList = new ArrayList<>();
 
 152                 Range range = new Range();
 
 153                 range.min = resourceRequest.rangeMinOverride;
 
 154                 range.max = resourceRequest.rangeMaxOverride;
 
 155                 ar.rangeList.add(range);
 
 158             if (resourceRequest.rangeMinOverride >= 0) {
 
 159                 ar.rangeList.get(0).min = resourceRequest.rangeMinOverride;
 
 161             if (resourceRequest.rangeMaxOverride >= 0) {
 
 162                 ar.rangeList.get(ar.rangeList.size() - 1).max = resourceRequest.rangeMaxOverride;
 
 165         ar.forceNewNumbers = resourceRequest.rangeForceNewNumbers;
 
 166         StrUtil.info(log, ar);
 
 170     private AllocationRequest buildAllocationRequest(ResourceRule resourceRule, ResourceEntity resourceEntity,
 
 171             ResourceTarget resourceTarget, ResourceRequest resourceRequest, boolean checkOnly, boolean change) {
 
 172         StrUtil.info(log, resourceRule);
 
 174         LimitAllocationRequest ar = new LimitAllocationRequest();
 
 175         ar.applicationId = resourceRequest.applicationId;
 
 176         ar.resourceUnionId = resourceEntity.resourceEntityType + "::" + resourceEntity.resourceEntityId;
 
 177         ar.resourceSetId = ar.resourceUnionId + "::" + resourceEntity.resourceEntityVersion;
 
 178         ar.resourceName = resourceRule.resourceName;
 
 179         if (resourceRequest.resourceShareGroup != null) {
 
 180             ar.resourceShareGroupList = Collections.singleton(resourceRequest.resourceShareGroup);
 
 182         ar.assetId = resourceTarget.resourceTargetType + "::" + resourceTarget.resourceTargetId;
 
 183         ar.missingResourceAction = AllocationAction.Succeed_Allocate;
 
 184         ar.expiredResourceAction = AllocationAction.Succeed_Allocate;
 
 185         ar.replace = resourceRequest.replace;
 
 187         ar.checkLimit = ExpressionEvaluator.evalLong(
 
 188                 change ? resourceRule.hardLimitExpression : resourceRule.softLimitExpression, resourceTarget.data);
 
 189         ar.checkCount = ExpressionEvaluator.evalLong(resourceRule.allocationExpression, resourceEntity.data);
 
 190         ar.allocateCount = checkOnly ? 0 : ar.checkCount;
 
 194     private AllocationRequest buildAllocationRequest(RangeRule rangeRule, ResourceEntity resourceEntity,
 
 195             ResourceTarget resourceTarget, ResourceRequest resourceRequest, boolean checkOnly, boolean change) {
 
 196         StrUtil.info(log, rangeRule);
 
 198         RangeAllocationRequest ar = new RangeAllocationRequest();
 
 199         ar.applicationId = resourceRequest.applicationId;
 
 200         if (resourceRequest.endPointPosition != null) {
 
 201             ar.resourceUnionId = resourceEntity.resourceEntityType + "::" + resourceEntity.resourceEntityId + "::"
 
 202                     + resourceRequest.endPointPosition;
 
 203             ar.endPointPosition = resourceRequest.endPointPosition;
 
 205             ar.resourceUnionId = resourceEntity.resourceEntityType + "::" + resourceEntity.resourceEntityId;
 
 207         ar.resourceSetId = ar.resourceUnionId + "::" + resourceEntity.resourceEntityVersion;
 
 208         ar.resourceName = rangeRule.rangeName;
 
 209         if (resourceRequest.resourceShareGroup != null) {
 
 210             ar.resourceShareGroupList = Collections.singleton(resourceRequest.resourceShareGroup);
 
 212         ar.assetId = resourceTarget.resourceTargetType + "::" + resourceTarget.resourceTargetId;
 
 213         ar.requestedNumbers = StrUtil.listInt(resourceRequest.rangeRequestedNumbers,
 
 214                 "Invalid value for requested-numbers");
 
 215         if (ar.requestedNumbers != null) {
 
 216             ar.requestedCount = ar.requestedNumbers.size();
 
 218         ar.excludeNumbers = StrUtil.listInt(resourceRequest.rangeExcludeNumbers, "Invalid value for exclude-numbers");
 
 219         ar.reverseOrder = resourceRequest.rangeReverseOrder;
 
 220         ar.missingResourceAction = AllocationAction.Succeed_Allocate;
 
 221         ar.expiredResourceAction = AllocationAction.Succeed_Allocate;
 
 222         ar.replace = resourceRequest.replace;
 
 224         ar.allocate = !checkOnly;
 
 225         ar.rangeList = rangeRule.rangeList;
 
 226         if (ar.rangeList == null || ar.rangeList.isEmpty()) {
 
 227             if (resourceRequest.rangeMinOverride >= 0
 
 228                     && resourceRequest.rangeMaxOverride >= resourceRequest.rangeMinOverride) {
 
 229                 ar.rangeList = new ArrayList<>();
 
 230                 Range range = new Range();
 
 231                 range.min = resourceRequest.rangeMinOverride;
 
 232                 range.max = resourceRequest.rangeMaxOverride;
 
 233                 ar.rangeList.add(range);
 
 236             if (resourceRequest.rangeMinOverride >= 0) {
 
 237                 ar.rangeList.get(0).min = resourceRequest.rangeMinOverride;
 
 239             if (resourceRequest.rangeMaxOverride >= 0) {
 
 240                 ar.rangeList.get(ar.rangeList.size() - 1).max = resourceRequest.rangeMaxOverride;
 
 244         ar.forceNewNumbers = resourceRequest.rangeForceNewNumbers;
 
 245         ar.nextInSequence = resourceRequest.rangeNextInSequence;
 
 249     public void setResourceRuleDao(ResourceRuleDao resourceRuleDao) {
 
 250         this.resourceRuleDao = resourceRuleDao;
 
 253     public void setRangeRuleDao(RangeRuleDao rangeRuleDao) {
 
 254         this.rangeRuleDao = rangeRuleDao;