2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 ONAP Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.openecomp.sdnc.ra.comp;
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.HashMap;
27 import java.util.List;
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;
45 public class EndPointAllocatorImpl implements EndPointAllocator {
47 private static final Logger log = LoggerFactory.getLogger(EndPointAllocatorImpl.class);
49 private Map<String, List<EndPointAllocationDefinition>> endPointAllocationDefinitionMap;
51 private ResourceManager resourceManager;
54 public List<EndPointData> allocateEndPoints(
55 ServiceData serviceData,
56 Map<String, Object> equipmentConstraints,
60 List<EndPointAllocationDefinition> defList = endPointAllocationDefinitionMap.get(serviceData.serviceModel);
62 throw new NotImplementedException("Service model: " + serviceData.serviceModel + " not supported");
64 List<EndPointData> epList = new ArrayList<>();
65 for (EndPointAllocationDefinition def : defList) {
66 if (serviceData.endPointPosition != null && !serviceData.endPointPosition.equals(def.endPointPosition))
70 "Starting allocation of end point: " + def.endPointPosition + ": " + serviceData.serviceInstanceId);
72 String resourceUnionId = serviceData.serviceInstanceId + '/' + def.endPointPosition;
73 String resourceSetId = resourceUnionId + '/' + changeNumber;
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);
81 log.info("Trying assignment on the current equipment: " + epExisting.equipmentId);
85 List<EquipmentData> equipList = def.equipmentReader.readEquipment(equipmentConstraints);
86 if (equipList == null || equipList.isEmpty()) {
87 log.info("Equipment not found for " + def.endPointPosition);
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;
99 if (equipList.isEmpty()) {
100 log.info("No equipment meets the requiremets for the service for: " + def.endPointPosition);
105 if (equipList.size() > 1 && def.preferenceRuleList != null && !def.preferenceRuleList.isEmpty()) {
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);
115 for (PreferenceRule prefRule : def.preferenceRuleList)
116 prefEquip.prefNumbers[i++] =
117 prefRule.assignOrderNumber(def.endPointPosition, serviceData, equipData);
120 Collections.sort(prefEquipList);
122 equipList = new ArrayList<>();
123 for (PrefEquipment prefEquip : prefEquipList)
124 equipList.add(prefEquip.equipData);
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);
134 AllocationOutcome ao = resourceManager.allocateResources(ar);
135 if (ao.status != AllocationStatus.Success) {
142 EndPointData ep = readEndPoint(resourceUnionId, resourceSetId);
152 private EndPointData readEndPoint(String resourceUnionId, String resourceSetId) {
153 EndPointData ep = new EndPointData();
154 ep.resourceUnionId = resourceUnionId;
155 ep.resourceSetId = resourceSetId;
157 int i1 = resourceUnionId.indexOf('/');
159 ep.endPointPosition = resourceUnionId.substring(i1 + 1);
161 ep.data = new HashMap<>();
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());
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);
189 private static class PrefEquipment implements Comparable<PrefEquipment> {
191 public long[] prefNumbers;
192 public EquipmentData equipData;
195 public int compareTo(PrefEquipment o) {
196 for (int i = 0; i < prefNumbers.length; i++) {
197 if (prefNumbers[i] < o.prefNumbers[i])
199 if (prefNumbers[i] > o.prefNumbers[i])
206 public void setEndPointAllocationDefinitionMap(
207 Map<String, List<EndPointAllocationDefinition>> endPointAllocationDefinitionMap) {
208 this.endPointAllocationDefinitionMap = endPointAllocationDefinitionMap;
211 public void setResourceManager(ResourceManager resourceManager) {
212 this.resourceManager = resourceManager;