83dab5418da281f5b365c303489841dd87cf3104
[ccsdk/sli/adaptors.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.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;
49
50 public class EndPointAllocatorImpl implements EndPointAllocator {
51
52     private static final Logger log = LoggerFactory.getLogger(EndPointAllocatorImpl.class);
53
54     private ResourceManager resourceManager;
55
56     private Map<String, List<AllocationRule>> allocationRuleMap;
57
58     @Override
59     public List<ResourceData> allocateResources(String serviceModel, ResourceEntity resourceEntity,
60             ResourceTarget resourceTarget, ResourceRequest resourceRequest, boolean checkOnly, boolean change) {
61
62         List<ResourceData> resourceList = new ArrayList<>();
63
64         if (allocationRuleMap != null) {
65             List<AllocationRule> allocationRuleList = allocationRuleMap.get(serviceModel);
66             if (allocationRuleList == null) {
67                 allocationRuleList = allocationRuleMap.get("DEFAULT");
68             }
69
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);
75                     if (ar != null) {
76                         AllocationOutcome ao = resourceManager.allocateResources(ar);
77                         List<ResourceData> rr = getResourceData(ao);
78                         resourceList.addAll(rr);
79
80                         if (ao.status != AllocationStatus.Success) {
81                             allgood = false;
82                         }
83                     }
84                 }
85
86                 if (!allgood) {
87                     String resourceSetId = resourceEntity.resourceEntityType + "::" + resourceEntity.resourceEntityId
88                             + "::" + resourceEntity.resourceEntityVersion;
89                     resourceManager.releaseResources(ReleaseRequest.resourceSet(resourceSetId));
90                 }
91             }
92         }
93
94         return resourceList;
95     }
96
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));
102             }
103             return rr;
104         }
105
106         ResourceData rd = new ResourceData();
107         rd.data = new HashMap<>();
108
109         AllocationRequest ar = ao.request;
110         rd.resourceName = ar.resourceName;
111         rd.endPointPosition = ar.endPointPosition;
112         int i1 = ar.assetId.indexOf("::");
113         if (i1 > 0) {
114             rd.resourceTargetType = ar.assetId.substring(0, i1);
115             rd.resourceTargetId = ar.assetId.substring(i1 + 2);
116         } else {
117             rd.resourceTargetType = "";
118             rd.resourceTargetId = ar.assetId;
119         }
120         rd.status = ao.status.toString();
121
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)));
132         }
133
134         return Collections.singletonList(rd);
135     }
136
137     @Override
138     public List<ResourceData> getResourcesForEntity(String resourceEntityType, String resourceEntityId,
139             String resourceEntityVersion) {
140         List<ResourceData> rdlist = new ArrayList<>();
141
142         String resourceUnionId = resourceEntityType + "::" + resourceEntityId;
143         List<Resource> rlist = resourceManager.getResourceUnion(resourceUnionId);
144
145         for (Resource r : rlist) {
146
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)) {
154                         ai = ai1;
155                         break;
156                     }
157                 }
158             } else {
159                 Date aitime = null;
160                 for (AllocationItem ai1 : r.allocationItems) {
161                     if (ai1.resourceUnionId.equals(resourceUnionId)) {
162                         if (aitime == null || ai1.allocationTime.after(aitime)) {
163                             ai = ai1;
164                             aitime = ai1.allocationTime;
165                         }
166                     }
167                 }
168             }
169
170             if (ai != null) {
171                 ResourceData rd = new ResourceData();
172                 rdlist.add(rd);
173
174                 rd.resourceName = r.resourceKey.resourceName;
175                 int i1 = r.resourceKey.assetId.indexOf("::");
176                 if (i1 > 0) {
177                     rd.resourceTargetType = r.resourceKey.assetId.substring(0, i1);
178                     rd.resourceTargetId = r.resourceKey.assetId.substring(i1 + 2);
179
180                     int i2 = r.resourceKey.assetId.lastIndexOf("::");
181                     if (i2 > i1) {
182                         rd.resourceTargetValue = r.resourceKey.assetId.substring(i2 + 2);
183                     }
184                 } else {
185                     rd.resourceTargetType = "";
186                     rd.resourceTargetId = r.resourceKey.assetId;
187                 }
188
189                 rd.data = new HashMap<>();
190
191                 if (ai instanceof RangeAllocationItem) {
192                     RangeAllocationItem rai = (RangeAllocationItem) ai;
193
194                     String ss = String.valueOf(rai.used);
195                     ss = ss.substring(1, ss.length() - 1);
196                     rd.data.put("allocated", ss);
197
198                 } else if (ai instanceof LimitAllocationItem) {
199                     LimitAllocationItem lai = (LimitAllocationItem) ai;
200
201                     rd.data.put("allocated", String.valueOf(lai.used));
202                 }
203             }
204         }
205
206         return rdlist;
207     }
208
209     @Override
210     public List<ResourceData> getResourcesForTarget(String resourceTargetTypeFilter, String resourceTargetIdFilter,
211             String resourceName) {
212         List<ResourceData> rdlist = new ArrayList<>();
213
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;
221         }
222
223         List<Resource> rlist = resourceManager.queryResources(resourceName, assetIdFilter);
224
225         for (Resource r : rlist) {
226             if (r.allocationItems == null || r.allocationItems.isEmpty()) {
227                 continue;
228             }
229
230             log.info("ResourceName:" + r.resourceKey.resourceName + " assetId:" + r.resourceKey.assetId);
231
232            
233             rdlist.add(getResourceData(r));
234         }
235
236         return rdlist;
237     }
238
239     @Override
240     public ResourceData getResource(String resourceTargetType, String resourceTargetId, String resourceName,
241             String resourceEntityTypeFilter, String resourceEntityIdFilter, String resourceShareGroupFilter) {
242         String assetId = resourceTargetType + "::" + resourceTargetId;
243
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;
251         }
252
253         Resource r = null;
254         if (resourceUnionFilter != null || resourceShareGroupFilter != null) {
255             r = resourceManager.queryResource(resourceName, assetId, resourceUnionFilter, resourceShareGroupFilter);
256         } else {
257             r = resourceManager.getResource(resourceName, assetId);
258         }
259
260         if (r != null && r.allocationItems != null && !r.allocationItems.isEmpty()) {
261             log.info("ResourceName:" + r.resourceKey.resourceName + " assetId:" + r.resourceKey.assetId);
262
263             ResourceData rd = getResourceData(r);
264             return rd;
265         }
266
267         return null;
268     }
269
270     private ResourceData getResourceData(Resource r) {
271         ResourceData rd = new ResourceData();
272
273         rd.resourceName = r.resourceKey.resourceName;
274         int i1 = r.resourceKey.assetId.indexOf("::");
275         if (i1 > 0) {
276             rd.resourceTargetType = r.resourceKey.assetId.substring(0, i1);
277             rd.resourceTargetId = r.resourceKey.assetId.substring(i1 + 2);
278
279             int i2 = r.resourceKey.assetId.lastIndexOf("::");
280             if (i2 > i1) {
281                 rd.resourceTargetValue = r.resourceKey.assetId.substring(i2 + 2);
282             }
283         } else {
284             rd.resourceTargetType = "";
285             rd.resourceTargetId = r.resourceKey.assetId;
286         }
287
288         rd.data = new HashMap<>();
289
290         if (r instanceof RangeResource) {
291             RangeResource rr = (RangeResource) r;
292
293             log.info("rr.used: " + rr.used);
294             String ss = String.valueOf(rr.used);
295             ss = ss.substring(1, ss.length() - 1);
296             rd.data.put("allocated", ss);
297
298         } else if (r instanceof LimitResource) {
299             LimitResource lr = (LimitResource) r;
300
301             log.info("lr.used: " + lr.used);
302             rd.data.put("allocated", String.valueOf(lr.used));
303         }
304
305         rd.allocationDataList = new ArrayList<>();
306
307         if (r.allocationItems != null) {
308             for (AllocationItem ai : r.allocationItems) {
309                 AllocationData ad = new AllocationData();
310                 rd.allocationDataList.add(ad);
311
312                 i1 = ai.resourceUnionId.indexOf("::");
313                 if (i1 > 0) {
314                     ad.resourceEntityType = ai.resourceUnionId.substring(0, i1);
315                     ad.resourceEntityId = ai.resourceUnionId.substring(i1 + 2);
316                 } else {
317                     ad.resourceEntityType = "";
318                     ad.resourceEntityId = ai.resourceUnionId;
319                 }
320
321                 i1 = ai.resourceSetId.lastIndexOf("::");
322                 if (i1 > 0) {
323                     ad.resourceEntityVersion = ai.resourceSetId.substring(i1 + 2);
324                 } else {
325                     ad.resourceEntityVersion = "";
326                 }
327
328                 ad.data = new HashMap<>();
329
330                 if (ai instanceof RangeAllocationItem) {
331                     RangeAllocationItem rai = (RangeAllocationItem) ai;
332
333                     log.info("rr.used: " + rai.used);
334                     String ss = String.valueOf(rai.used);
335                     ss = ss.substring(1, ss.length() - 1);
336                     ad.data.put("allocated", ss);
337
338                 } else if (ai instanceof LimitAllocationItem) {
339                     LimitAllocationItem lai = (LimitAllocationItem) ai;
340
341                     log.info("lr.used: " + lai.used);
342                     ad.data.put("allocated", String.valueOf(lai.used));
343                 }
344             }
345         }
346
347         return rd;
348     }
349
350     public void setResourceManager(ResourceManager resourceManager) {
351         this.resourceManager = resourceManager;
352     }
353
354     public void setAllocationRuleMap(Map<String, List<AllocationRule>> allocationRuleMap) {
355         this.allocationRuleMap = allocationRuleMap;
356     }
357 }