2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
8 * Modifications Copyright (C) 2019 IBM.
9 * ================================================================================
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 * ============LICENSE_END=========================================================
24 package org.onap.ccsdk.sli.adaptors.ra.comp;
26 import java.util.ArrayList;
27 import java.util.Collections;
28 import java.util.Date;
29 import java.util.HashMap;
30 import java.util.List;
32 import org.onap.ccsdk.sli.adaptors.rm.comp.ResourceManager;
33 import org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem;
34 import org.onap.ccsdk.sli.adaptors.rm.data.AllocationOutcome;
35 import org.onap.ccsdk.sli.adaptors.rm.data.AllocationRequest;
36 import org.onap.ccsdk.sli.adaptors.rm.data.AllocationStatus;
37 import org.onap.ccsdk.sli.adaptors.rm.data.LimitAllocationItem;
38 import org.onap.ccsdk.sli.adaptors.rm.data.LimitAllocationOutcome;
39 import org.onap.ccsdk.sli.adaptors.rm.data.LimitResource;
40 import org.onap.ccsdk.sli.adaptors.rm.data.MultiResourceAllocationOutcome;
41 import org.onap.ccsdk.sli.adaptors.rm.data.RangeAllocationItem;
42 import org.onap.ccsdk.sli.adaptors.rm.data.RangeAllocationOutcome;
43 import org.onap.ccsdk.sli.adaptors.rm.data.RangeResource;
44 import org.onap.ccsdk.sli.adaptors.rm.data.ReleaseRequest;
45 import org.onap.ccsdk.sli.adaptors.rm.data.Resource;
46 import org.onap.ccsdk.sli.adaptors.util.str.StrUtil;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
50 public class EndPointAllocatorImpl implements EndPointAllocator {
52 private static final Logger log = LoggerFactory.getLogger(EndPointAllocatorImpl.class);
54 private ResourceManager resourceManager;
56 private Map<String, List<AllocationRule>> allocationRuleMap;
59 public List<ResourceData> allocateResources(String serviceModel, ResourceEntity resourceEntity,
60 ResourceTarget resourceTarget, ResourceRequest resourceRequest, boolean checkOnly, boolean change) {
62 List<ResourceData> resourceList = new ArrayList<>();
64 if (allocationRuleMap != null) {
65 List<AllocationRule> allocationRuleList = allocationRuleMap.get(serviceModel);
66 if (allocationRuleList == null) {
67 allocationRuleList = allocationRuleMap.get("DEFAULT");
70 if (allocationRuleList != null) {
71 boolean allgood = true;
72 for (AllocationRule allocationRule : allocationRuleList) {
73 AllocationRequest ar = allocationRule.buildAllocationRequest(serviceModel, resourceEntity,
74 resourceTarget, resourceRequest, checkOnly, change);
76 AllocationOutcome ao = resourceManager.allocateResources(ar);
77 List<ResourceData> rr = getResourceData(ao);
78 resourceList.addAll(rr);
80 if (ao.status != AllocationStatus.Success) {
87 String resourceSetId = resourceEntity.resourceEntityType + "::" + resourceEntity.resourceEntityId
88 + "::" + resourceEntity.resourceEntityVersion;
89 resourceManager.releaseResources(ReleaseRequest.resourceSet(resourceSetId));
97 private List<ResourceData> getResourceData(AllocationOutcome ao) {
98 if (ao instanceof MultiResourceAllocationOutcome) {
99 List<ResourceData> rr = new ArrayList<>();
100 for (AllocationOutcome ao1 : ((MultiResourceAllocationOutcome) ao).allocationOutcomeList) {
101 rr.addAll(getResourceData(ao1));
106 ResourceData rd = new ResourceData();
107 rd.data = new HashMap<>();
109 AllocationRequest ar = ao.request;
110 rd.resourceName = ar.resourceName;
111 rd.endPointPosition = ar.endPointPosition;
112 int i1 = ar.assetId.indexOf("::");
114 rd.resourceTargetType = ar.assetId.substring(0, i1);
115 rd.resourceTargetId = ar.assetId.substring(i1 + 2);
117 rd.resourceTargetType = "";
118 rd.resourceTargetId = ar.assetId;
120 rd.status = ao.status.toString();
122 if (ao instanceof LimitAllocationOutcome) {
123 LimitAllocationOutcome lao = (LimitAllocationOutcome) ao;
124 rd.data.put("allocated", String.valueOf(lao.allocatedCount));
125 rd.data.put("used", String.valueOf(lao.used));
126 rd.data.put("limit", String.valueOf(lao.limit));
127 rd.data.put("available", String.valueOf(lao.limit - lao.used));
128 } else if (ao instanceof RangeAllocationOutcome) {
129 RangeAllocationOutcome rao = (RangeAllocationOutcome) ao;
130 rd.data.put("allocated", String.valueOf(StrUtil.listInt(rao.allocated)));
131 rd.data.put("used", String.valueOf(StrUtil.listInt(rao.used)));
134 return Collections.singletonList(rd);
138 public List<ResourceData> getResourcesForEntity(String resourceEntityType, String resourceEntityId,
139 String resourceEntityVersion) {
140 List<ResourceData> rdlist = new ArrayList<>();
142 String resourceUnionId = resourceEntityType + "::" + resourceEntityId;
143 List<Resource> rlist = resourceManager.getResourceUnion(resourceUnionId);
145 for (Resource r : rlist) {
147 // Find the needed allocation item: if resourceEntityVersion is specified, use that,
148 // otherwise, find the latest allocation item
149 AllocationItem ai = null;
150 if (resourceEntityVersion != null) {
151 String resourceSetId = resourceUnionId + "::" + resourceEntityVersion;
152 for (AllocationItem ai1 : r.allocationItems) {
153 if (ai1.resourceSetId.equals(resourceSetId)) {
160 for (AllocationItem ai1 : r.allocationItems) {
161 if (ai1.resourceUnionId.equals(resourceUnionId)) {
162 if (aitime == null || ai1.allocationTime.after(aitime)) {
164 aitime = ai1.allocationTime;
171 ResourceData rd = new ResourceData();
174 rd.resourceName = r.resourceKey.resourceName;
175 int i1 = r.resourceKey.assetId.indexOf("::");
177 rd.resourceTargetType = r.resourceKey.assetId.substring(0, i1);
178 rd.resourceTargetId = r.resourceKey.assetId.substring(i1 + 2);
180 int i2 = r.resourceKey.assetId.lastIndexOf("::");
182 rd.resourceTargetValue = r.resourceKey.assetId.substring(i2 + 2);
185 rd.resourceTargetType = "";
186 rd.resourceTargetId = r.resourceKey.assetId;
189 rd.data = new HashMap<>();
191 if (ai instanceof RangeAllocationItem) {
192 RangeAllocationItem rai = (RangeAllocationItem) ai;
194 String ss = String.valueOf(rai.used);
195 ss = ss.substring(1, ss.length() - 1);
196 rd.data.put("allocated", ss);
198 } else if (ai instanceof LimitAllocationItem) {
199 LimitAllocationItem lai = (LimitAllocationItem) ai;
201 rd.data.put("allocated", String.valueOf(lai.used));
210 public List<ResourceData> getResourcesForTarget(String resourceTargetTypeFilter, String resourceTargetIdFilter,
211 String resourceName) {
212 List<ResourceData> rdlist = new ArrayList<>();
214 String assetIdFilter = null;
215 if (resourceTargetTypeFilter != null && resourceTargetIdFilter != null) {
216 assetIdFilter = resourceTargetTypeFilter + "::" + resourceTargetIdFilter;
217 } else if (resourceTargetTypeFilter != null) {
218 assetIdFilter = resourceTargetTypeFilter;
219 } else if (resourceTargetIdFilter != null) {
220 assetIdFilter = resourceTargetIdFilter;
223 List<Resource> rlist = resourceManager.queryResources(resourceName, assetIdFilter);
225 for (Resource r : rlist) {
226 if (r.allocationItems == null || r.allocationItems.isEmpty()) {
230 log.info("ResourceName:" + r.resourceKey.resourceName + " assetId:" + r.resourceKey.assetId);
233 rdlist.add(getResourceData(r));
240 public ResourceData getResource(String resourceTargetType, String resourceTargetId, String resourceName,
241 String resourceEntityTypeFilter, String resourceEntityIdFilter, String resourceShareGroupFilter) {
242 String assetId = resourceTargetType + "::" + resourceTargetId;
244 String resourceUnionFilter = null;
245 if (resourceEntityTypeFilter != null && resourceEntityIdFilter != null) {
246 resourceUnionFilter = resourceEntityTypeFilter + "::" + resourceEntityIdFilter;
247 } else if (resourceEntityTypeFilter != null) {
248 resourceUnionFilter = resourceEntityTypeFilter;
249 } else if (resourceEntityIdFilter != null) {
250 resourceUnionFilter = resourceEntityIdFilter;
254 if (resourceUnionFilter != null || resourceShareGroupFilter != null) {
255 r = resourceManager.queryResource(resourceName, assetId, resourceUnionFilter, resourceShareGroupFilter);
257 r = resourceManager.getResource(resourceName, assetId);
260 if (r != null && r.allocationItems != null && !r.allocationItems.isEmpty()) {
261 log.info("ResourceName:" + r.resourceKey.resourceName + " assetId:" + r.resourceKey.assetId);
262 return getResourceData(r);
268 private ResourceData getResourceData(Resource r) {
269 ResourceData rd = new ResourceData();
271 rd.resourceName = r.resourceKey.resourceName;
272 int i1 = r.resourceKey.assetId.indexOf("::");
274 rd.resourceTargetType = r.resourceKey.assetId.substring(0, i1);
275 rd.resourceTargetId = r.resourceKey.assetId.substring(i1 + 2);
277 int i2 = r.resourceKey.assetId.lastIndexOf("::");
279 rd.resourceTargetValue = r.resourceKey.assetId.substring(i2 + 2);
282 rd.resourceTargetType = "";
283 rd.resourceTargetId = r.resourceKey.assetId;
286 rd.data = new HashMap<>();
288 if (r instanceof RangeResource) {
289 RangeResource rr = (RangeResource) r;
291 log.info("rr.used: " + rr.used);
292 String ss = String.valueOf(rr.used);
293 ss = ss.substring(1, ss.length() - 1);
294 rd.data.put("allocated", ss);
296 } else if (r instanceof LimitResource) {
297 LimitResource lr = (LimitResource) r;
299 log.info("lr.used: " + lr.used);
300 rd.data.put("allocated", String.valueOf(lr.used));
303 rd.allocationDataList = new ArrayList<>();
305 if (r.allocationItems != null) {
306 for (AllocationItem ai : r.allocationItems) {
307 AllocationData ad = new AllocationData();
308 rd.allocationDataList.add(ad);
310 i1 = ai.resourceUnionId.indexOf("::");
312 ad.resourceEntityType = ai.resourceUnionId.substring(0, i1);
313 ad.resourceEntityId = ai.resourceUnionId.substring(i1 + 2);
315 ad.resourceEntityType = "";
316 ad.resourceEntityId = ai.resourceUnionId;
319 i1 = ai.resourceSetId.lastIndexOf("::");
321 ad.resourceEntityVersion = ai.resourceSetId.substring(i1 + 2);
323 ad.resourceEntityVersion = "";
326 ad.data = new HashMap<>();
328 if (ai instanceof RangeAllocationItem) {
329 RangeAllocationItem rai = (RangeAllocationItem) ai;
331 log.info("rr.used: " + rai.used);
332 String ss = String.valueOf(rai.used);
333 ss = ss.substring(1, ss.length() - 1);
334 ad.data.put("allocated", ss);
336 } else if (ai instanceof LimitAllocationItem) {
337 LimitAllocationItem lai = (LimitAllocationItem) ai;
339 log.info("lr.used: " + lai.used);
340 ad.data.put("allocated", String.valueOf(lai.used));
348 public void setResourceManager(ResourceManager resourceManager) {
349 this.resourceManager = resourceManager;
352 public void setAllocationRuleMap(Map<String, List<AllocationRule>> allocationRuleMap) {
353 this.allocationRuleMap = allocationRuleMap;