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