36e40d5899dd0865a03364095ab252df33ddd358
[ccsdk/sli.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
7  *
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
13  *
14  *      http://www.apache.org/licenses/LICENSE-2.0
15  *
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=========================================================
22  */
23
24 package org.onap.ccsdk.sli.adaptors.ra.comp;
25
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;
31 import java.util.Map;
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;
51
52 public class EndPointAllocatorImpl implements EndPointAllocator {
53
54     private static final Logger log = LoggerFactory.getLogger(EndPointAllocatorImpl.class);
55
56     private ResourceManager resourceManager;
57
58     private Map<String, List<AllocationRule>> allocationRuleMap;
59
60     @Override
61     public List<ResourceData> allocateResources(String serviceModel, ResourceEntity resourceEntity,
62             ResourceTarget resourceTarget, ResourceRequest resourceRequest, boolean checkOnly, boolean change) {
63
64         List<ResourceData> resourceList = new ArrayList<>();
65
66         if (allocationRuleMap != null) {
67             List<AllocationRule> allocationRuleList = allocationRuleMap.get(serviceModel);
68             if (allocationRuleList == null) {
69                 allocationRuleList = allocationRuleMap.get("DEFAULT");
70             }
71
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);
77                     if (ar != null) {
78                         AllocationOutcome ao = resourceManager.allocateResources(ar);
79                         List<ResourceData> rr = getResourceData(ao);
80                         resourceList.addAll(rr);
81
82                         if (ao.status != AllocationStatus.Success) {
83                             allgood = false;
84                         }
85                     }
86                 }
87
88                 if (!allgood) {
89                     String resourceSetId = resourceEntity.resourceEntityType + "::" + resourceEntity.resourceEntityId
90                             + "::" + resourceEntity.resourceEntityVersion;
91                     resourceManager.releaseResources(ReleaseRequest.resourceSet(resourceSetId));
92                 }
93             }
94         }
95
96         return resourceList;
97     }
98
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));
104             }
105             return rr;
106         }
107
108         ResourceData rd = new ResourceData();
109         rd.data = new HashMap<>();
110
111         AllocationRequest ar = ao.request;
112         rd.resourceName = ar.resourceName;
113         rd.endPointPosition = ar.endPointPosition;
114         int i1 = ar.assetId.indexOf("::");
115         if (i1 > 0) {
116             rd.resourceTargetType = ar.assetId.substring(0, i1);
117             rd.resourceTargetId = ar.assetId.substring(i1 + 2);
118         } else {
119             rd.resourceTargetType = "";
120             rd.resourceTargetId = ar.assetId;
121         }
122         rd.status = ao.status.toString();
123
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);
141                     ll.add(mm);
142                 }
143                 rd.data.put("range-list", ll);
144             }
145         }
146
147         return Collections.singletonList(rd);
148     }
149
150     @Override
151     public List<ResourceData> getResourcesForEntity(String resourceEntityType, String resourceEntityId,
152             String resourceEntityVersion) {
153         List<ResourceData> rdlist = new ArrayList<>();
154
155         String resourceUnionId = resourceEntityType + "::" + resourceEntityId;
156         List<Resource> rlist = resourceManager.getResourceUnion(resourceUnionId);
157
158         for (Resource r : rlist) {
159
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)) {
167                         ai = ai1;
168                         break;
169                     }
170                 }
171             } else {
172                 Date aitime = null;
173                 for (AllocationItem ai1 : r.allocationItems) {
174                     if (ai1.resourceUnionId.equals(resourceUnionId)) {
175                         if (aitime == null || ai1.allocationTime.after(aitime)) {
176                             ai = ai1;
177                             aitime = ai1.allocationTime;
178                         }
179                     }
180                 }
181             }
182
183             if (ai != null) {
184                 ResourceData rd = new ResourceData();
185                 rdlist.add(rd);
186
187                 rd.resourceName = r.resourceKey.resourceName;
188                 int i1 = r.resourceKey.assetId.indexOf("::");
189                 if (i1 > 0) {
190                     rd.resourceTargetType = r.resourceKey.assetId.substring(0, i1);
191                     rd.resourceTargetId = r.resourceKey.assetId.substring(i1 + 2);
192
193                     int i2 = r.resourceKey.assetId.lastIndexOf("::");
194                     if (i2 > i1) {
195                         rd.resourceTargetValue = r.resourceKey.assetId.substring(i2 + 2);
196                     }
197                 } else {
198                     rd.resourceTargetType = "";
199                     rd.resourceTargetId = r.resourceKey.assetId;
200                 }
201
202                 rd.data = new HashMap<>();
203
204                 if (ai instanceof RangeAllocationItem) {
205                     RangeAllocationItem rai = (RangeAllocationItem) ai;
206
207                     String ss = String.valueOf(rai.used);
208                     ss = ss.substring(1, ss.length() - 1);
209                     rd.data.put("allocated", ss);
210
211                 } else if (ai instanceof LimitAllocationItem) {
212                     LimitAllocationItem lai = (LimitAllocationItem) ai;
213
214                     rd.data.put("allocated", String.valueOf(lai.used));
215                 }
216             }
217         }
218
219         return rdlist;
220     }
221
222     @Override
223     public List<ResourceData> getResourcesForTarget(String resourceTargetTypeFilter, String resourceTargetIdFilter,
224             String resourceName) {
225         List<ResourceData> rdlist = new ArrayList<>();
226
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;
234         }
235
236         List<Resource> rlist = resourceManager.queryResources(resourceName, assetIdFilter);
237
238         for (Resource r : rlist) {
239             if (r.allocationItems == null || r.allocationItems.isEmpty()) {
240                 continue;
241             }
242
243             log.info("ResourceName:" + r.resourceKey.resourceName + " assetId:" + r.resourceKey.assetId);
244
245
246             rdlist.add(getResourceData(r));
247         }
248
249         return rdlist;
250     }
251
252     @Override
253     public ResourceData getResource(String resourceTargetType, String resourceTargetId, String resourceName,
254             String resourceEntityTypeFilter, String resourceEntityIdFilter, String resourceShareGroupFilter) {
255         String assetId = resourceTargetType + "::" + resourceTargetId;
256
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;
264         }
265
266         Resource r = null;
267         if (resourceUnionFilter != null || resourceShareGroupFilter != null) {
268             r = resourceManager.queryResource(resourceName, assetId, resourceUnionFilter, resourceShareGroupFilter);
269         } else {
270             r = resourceManager.getResource(resourceName, assetId);
271         }
272
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);
276         }
277
278         return null;
279     }
280
281     private ResourceData getResourceData(Resource r) {
282         ResourceData rd = new ResourceData();
283
284         rd.resourceName = r.resourceKey.resourceName;
285         int i1 = r.resourceKey.assetId.indexOf("::");
286         if (i1 > 0) {
287             rd.resourceTargetType = r.resourceKey.assetId.substring(0, i1);
288             rd.resourceTargetId = r.resourceKey.assetId.substring(i1 + 2);
289
290             int i2 = r.resourceKey.assetId.lastIndexOf("::");
291             if (i2 > i1) {
292                 rd.resourceTargetValue = r.resourceKey.assetId.substring(i2 + 2);
293             }
294         } else {
295             rd.resourceTargetType = "";
296             rd.resourceTargetId = r.resourceKey.assetId;
297         }
298
299         rd.data = new HashMap<>();
300
301         if (r instanceof RangeResource) {
302             RangeResource rr = (RangeResource) r;
303
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);
308
309         } else if (r instanceof LimitResource) {
310             LimitResource lr = (LimitResource) r;
311
312             log.info("lr.used: " + lr.used);
313             rd.data.put("allocated", String.valueOf(lr.used));
314         }
315
316         rd.allocationDataList = new ArrayList<>();
317
318         if (r.allocationItems != null) {
319             for (AllocationItem ai : r.allocationItems) {
320                 AllocationData ad = new AllocationData();
321                 rd.allocationDataList.add(ad);
322
323                 i1 = ai.resourceUnionId.indexOf("::");
324                 if (i1 > 0) {
325                     ad.resourceEntityType = ai.resourceUnionId.substring(0, i1);
326                     ad.resourceEntityId = ai.resourceUnionId.substring(i1 + 2);
327                 } else {
328                     ad.resourceEntityType = "";
329                     ad.resourceEntityId = ai.resourceUnionId;
330                 }
331
332                 i1 = ai.resourceSetId.lastIndexOf("::");
333                 if (i1 > 0) {
334                     ad.resourceEntityVersion = ai.resourceSetId.substring(i1 + 2);
335                 } else {
336                     ad.resourceEntityVersion = "";
337                 }
338
339                 ad.data = new HashMap<>();
340
341                 if (ai instanceof RangeAllocationItem) {
342                     RangeAllocationItem rai = (RangeAllocationItem) ai;
343
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);
348
349                 } else if (ai instanceof LimitAllocationItem) {
350                     LimitAllocationItem lai = (LimitAllocationItem) ai;
351
352                     log.info("lr.used: " + lai.used);
353                     ad.data.put("allocated", String.valueOf(lai.used));
354                 }
355             }
356         }
357
358         return rd;
359     }
360
361     public void setResourceManager(ResourceManager resourceManager) {
362         this.resourceManager = resourceManager;
363     }
364
365     public void setAllocationRuleMap(Map<String, List<AllocationRule>> allocationRuleMap) {
366         this.allocationRuleMap = allocationRuleMap;
367     }
368 }