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.comp;
 
  24 import java.util.ArrayList;
 
  25 import java.util.Collections;
 
  26 import java.util.Date;
 
  27 import java.util.HashMap;
 
  28 import java.util.List;
 
  30 import org.onap.ccsdk.sli.adaptors.rm.comp.ResourceManager;
 
  31 import org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem;
 
  32 import org.onap.ccsdk.sli.adaptors.rm.data.AllocationOutcome;
 
  33 import org.onap.ccsdk.sli.adaptors.rm.data.AllocationRequest;
 
  34 import org.onap.ccsdk.sli.adaptors.rm.data.AllocationStatus;
 
  35 import org.onap.ccsdk.sli.adaptors.rm.data.LimitAllocationItem;
 
  36 import org.onap.ccsdk.sli.adaptors.rm.data.LimitAllocationOutcome;
 
  37 import org.onap.ccsdk.sli.adaptors.rm.data.LimitResource;
 
  38 import org.onap.ccsdk.sli.adaptors.rm.data.MultiResourceAllocationOutcome;
 
  39 import org.onap.ccsdk.sli.adaptors.rm.data.RangeAllocationItem;
 
  40 import org.onap.ccsdk.sli.adaptors.rm.data.RangeAllocationOutcome;
 
  41 import org.onap.ccsdk.sli.adaptors.rm.data.RangeResource;
 
  42 import org.onap.ccsdk.sli.adaptors.rm.data.ReleaseRequest;
 
  43 import org.onap.ccsdk.sli.adaptors.rm.data.Resource;
 
  44 import org.onap.ccsdk.sli.adaptors.util.str.StrUtil;
 
  45 import org.slf4j.Logger;
 
  46 import org.slf4j.LoggerFactory;
 
  48 public class EndPointAllocatorImpl implements EndPointAllocator {
 
  50     private static final Logger log = LoggerFactory.getLogger(EndPointAllocatorImpl.class);
 
  52     private ResourceManager resourceManager;
 
  54     private Map<String, List<AllocationRule>> allocationRuleMap;
 
  57     public List<ResourceData> allocateResources(String serviceModel, ResourceEntity resourceEntity,
 
  58             ResourceTarget resourceTarget, ResourceRequest resourceRequest, boolean checkOnly, boolean change) {
 
  60         List<ResourceData> resourceList = new ArrayList<>();
 
  62         if (allocationRuleMap != null) {
 
  63             List<AllocationRule> allocationRuleList = allocationRuleMap.get(serviceModel);
 
  64             if (allocationRuleList == null) {
 
  65                 allocationRuleList = allocationRuleMap.get("DEFAULT");
 
  68             if (allocationRuleList != null) {
 
  69                 boolean allgood = true;
 
  70                 for (AllocationRule allocationRule : allocationRuleList) {
 
  71                     AllocationRequest ar = allocationRule.buildAllocationRequest(serviceModel, resourceEntity,
 
  72                             resourceTarget, resourceRequest, checkOnly, change);
 
  74                         AllocationOutcome ao = resourceManager.allocateResources(ar);
 
  75                         List<ResourceData> rr = getResourceData(ao);
 
  76                         resourceList.addAll(rr);
 
  78                         if (ao.status != AllocationStatus.Success) {
 
  85                     String resourceSetId = resourceEntity.resourceEntityType + "::" + resourceEntity.resourceEntityId
 
  86                             + "::" + resourceEntity.resourceEntityVersion;
 
  87                     resourceManager.releaseResources(ReleaseRequest.resourceSet(resourceSetId));
 
  95     private List<ResourceData> getResourceData(AllocationOutcome ao) {
 
  96         if (ao instanceof MultiResourceAllocationOutcome) {
 
  97             List<ResourceData> rr = new ArrayList<>();
 
  98             for (AllocationOutcome ao1 : ((MultiResourceAllocationOutcome) ao).allocationOutcomeList) {
 
  99                 rr.addAll(getResourceData(ao1));
 
 104         ResourceData rd = new ResourceData();
 
 105         rd.data = new HashMap<>();
 
 107         AllocationRequest ar = ao.request;
 
 108         rd.resourceName = ar.resourceName;
 
 109         rd.endPointPosition = ar.endPointPosition;
 
 110         int i1 = ar.assetId.indexOf("::");
 
 112             rd.resourceTargetType = ar.assetId.substring(0, i1);
 
 113             rd.resourceTargetId = ar.assetId.substring(i1 + 2);
 
 115             rd.resourceTargetType = "";
 
 116             rd.resourceTargetId = ar.assetId;
 
 118         rd.status = ao.status.toString();
 
 120         if (ao instanceof LimitAllocationOutcome) {
 
 121             LimitAllocationOutcome lao = (LimitAllocationOutcome) ao;
 
 122             rd.data.put("allocated", String.valueOf(lao.allocatedCount));
 
 123             rd.data.put("used", String.valueOf(lao.used));
 
 124             rd.data.put("limit", String.valueOf(lao.limit));
 
 125             rd.data.put("available", String.valueOf(lao.limit - lao.used));
 
 126         } else if (ao instanceof RangeAllocationOutcome) {
 
 127             RangeAllocationOutcome rao = (RangeAllocationOutcome) ao;
 
 128             rd.data.put("allocated", String.valueOf(StrUtil.listInt(rao.allocated)));
 
 129             rd.data.put("used", String.valueOf(StrUtil.listInt(rao.used)));
 
 132         return Collections.singletonList(rd);
 
 136     public List<ResourceData> getResourcesForEntity(String resourceEntityType, String resourceEntityId,
 
 137             String resourceEntityVersion) {
 
 138         List<ResourceData> rdlist = new ArrayList<>();
 
 140         String resourceUnionId = resourceEntityType + "::" + resourceEntityId;
 
 141         List<Resource> rlist = resourceManager.getResourceUnion(resourceUnionId);
 
 143         for (Resource r : rlist) {
 
 145             // Find the needed allocation item: if resourceEntityVersion is specified, use that,
 
 146             // otherwise, find the latest allocation item
 
 147             AllocationItem ai = null;
 
 148             if (resourceEntityVersion != null) {
 
 149                 String resourceSetId = resourceUnionId + "::" + resourceEntityVersion;
 
 150                 for (AllocationItem ai1 : r.allocationItems) {
 
 151                     if (ai1.resourceSetId.equals(resourceSetId)) {
 
 158                 for (AllocationItem ai1 : r.allocationItems) {
 
 159                     if (ai1.resourceUnionId.equals(resourceUnionId)) {
 
 160                         if (aitime == null || ai1.allocationTime.after(aitime)) {
 
 162                             aitime = ai1.allocationTime;
 
 169                 ResourceData rd = new ResourceData();
 
 172                 rd.resourceName = r.resourceKey.resourceName;
 
 173                 int i1 = r.resourceKey.assetId.indexOf("::");
 
 175                     rd.resourceTargetType = r.resourceKey.assetId.substring(0, i1);
 
 176                     rd.resourceTargetId = r.resourceKey.assetId.substring(i1 + 2);
 
 178                     int i2 = r.resourceKey.assetId.lastIndexOf("::");
 
 180                         rd.resourceTargetValue = r.resourceKey.assetId.substring(i2 + 2);
 
 183                     rd.resourceTargetType = "";
 
 184                     rd.resourceTargetId = r.resourceKey.assetId;
 
 187                 rd.data = new HashMap<>();
 
 189                 if (ai instanceof RangeAllocationItem) {
 
 190                     RangeAllocationItem rai = (RangeAllocationItem) ai;
 
 192                     String ss = String.valueOf(rai.used);
 
 193                     ss = ss.substring(1, ss.length() - 1);
 
 194                     rd.data.put("allocated", ss);
 
 196                 } else if (ai instanceof LimitAllocationItem) {
 
 197                     LimitAllocationItem lai = (LimitAllocationItem) ai;
 
 199                     rd.data.put("allocated", String.valueOf(lai.used));
 
 208     public List<ResourceData> getResourcesForTarget(String resourceTargetTypeFilter, String resourceTargetIdFilter,
 
 209             String resourceName) {
 
 210         List<ResourceData> rdlist = new ArrayList<>();
 
 212         String assetIdFilter = null;
 
 213         if (resourceTargetTypeFilter != null && resourceTargetIdFilter != null) {
 
 214             assetIdFilter = resourceTargetTypeFilter + "::" + resourceTargetIdFilter;
 
 215         } else if (resourceTargetTypeFilter != null) {
 
 216             assetIdFilter = resourceTargetTypeFilter;
 
 217         } else if (resourceTargetIdFilter != null) {
 
 218             assetIdFilter = resourceTargetIdFilter;
 
 221         List<Resource> rlist = resourceManager.queryResources(resourceName, assetIdFilter);
 
 223         for (Resource r : rlist) {
 
 225             log.info("ResourceName:" + r.resourceKey.resourceName + " assetId:" + r.resourceKey.assetId);
 
 227             ResourceData rd = new ResourceData();
 
 230             rd.resourceName = r.resourceKey.resourceName;
 
 231             int i1 = r.resourceKey.assetId.indexOf("::");
 
 233                 rd.resourceTargetType = r.resourceKey.assetId.substring(0, i1);
 
 234                 rd.resourceTargetId = r.resourceKey.assetId.substring(i1 + 2);
 
 236                 int i2 = r.resourceKey.assetId.lastIndexOf("::");
 
 238                     rd.resourceTargetValue = r.resourceKey.assetId.substring(i2 + 2);
 
 241                 rd.resourceTargetType = "";
 
 242                 rd.resourceTargetId = r.resourceKey.assetId;
 
 245             rd.data = new HashMap<>();
 
 247             if (r instanceof RangeResource) {
 
 248                 RangeResource rr = (RangeResource) r;
 
 250                 log.info("rr.used: " + rr.used);
 
 251                 String ss = String.valueOf(rr.used);
 
 252                 ss = ss.substring(1, ss.length() - 1);
 
 253                 rd.data.put("allocated", ss);
 
 255             } else if (r instanceof LimitResource) {
 
 256                 LimitResource lr = (LimitResource) r;
 
 258                 log.info("lr.used: " + lr.used);
 
 259                 rd.data.put("allocated", String.valueOf(lr.used));
 
 267     public ResourceData getResource(String resourceTargetType, String resourceTargetId, String resourceName,
 
 268             String resourceEntityTypeFilter, String resourceEntityIdFilter, String resourceShareGroupFilter) {
 
 269         ResourceData rd = new ResourceData();
 
 270         String assetId = resourceTargetType + "::" + resourceTargetId;
 
 272         String resourceUnionFilter = null;
 
 273         if (resourceEntityTypeFilter != null && resourceEntityIdFilter != null) {
 
 274             resourceUnionFilter = resourceEntityTypeFilter + "::" + resourceEntityIdFilter;
 
 275         } else if (resourceEntityTypeFilter != null) {
 
 276             resourceUnionFilter = resourceEntityTypeFilter;
 
 277         } else if (resourceEntityIdFilter != null) {
 
 278             resourceUnionFilter = resourceEntityIdFilter;
 
 282         if (resourceUnionFilter != null || resourceShareGroupFilter != null) {
 
 283             r = resourceManager.queryResource(resourceName, assetId, resourceUnionFilter, resourceShareGroupFilter);
 
 285             r = resourceManager.getResource(resourceName, assetId);
 
 289             log.info("ResourceName:" + r.resourceKey.resourceName + " assetId:" + r.resourceKey.assetId);
 
 291             rd.resourceName = r.resourceKey.resourceName;
 
 292             int i1 = r.resourceKey.assetId.indexOf("::");
 
 294                 rd.resourceTargetType = r.resourceKey.assetId.substring(0, i1);
 
 295                 rd.resourceTargetId = r.resourceKey.assetId.substring(i1 + 2);
 
 297                 int i2 = r.resourceKey.assetId.lastIndexOf("::");
 
 299                     rd.resourceTargetValue = r.resourceKey.assetId.substring(i2 + 2);
 
 302                 rd.resourceTargetType = "";
 
 303                 rd.resourceTargetId = r.resourceKey.assetId;
 
 306             rd.data = new HashMap<>();
 
 308             if (r instanceof RangeResource) {
 
 309                 RangeResource rr = (RangeResource) r;
 
 311                 log.info("rr.used: " + rr.used);
 
 312                 String ss = String.valueOf(rr.used);
 
 313                 ss = ss.substring(1, ss.length() - 1);
 
 314                 rd.data.put("allocated", ss);
 
 316             } else if (r instanceof LimitResource) {
 
 317                 LimitResource lr = (LimitResource) r;
 
 319                 log.info("lr.used: " + lr.used);
 
 320                 rd.data.put("allocated", String.valueOf(lr.used));
 
 327     public void setResourceManager(ResourceManager resourceManager) {
 
 328         this.resourceManager = resourceManager;
 
 331     public void setAllocationRuleMap(Map<String, List<AllocationRule>> allocationRuleMap) {
 
 332         this.allocationRuleMap = allocationRuleMap;