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.Range;
42 import org.onap.ccsdk.sli.adaptors.rm.data.RangeAllocationItem;
43 import org.onap.ccsdk.sli.adaptors.rm.data.RangeAllocationOutcome;
44 import org.onap.ccsdk.sli.adaptors.rm.data.RangeAllocationRequest;
45 import org.onap.ccsdk.sli.adaptors.rm.data.RangeResource;
46 import org.onap.ccsdk.sli.adaptors.rm.data.ReleaseRequest;
47 import org.onap.ccsdk.sli.adaptors.rm.data.Resource;
48 import org.onap.ccsdk.sli.adaptors.util.str.StrUtil;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
52 public class EndPointAllocatorImpl implements EndPointAllocator {
54 private static final Logger log = LoggerFactory.getLogger(EndPointAllocatorImpl.class);
56 private ResourceManager resourceManager;
58 private Map<String, List<AllocationRule>> allocationRuleMap;
61 public List<ResourceData> allocateResources(String serviceModel, ResourceEntity resourceEntity,
62 ResourceTarget resourceTarget, ResourceRequest resourceRequest, boolean checkOnly, boolean change) {
64 List<ResourceData> resourceList = new ArrayList<>();
66 if (allocationRuleMap != null) {
67 List<AllocationRule> allocationRuleList = allocationRuleMap.get(serviceModel);
68 if (allocationRuleList == null) {
69 allocationRuleList = allocationRuleMap.get("DEFAULT");
72 if (allocationRuleList != null) {
73 boolean allgood = true;
74 for (AllocationRule allocationRule : allocationRuleList) {
75 AllocationRequest ar = allocationRule.buildAllocationRequest(serviceModel, resourceEntity,
76 resourceTarget, resourceRequest, checkOnly, change);
78 AllocationOutcome ao = resourceManager.allocateResources(ar);
79 List<ResourceData> rr = getResourceData(ao);
80 resourceList.addAll(rr);
82 if (ao.status != AllocationStatus.Success) {
89 String resourceSetId = resourceEntity.resourceEntityType + "::" + resourceEntity.resourceEntityId
90 + "::" + resourceEntity.resourceEntityVersion;
91 resourceManager.releaseResources(ReleaseRequest.resourceSet(resourceSetId));
99 private List<ResourceData> getResourceData(AllocationOutcome ao) {
100 if (ao instanceof MultiResourceAllocationOutcome) {
101 List<ResourceData> rr = new ArrayList<>();
102 for (AllocationOutcome ao1 : ((MultiResourceAllocationOutcome) ao).allocationOutcomeList) {
103 rr.addAll(getResourceData(ao1));
108 ResourceData rd = new ResourceData();
109 rd.data = new HashMap<>();
111 AllocationRequest ar = ao.request;
112 rd.resourceName = ar.resourceName;
113 rd.endPointPosition = ar.endPointPosition;
114 int i1 = ar.assetId.indexOf("::");
116 rd.resourceTargetType = ar.assetId.substring(0, i1);
117 rd.resourceTargetId = ar.assetId.substring(i1 + 2);
119 rd.resourceTargetType = "";
120 rd.resourceTargetId = ar.assetId;
122 rd.status = ao.status.toString();
124 if (ao instanceof LimitAllocationOutcome) {
125 LimitAllocationOutcome lao = (LimitAllocationOutcome) ao;
126 rd.data.put("allocated", String.valueOf(lao.allocatedCount));
127 rd.data.put("used", String.valueOf(lao.used));
128 rd.data.put("limit", String.valueOf(lao.limit));
129 rd.data.put("available", String.valueOf(lao.limit - lao.used));
130 } else if (ao instanceof RangeAllocationOutcome) {
131 RangeAllocationOutcome rao = (RangeAllocationOutcome) ao;
132 rd.data.put("allocated", String.valueOf(StrUtil.listInt(rao.allocated)));
133 rd.data.put("used", String.valueOf(StrUtil.listInt(rao.used)));
134 List<Range> rangeList = ((RangeAllocationRequest) rao.request).rangeList;
135 if (rangeList != null && !rangeList.isEmpty()) {
136 List<Object> ll = new ArrayList<>();
137 for (Range r : rangeList) {
138 Map<String, Object> mm = new HashMap<>();
139 mm.put("min", r.min);
140 mm.put("max", r.max);
143 rd.data.put("range-list", ll);
147 return Collections.singletonList(rd);
151 public List<ResourceData> getResourcesForEntity(String resourceEntityType, String resourceEntityId,
152 String resourceEntityVersion) {
153 List<ResourceData> rdlist = new ArrayList<>();
155 String resourceUnionId = resourceEntityType + "::" + resourceEntityId;
156 List<Resource> rlist = resourceManager.getResourceUnion(resourceUnionId);
158 for (Resource r : rlist) {
160 // Find the needed allocation item: if resourceEntityVersion is specified, use that,
161 // otherwise, find the latest allocation item
162 AllocationItem ai = null;
163 if (resourceEntityVersion != null) {
164 String resourceSetId = resourceUnionId + "::" + resourceEntityVersion;
165 for (AllocationItem ai1 : r.allocationItems) {
166 if (ai1.resourceSetId.equals(resourceSetId)) {
173 for (AllocationItem ai1 : r.allocationItems) {
174 if (ai1.resourceUnionId.equals(resourceUnionId)) {
175 if (aitime == null || ai1.allocationTime.after(aitime)) {
177 aitime = ai1.allocationTime;
184 ResourceData rd = new ResourceData();
187 rd.resourceName = r.resourceKey.resourceName;
188 int i1 = r.resourceKey.assetId.indexOf("::");
190 rd.resourceTargetType = r.resourceKey.assetId.substring(0, i1);
191 rd.resourceTargetId = r.resourceKey.assetId.substring(i1 + 2);
193 int i2 = r.resourceKey.assetId.lastIndexOf("::");
195 rd.resourceTargetValue = r.resourceKey.assetId.substring(i2 + 2);
198 rd.resourceTargetType = "";
199 rd.resourceTargetId = r.resourceKey.assetId;
202 rd.data = new HashMap<>();
204 if (ai instanceof RangeAllocationItem) {
205 RangeAllocationItem rai = (RangeAllocationItem) ai;
207 String ss = String.valueOf(rai.used);
208 ss = ss.substring(1, ss.length() - 1);
209 rd.data.put("allocated", ss);
211 } else if (ai instanceof LimitAllocationItem) {
212 LimitAllocationItem lai = (LimitAllocationItem) ai;
214 rd.data.put("allocated", String.valueOf(lai.used));
223 public List<ResourceData> getResourcesForTarget(String resourceTargetTypeFilter, String resourceTargetIdFilter,
224 String resourceName) {
225 List<ResourceData> rdlist = new ArrayList<>();
227 String assetIdFilter = null;
228 if (resourceTargetTypeFilter != null && resourceTargetIdFilter != null) {
229 assetIdFilter = resourceTargetTypeFilter + "::" + resourceTargetIdFilter;
230 } else if (resourceTargetTypeFilter != null) {
231 assetIdFilter = resourceTargetTypeFilter;
232 } else if (resourceTargetIdFilter != null) {
233 assetIdFilter = resourceTargetIdFilter;
236 List<Resource> rlist = resourceManager.queryResources(resourceName, assetIdFilter);
238 for (Resource r : rlist) {
239 if (r.allocationItems == null || r.allocationItems.isEmpty()) {
243 log.info("ResourceName:" + r.resourceKey.resourceName + " assetId:" + r.resourceKey.assetId);
246 rdlist.add(getResourceData(r));
253 public ResourceData getResource(String resourceTargetType, String resourceTargetId, String resourceName,
254 String resourceEntityTypeFilter, String resourceEntityIdFilter, String resourceShareGroupFilter) {
255 String assetId = resourceTargetType + "::" + resourceTargetId;
257 String resourceUnionFilter = null;
258 if (resourceEntityTypeFilter != null && resourceEntityIdFilter != null) {
259 resourceUnionFilter = resourceEntityTypeFilter + "::" + resourceEntityIdFilter;
260 } else if (resourceEntityTypeFilter != null) {
261 resourceUnionFilter = resourceEntityTypeFilter;
262 } else if (resourceEntityIdFilter != null) {
263 resourceUnionFilter = resourceEntityIdFilter;
267 if (resourceUnionFilter != null || resourceShareGroupFilter != null) {
268 r = resourceManager.queryResource(resourceName, assetId, resourceUnionFilter, resourceShareGroupFilter);
270 r = resourceManager.getResource(resourceName, assetId);
273 if (r != null && r.allocationItems != null && !r.allocationItems.isEmpty()) {
274 log.info("ResourceName:" + r.resourceKey.resourceName + " assetId:" + r.resourceKey.assetId);
275 return getResourceData(r);
281 private ResourceData getResourceData(Resource r) {
282 ResourceData rd = new ResourceData();
284 rd.resourceName = r.resourceKey.resourceName;
285 int i1 = r.resourceKey.assetId.indexOf("::");
287 rd.resourceTargetType = r.resourceKey.assetId.substring(0, i1);
288 rd.resourceTargetId = r.resourceKey.assetId.substring(i1 + 2);
290 int i2 = r.resourceKey.assetId.lastIndexOf("::");
292 rd.resourceTargetValue = r.resourceKey.assetId.substring(i2 + 2);
295 rd.resourceTargetType = "";
296 rd.resourceTargetId = r.resourceKey.assetId;
299 rd.data = new HashMap<>();
301 if (r instanceof RangeResource) {
302 RangeResource rr = (RangeResource) r;
304 log.info("rr.used: " + rr.used);
305 String ss = String.valueOf(rr.used);
306 ss = ss.substring(1, ss.length() - 1);
307 rd.data.put("allocated", ss);
309 } else if (r instanceof LimitResource) {
310 LimitResource lr = (LimitResource) r;
312 log.info("lr.used: " + lr.used);
313 rd.data.put("allocated", String.valueOf(lr.used));
316 rd.allocationDataList = new ArrayList<>();
318 if (r.allocationItems != null) {
319 for (AllocationItem ai : r.allocationItems) {
320 AllocationData ad = new AllocationData();
321 rd.allocationDataList.add(ad);
323 i1 = ai.resourceUnionId.indexOf("::");
325 ad.resourceEntityType = ai.resourceUnionId.substring(0, i1);
326 ad.resourceEntityId = ai.resourceUnionId.substring(i1 + 2);
328 ad.resourceEntityType = "";
329 ad.resourceEntityId = ai.resourceUnionId;
332 i1 = ai.resourceSetId.lastIndexOf("::");
334 ad.resourceEntityVersion = ai.resourceSetId.substring(i1 + 2);
336 ad.resourceEntityVersion = "";
339 ad.data = new HashMap<>();
341 if (ai instanceof RangeAllocationItem) {
342 RangeAllocationItem rai = (RangeAllocationItem) ai;
344 log.info("rr.used: " + rai.used);
345 String ss = String.valueOf(rai.used);
346 ss = ss.substring(1, ss.length() - 1);
347 ad.data.put("allocated", ss);
349 } else if (ai instanceof LimitAllocationItem) {
350 LimitAllocationItem lai = (LimitAllocationItem) ai;
352 log.info("lr.used: " + lai.used);
353 ad.data.put("allocated", String.valueOf(lai.used));
361 public void setResourceManager(ResourceManager resourceManager) {
362 this.resourceManager = resourceManager;
365 public void setAllocationRuleMap(Map<String, List<AllocationRule>> allocationRuleMap) {
366 this.allocationRuleMap = allocationRuleMap;