[CCSDK-6] Populate seed code
[ccsdk/sli/adaptors.git] / resource-assignment / provider / src / main / java / org / openecomp / sdnc / ra / pref / AffinityLinkPref.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.pref;
23
24 import java.util.ArrayList;
25 import java.util.List;
26
27 import org.openecomp.sdnc.ra.comp.PreferenceRule;
28 import org.openecomp.sdnc.ra.comp.ServiceData;
29 import org.openecomp.sdnc.ra.equip.data.EquipmentData;
30 import org.openecomp.sdnc.rm.comp.ResourceManager;
31 import org.openecomp.sdnc.rm.data.LimitResource;
32 import org.openecomp.sdnc.rm.data.Resource;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 public class AffinityLinkPref implements PreferenceRule {
37
38         private static final Logger log = LoggerFactory.getLogger(AffinityLinkPref.class);
39
40         private ResourceManager resourceManager;
41         private List<String> affinityLinkIdList;
42
43         public AffinityLinkPref() {
44                 // Set default values for affinity link ids (can be overridden by the spring config)
45                 affinityLinkIdList = new ArrayList<>();
46                 affinityLinkIdList.add("1");
47                 affinityLinkIdList.add("2");
48         }
49
50         @Override
51         public int assignOrderNumber(String endPointPosition, ServiceData serviceData, EquipmentData equipData) {
52
53                 // This class does not really assign order number, but instead sets the affinity link with the lowest
54                 // assigned bandwidth in the equipment data
55
56                 String preferedAffinityLinkId = "1";
57                 long lowestAssignedBw = Long.MAX_VALUE;
58                 for (String affinityLinkId : affinityLinkIdList) {
59                         String assetId = equipData.equipmentId + "-" + affinityLinkId;
60                         Resource r = resourceManager.getResource("Bandwidth", assetId);
61                         if (r != null) {
62                                 LimitResource ll = (LimitResource) r;
63                                 if (ll.used < lowestAssignedBw) {
64                                         lowestAssignedBw = ll.used;
65                                         preferedAffinityLinkId = affinityLinkId;
66                                 }
67                                 log.info("Assigned bandwidth on affinity link: " + assetId + ": " + ll.used);
68                         }
69                 }
70
71                 equipData.data.put("affinity-link", preferedAffinityLinkId);
72
73                 log.info("Prefered affinity link for " + equipData.equipmentId + ": " + preferedAffinityLinkId);
74
75                 return 0;
76         }
77
78         public void setResourceManager(ResourceManager resourceManager) {
79                 this.resourceManager = resourceManager;
80         }
81
82         public void setAffinityLinkIdList(List<String> affinityLinkIdList) {
83                 this.affinityLinkIdList = affinityLinkIdList;
84         }
85 }