Support for defining multiple ranges in RANGE_RULE
[ccsdk/sli/adaptors.git] / resource-assignment / provider / src / main / java / org / onap / ccsdk / sli / adaptors / ra / comp / EndPointAllocatorImpl.java
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.Resource;
43 import org.onap.ccsdk.sli.adaptors.util.str.StrUtil;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47 public class EndPointAllocatorImpl implements EndPointAllocator {
48
49     private static final Logger log = LoggerFactory.getLogger(EndPointAllocatorImpl.class);
50     
51     private ResourceManager resourceManager;
52
53     private Map<String, List<AllocationRule>> allocationRuleMap;
54
55     @Override
56     public List<ResourceData> allocateResources(String serviceModel, ResourceEntity resourceEntity,
57             ResourceTarget resourceTarget, ResourceRequest resourceRequest, boolean checkOnly, boolean change) {
58
59         List<ResourceData> resourceList = new ArrayList<>();
60
61         if (allocationRuleMap != null) {
62             List<AllocationRule> allocationRuleList = allocationRuleMap.get(serviceModel);
63             if (allocationRuleList == null) {
64                 allocationRuleList = allocationRuleMap.get("DEFAULT");
65             }
66
67             if (allocationRuleList != null) {
68                 boolean allgood = true;
69                 for (AllocationRule allocationRule : allocationRuleList) {
70                     AllocationRequest ar = allocationRule.buildAllocationRequest(serviceModel, resourceEntity,
71                             resourceTarget, resourceRequest, checkOnly, change);
72                     if (ar != null) {
73                         AllocationOutcome ao = resourceManager.allocateResources(ar);
74                         List<ResourceData> rr = getResourceData(ao);
75                         resourceList.addAll(rr);
76
77                         if (ao.status != AllocationStatus.Success) {
78                             allgood = false;
79                         }
80                     }
81                 }
82
83                 if (!allgood) {
84                     String resourceSetId = resourceEntity.resourceEntityType + "::" + resourceEntity.resourceEntityId
85                             + "::" + resourceEntity.resourceEntityVersion;
86                     resourceManager.releaseResourceSet(resourceSetId);
87                 }
88             }
89         }
90
91         return resourceList;
92     }
93
94     private List<ResourceData> getResourceData(AllocationOutcome ao) {
95         if (ao instanceof MultiResourceAllocationOutcome) {
96             List<ResourceData> rr = new ArrayList<>();
97             for (AllocationOutcome ao1 : ((MultiResourceAllocationOutcome) ao).allocationOutcomeList) {
98                 rr.addAll(getResourceData(ao1));
99             }
100             return rr;
101         }
102
103         ResourceData rd = new ResourceData();
104         rd.data = new HashMap<>();
105
106         AllocationRequest ar = ao.request;
107         rd.resourceName = ar.resourceName;
108         rd.endPointPosition = ar.endPointPosition;
109         int i1 = ar.assetId.indexOf("::");
110         if (i1 > 0) {
111             rd.resourceTargetType = ar.assetId.substring(0, i1);
112             rd.resourceTargetId = ar.assetId.substring(i1 + 2);
113         } else {
114             rd.resourceTargetType = "";
115             rd.resourceTargetId = ar.assetId;
116         }
117         rd.status = ao.status.toString();
118
119         if (ao instanceof LimitAllocationOutcome) {
120             LimitAllocationOutcome lao = (LimitAllocationOutcome) ao;
121             rd.data.put("allocated", String.valueOf(lao.allocatedCount));
122             rd.data.put("used", String.valueOf(lao.used));
123             rd.data.put("limit", String.valueOf(lao.limit));
124             rd.data.put("available", String.valueOf(lao.limit - lao.used));
125         } else if (ao instanceof RangeAllocationOutcome) {
126             RangeAllocationOutcome rao = (RangeAllocationOutcome) ao;
127             rd.data.put("allocated", String.valueOf(StrUtil.listInt(rao.allocated)));
128             rd.data.put("used", String.valueOf(StrUtil.listInt(rao.used)));
129         }
130
131         return Collections.singletonList(rd);
132     }
133
134     @Override
135     public List<ResourceData> getResourcesForEntity(String resourceEntityType, String resourceEntityId,
136             String resourceEntityVersion) {
137         List<ResourceData> rdlist = new ArrayList<>();
138
139         String resourceUnionId = resourceEntityType + "::" + resourceEntityId;
140         List<Resource> rlist = resourceManager.getResourceUnion(resourceUnionId);
141
142         for (Resource r : rlist) {
143
144             // Find the needed allocation item: if resourceEntityVersion is specified, use that,
145             // otherwise, find the latest allocation item
146             AllocationItem ai = null;
147             if (resourceEntityVersion != null) {
148                 String resourceSetId = resourceUnionId + "::" + resourceEntityVersion;
149                 for (AllocationItem ai1 : r.allocationItems) {
150                     if (ai1.resourceSetId.equals(resourceSetId)) {
151                         ai = ai1;
152                         break;
153                     }
154                 }
155             } else {
156                 Date aitime = null;
157                 for (AllocationItem ai1 : r.allocationItems) {
158                     if (ai1.resourceUnionId.equals(resourceUnionId)) {
159                         if (aitime == null || ai1.allocationTime.after(aitime)) {
160                             ai = ai1;
161                             aitime = ai1.allocationTime;
162                         }
163                     }
164                 }
165             }
166
167             if (ai != null) {
168                 ResourceData rd = new ResourceData();
169                 rdlist.add(rd);
170
171                 rd.resourceName = r.resourceKey.resourceName;
172                 int i1 = r.resourceKey.assetId.indexOf("::");
173                 if (i1 > 0) {
174                     rd.resourceTargetType = r.resourceKey.assetId.substring(0, i1);
175                     rd.resourceTargetId = r.resourceKey.assetId.substring(i1 + 2);
176
177                     int i2 = r.resourceKey.assetId.lastIndexOf("::");
178                     if (i2 > i1) {
179                         rd.resourceTargetValue = r.resourceKey.assetId.substring(i2 + 2);
180                     }
181                 } else {
182                     rd.resourceTargetType = "";
183                     rd.resourceTargetId = r.resourceKey.assetId;
184                 }
185
186                 rd.data = new HashMap<>();
187
188                 if (ai instanceof RangeAllocationItem) {
189                     RangeAllocationItem rai = (RangeAllocationItem) ai;
190
191                     String ss = String.valueOf(rai.used);
192                     ss = ss.substring(1, ss.length() - 1);
193                     rd.data.put("allocated", ss);
194
195                 } else if (ai instanceof LimitAllocationItem) {
196                     LimitAllocationItem lai = (LimitAllocationItem) ai;
197
198                     rd.data.put("allocated", String.valueOf(lai.used));
199                 }
200             }
201         }
202
203         return rdlist;
204     }
205
206     @Override
207     public ResourceData getResource(String resourceTargetType, String resourceTargetId, String resourceName) {
208         ResourceData rd = new ResourceData();;
209         String assetId = resourceTargetType + "::" + resourceTargetId;
210         Resource r = resourceManager.getResource(resourceName, assetId);
211         if (r != null) {
212             log.info("ResourceName:" + r.resourceKey.resourceName + " assetId:" + r.resourceKey.assetId);
213
214             rd.resourceName = r.resourceKey.resourceName;
215             int i1 = r.resourceKey.assetId.indexOf("::");
216             if (i1 > 0) {
217                 rd.resourceTargetType = r.resourceKey.assetId.substring(0, i1);
218                 rd.resourceTargetId = r.resourceKey.assetId.substring(i1 + 2);
219
220                 int i2 = r.resourceKey.assetId.lastIndexOf("::");
221                 if (i2 > i1) {
222                     rd.resourceTargetValue = r.resourceKey.assetId.substring(i2 + 2);
223                 }
224             } else {
225                 rd.resourceTargetType = "";
226                 rd.resourceTargetId = r.resourceKey.assetId;
227             }
228
229             rd.data = new HashMap<>();
230
231             if (r instanceof RangeResource) {
232                 RangeResource rr = (RangeResource) r;
233
234                 log.info("rr.used: " + rr.used);
235                 String ss = String.valueOf(rr.used);
236                 ss = ss.substring(1, ss.length() - 1);
237                 rd.data.put("allocated", ss);
238
239             } else if (r instanceof LimitResource) {
240                 LimitResource lr = (LimitResource) r;
241
242                 log.info("lr.used: " + lr.used);
243                 rd.data.put("allocated", String.valueOf(lr.used));
244             }
245         }
246
247         return rd;
248     }
249
250     public void setResourceManager(ResourceManager resourceManager) {
251         this.resourceManager = resourceManager;
252     }
253
254     public void setAllocationRuleMap(Map<String, List<AllocationRule>> allocationRuleMap) {
255         this.allocationRuleMap = allocationRuleMap;
256     }
257 }