[CCSDK-6] Populate seed code
[ccsdk/sli/adaptors.git] / resource-assignment / provider / src / main / java / org / openecomp / sdnc / ra / comp / EndPointAllocatorImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
4  * ================================================================================
5  * Copyright (C) 2017 ONAP 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.openecomp.sdnc.ra.comp;
23
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map;
29
30 import org.apache.commons.lang.NotImplementedException;
31 import org.openecomp.sdnc.ra.equip.data.EquipmentData;
32 import org.openecomp.sdnc.rm.comp.ResourceManager;
33 import org.openecomp.sdnc.rm.data.AllocationItem;
34 import org.openecomp.sdnc.rm.data.AllocationOutcome;
35 import org.openecomp.sdnc.rm.data.AllocationRequest;
36 import org.openecomp.sdnc.rm.data.AllocationStatus;
37 import org.openecomp.sdnc.rm.data.LimitAllocationItem;
38 import org.openecomp.sdnc.rm.data.LimitResource;
39 import org.openecomp.sdnc.rm.data.RangeAllocationItem;
40 import org.openecomp.sdnc.rm.data.RangeResource;
41 import org.openecomp.sdnc.rm.data.Resource;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 public class EndPointAllocatorImpl implements EndPointAllocator {
46
47         private static final Logger log = LoggerFactory.getLogger(EndPointAllocatorImpl.class);
48
49         private Map<String, List<EndPointAllocationDefinition>> endPointAllocationDefinitionMap;
50
51         private ResourceManager resourceManager;
52
53         @Override
54         public List<EndPointData> allocateEndPoints(
55                 ServiceData serviceData,
56                 Map<String, Object> equipmentConstraints,
57                 boolean checkOnly,
58                 boolean change,
59                 int changeNumber) {
60                 List<EndPointAllocationDefinition> defList = endPointAllocationDefinitionMap.get(serviceData.serviceModel);
61                 if (defList == null)
62                         throw new NotImplementedException("Service model: " + serviceData.serviceModel + " not supported");
63
64                 List<EndPointData> epList = new ArrayList<>();
65                 for (EndPointAllocationDefinition def : defList) {
66                         if (serviceData.endPointPosition != null && !serviceData.endPointPosition.equals(def.endPointPosition))
67                                 continue;
68
69                         log.info(
70                                 "Starting allocation of end point: " + def.endPointPosition + ": " + serviceData.serviceInstanceId);
71
72                         String resourceUnionId = serviceData.serviceInstanceId + '/' + def.endPointPosition;
73                         String resourceSetId = resourceUnionId + '/' + changeNumber;
74
75                         String equipmentId = (String) equipmentConstraints.get("equipment-id");
76                         if (equipmentId == null) {
77                                 EndPointData epExisting = readEndPoint(resourceUnionId, resourceSetId);
78                                 if (epExisting != null && epExisting.equipmentId != null) {
79                                         equipmentConstraints.put("equipment-id", epExisting.equipmentId);
80
81                                         log.info("Trying assignment on the current equipment: " + epExisting.equipmentId);
82                                 }
83                         }
84
85                         List<EquipmentData> equipList = def.equipmentReader.readEquipment(equipmentConstraints);
86                         if (equipList == null || equipList.isEmpty()) {
87                                 log.info("Equipment not found for " + def.endPointPosition);
88                                 break;
89                         }
90
91                         if (def.equipmentCheckList != null) {
92                                 for (EquipmentCheck filter : def.equipmentCheckList) {
93                                         List<EquipmentData> newEquipList = new ArrayList<>();
94                                         for (EquipmentData equipData : equipList)
95                                                 if (filter.checkEquipment(def.endPointPosition, serviceData, equipData, equipmentConstraints))
96                                                         newEquipList.add(equipData);
97                                         equipList = newEquipList;
98                                 }
99                                 if (equipList.isEmpty()) {
100                                         log.info("No equipment meets the requiremets for the service for: " + def.endPointPosition);
101                                         break;
102                                 }
103                         }
104
105                         if (equipList.size() > 1 && def.preferenceRuleList != null && !def.preferenceRuleList.isEmpty()) {
106
107                                 List<PrefEquipment> prefEquipList = new ArrayList<>();
108                                 for (EquipmentData equipData : equipList) {
109                                         PrefEquipment prefEquip = new PrefEquipment();
110                                         prefEquip.equipData = equipData;
111                                         prefEquip.prefNumbers = new long[def.preferenceRuleList.size()];
112                                         prefEquipList.add(prefEquip);
113
114                                         int i = 0;
115                                         for (PreferenceRule prefRule : def.preferenceRuleList)
116                                                 prefEquip.prefNumbers[i++] =
117                                                         prefRule.assignOrderNumber(def.endPointPosition, serviceData, equipData);
118                                 }
119
120                                 Collections.sort(prefEquipList);
121
122                                 equipList = new ArrayList<>();
123                                 for (PrefEquipment prefEquip : prefEquipList)
124                                         equipList.add(prefEquip.equipData);
125                         }
126
127                         for (EquipmentData equipData : equipList) {
128                                 boolean allgood = true;
129                                 if (def.allocationRuleList != null)
130                                         for (AllocationRule allocationRule : def.allocationRuleList) {
131                                                 AllocationRequest ar = allocationRule.buildAllocationRequest(resourceUnionId, resourceSetId,
132                                                         def.endPointPosition, serviceData, equipData, checkOnly, change);
133                                                 if (ar != null) {
134                                                         AllocationOutcome ao = resourceManager.allocateResources(ar);
135                                                         if (ao.status != AllocationStatus.Success) {
136                                                                 allgood = false;
137                                                                 break;
138                                                         }
139                                                 }
140                                         }
141                                 if (allgood) {
142                                         EndPointData ep = readEndPoint(resourceUnionId, resourceSetId);
143                                         epList.add(ep);
144                                         break;
145                                 }
146                         }
147                 }
148
149                 return epList;
150         }
151
152         private EndPointData readEndPoint(String resourceUnionId, String resourceSetId) {
153                 EndPointData ep = new EndPointData();
154                 ep.resourceUnionId = resourceUnionId;
155                 ep.resourceSetId = resourceSetId;
156
157                 int i1 = resourceUnionId.indexOf('/');
158                 if (i1 > 0)
159                         ep.endPointPosition = resourceUnionId.substring(i1 + 1);
160
161                 ep.data = new HashMap<>();
162
163                 List<Resource> rlist = resourceManager.getResourceUnion(resourceUnionId);
164                 for (Resource r : rlist) {
165                         if (r instanceof RangeResource) {
166                                 RangeResource rr = (RangeResource) r;
167                                 for (AllocationItem ai : r.allocationItems)
168                                         if (ai.resourceUnionId.equals(resourceUnionId)) {
169                                                 RangeAllocationItem rai = (RangeAllocationItem) ai;
170                                                 ep.data.put(ep.endPointPosition + '.' + rr.resourceKey.resourceName, rai.used.first());
171                                         }
172                         }
173                         if (r instanceof LimitResource) {
174                                 LimitResource rr = (LimitResource) r;
175                                 for (AllocationItem ai : r.allocationItems)
176                                         if (ai.resourceUnionId.equals(resourceUnionId)) {
177                                                 LimitAllocationItem rai = (LimitAllocationItem) ai;
178                                                 ep.data.put(ep.endPointPosition + '.' + rr.resourceKey.resourceName + ".allocated", rai.used);
179                                                 ep.data.put(ep.endPointPosition + '.' + rr.resourceKey.resourceName + ".used", rr.used);
180                                                 ep.data.put(ep.endPointPosition + '.' + rr.resourceKey.resourceName + ".assetId",
181                                                         r.resourceKey.assetId);
182                                         }
183                         }
184                 }
185
186                 return ep;
187         }
188
189         private static class PrefEquipment implements Comparable<PrefEquipment> {
190
191                 public long[] prefNumbers;
192                 public EquipmentData equipData;
193
194                 @Override
195                 public int compareTo(PrefEquipment o) {
196                         for (int i = 0; i < prefNumbers.length; i++) {
197                                 if (prefNumbers[i] < o.prefNumbers[i])
198                                         return -1;
199                                 if (prefNumbers[i] > o.prefNumbers[i])
200                                         return 1;
201                         }
202                         return 0;
203                 }
204         }
205
206         public void setEndPointAllocationDefinitionMap(
207                 Map<String, List<EndPointAllocationDefinition>> endPointAllocationDefinitionMap) {
208                 this.endPointAllocationDefinitionMap = endPointAllocationDefinitionMap;
209         }
210
211         public void setResourceManager(ResourceManager resourceManager) {
212                 this.resourceManager = resourceManager;
213         }
214 }