Roll version for Casablanca
[ccsdk/sli/adaptors.git] / resource-assignment / provider / src / main / java / org / onap / ccsdk / sli / adaptors / ra / check / VpeLockCheck.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.check;
23
24 import java.util.Map;
25
26 import org.onap.ccsdk.sli.adaptors.ra.comp.EquipmentCheck;
27 import org.onap.ccsdk.sli.adaptors.ra.comp.ServiceData;
28 import org.onap.ccsdk.sli.adaptors.ra.equip.data.EquipmentData;
29 import org.onap.ccsdk.sli.adaptors.ra.rule.dao.VpeLockDao;
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.Resource;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 public class VpeLockCheck implements EquipmentCheck {
37
38     private static final Logger log = LoggerFactory.getLogger(VpeLockCheck.class);
39
40     private VpeLockDao vpeLockDao;
41     private ResourceManager resourceManager;
42
43     @Override
44     public boolean checkEquipment(
45             String endPointPosition,
46             ServiceData serviceData,
47             EquipmentData equipData,
48             Map<String, Object> equipmentConstraints) {
49         String vrfName = (String) serviceData.data.get("vrf-name");
50         if (vrfName == null)
51             return true;
52
53         String vpeName = (String) equipData.data.get("vpe-id");
54         String vpeLock = vpeLockDao.getVpeLock(vpeName);
55         if (vpeLock == null)
56             return true;
57
58         if (vpeLock.equals("vpe-total-lock")) {
59             log.info("Skipping VPE " + vpeName + ": There is a " + vpeLock + " on it.");
60             return false;
61         }
62
63         if (vpeLock.equals("vpe-vrf-lock") && requiresNewVrf(equipData.equipmentId, vrfName)) {
64             log.info("Skipping VPE " + vpeName + ": There is a " + vpeLock +
65                     " on it and it requires a new VRF for VPN: " + vrfName + ".");
66             return false;
67         }
68
69         if (vpeLock.equals("vpe-mvrf-lock") && requiresNewMVrf(equipData.equipmentId, vrfName)) {
70             log.info("Skipping VPE " + vpeName + ": There is a " + vpeLock +
71                     " on it and it requires a new multicast VRF for VPN: " + vrfName + ".");
72             return false;
73         }
74
75         return true;
76     }
77
78     boolean requiresNewVrf(String equipmentId, String vrfName) {
79         Resource r = resourceManager.getResource("VRF", equipmentId);
80         if (r == null || r.allocationItems == null)
81             return true;
82
83         for (AllocationItem ai : r.allocationItems) {
84             if (ai.resourceShareGroupList.contains(vrfName))
85                 return false;
86         }
87
88         return true;
89     }
90
91     boolean requiresNewMVrf(String equipmentId, String vrfName) {
92         Resource r = resourceManager.getResource("MVRF", equipmentId);
93         if (r == null || r.allocationItems == null)
94             return true;
95
96         for (AllocationItem ai : r.allocationItems) {
97             if (ai.resourceShareGroupList.contains(vrfName))
98                 return false;
99         }
100
101         return true;
102     }
103
104     public void setVpeLockDao(VpeLockDao vpeLockDao) {
105         this.vpeLockDao = vpeLockDao;
106     }
107
108     public void setResourceManager(ResourceManager resourceManager) {
109         this.resourceManager = resourceManager;
110     }
111 }