Fix license headers
[ccsdk/sli/adaptors.git] / resource-assignment / provider / src / main / java / org / openecomp / sdnc / ra / check / HubWithRgCheck.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.openecomp.sdnc.ra.check;
23
24 import java.util.Map;
25
26 import org.openecomp.sdnc.ra.comp.EquipmentCheck;
27 import org.openecomp.sdnc.ra.comp.ServiceData;
28 import org.openecomp.sdnc.ra.equip.data.EquipmentData;
29 import org.openecomp.sdnc.rm.comp.ResourceManager;
30 import org.openecomp.sdnc.rm.data.AllocationItem;
31 import org.openecomp.sdnc.rm.data.Resource;
32 import org.openecomp.sdnc.util.vrf.VpnParam;
33 import org.openecomp.sdnc.util.vrf.VrfUtil;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 public class HubWithRgCheck implements EquipmentCheck {
38
39     private static final Logger log = LoggerFactory.getLogger(HubWithRgCheck.class);
40
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         // Check if this is HUB. If not, this check is not applicable
54         VpnParam vpnp = VrfUtil.parseVrfInstanceName(vrfName);
55         if (vpnp.siteType == null || !vpnp.siteType.equals("HUB"))
56             return true;
57
58         boolean rgPresent = vpnp.routeGroupName != null;
59
60         // First check if a new VRF would be required. If not, we are good
61         Resource r = resourceManager.getResource("VRF", equipData.equipmentId);
62         if (r != null && r.allocationItems != null) {
63             for (AllocationItem ai : r.allocationItems)
64                 if (ai.resourceShareGroupList.contains(vrfName))
65                     return true;
66
67             String resourceUnionId = serviceData.serviceInstanceId + '/' + serviceData.endPointPosition;
68
69             // Check if there is already another HUB VRF with RG presence that does not match the requested
70             for (AllocationItem ai : r.allocationItems) {
71
72                 // Skip the allocation item for the current service instance, if there, in case it is a change order
73                 if (ai.resourceUnionId.equals(resourceUnionId))
74                     continue;
75
76                 if (ai.resourceShareGroupList != null && ai.resourceShareGroupList.size() > 0) {
77                     String vrfName2 = ai.resourceShareGroupList.iterator().next();
78                     VpnParam vpnp2 = VrfUtil.parseVrfInstanceName(vrfName2);
79
80                     if (vpnp2.siteType == null || !vpnp2.siteType.equals("HUB"))
81                         continue;
82
83                     boolean rgPresent2 = vpnp2.routeGroupName != null;
84
85                     if (rgPresent && !rgPresent2) {
86                         log.info("Skipping VPE " + equipData.equipmentId +
87                                 ": This request requires new HUB with RG VRF, " +
88                                 "but there is already another HUB VRF with no RG: " + vrfName2 + ".");
89                         return false;
90                     }
91                     if (!rgPresent && rgPresent2) {
92                         log.info("Skipping VPE " + equipData.equipmentId +
93                                 ": This request requires new HUB VRF with no RG, " +
94                                 "but there is already another HUB with RG VRF: " + vrfName2 + ".");
95                         return false;
96                     }
97                 }
98             }
99         }
100
101         return true;
102     }
103
104     public void setResourceManager(ResourceManager resourceManager) {
105         this.resourceManager = resourceManager;
106     }
107 }