e54d89cb177f24e15789ea44a498f5fb7aea439b
[ccsdk/sli/adaptors.git] / resource-assignment / provider / src / main / java / org / onap / ccsdk / sli / adaptors / ra / check / OneMVrfCheck.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.rm.comp.ResourceManager;
30 import org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem;
31 import org.onap.ccsdk.sli.adaptors.rm.data.Resource;
32 import org.onap.ccsdk.sli.adaptors.util.vrf.VpnParam;
33 import org.onap.ccsdk.sli.adaptors.util.vrf.VrfUtil;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 public class OneMVrfCheck implements EquipmentCheck {
38
39     private static final Logger log = LoggerFactory.getLogger(OneMVrfCheck.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         String v4MulticastStr = (String) serviceData.data.get("v4-multicast");
54         String v6MulticastStr = (String) serviceData.data.get("v6-multicast");
55         boolean v4Multicast = v4MulticastStr != null &&
56                 (v4MulticastStr.equalsIgnoreCase("Y") || v4MulticastStr.equalsIgnoreCase("true"));
57         boolean v6Multicast = v6MulticastStr != null &&
58                 (v6MulticastStr.equalsIgnoreCase("Y") || v6MulticastStr.equalsIgnoreCase("true"));
59         if (!v4Multicast && !v6Multicast)
60             return true;
61
62         // First check if a new VRF would be required. If not, we are good
63         Resource r = resourceManager.getResource("VRF", equipData.equipmentId);
64         if (r != null && r.allocationItems != null)
65             for (AllocationItem ai : r.allocationItems)
66                 if (ai.resourceShareGroupList.contains(vrfName))
67                     return true;
68
69         String resourceUnionId = serviceData.serviceInstanceId + '/' + serviceData.endPointPosition;
70
71         // Check if there is already another multicast VRF for the same VPN
72         VpnParam vpnp = VrfUtil.parseVrfInstanceName(vrfName);
73         r = resourceManager.getResource("MVRF", equipData.equipmentId);
74         if (r != null && r.allocationItems != null) {
75             for (AllocationItem ai : r.allocationItems) {
76
77                 // Skip the allocation item for the current service instance, if there, in case it is a change order
78                 if (ai.resourceUnionId.equals(resourceUnionId))
79                     continue;
80
81                 if (ai.resourceShareGroupList != null && ai.resourceShareGroupList.size() > 0) {
82                     String vrfName2 = ai.resourceShareGroupList.iterator().next();
83                     VpnParam vpnp2 = VrfUtil.parseVrfInstanceName(vrfName2);
84                     if (vpnp.vpnId.equals(vpnp2.vpnId)) {
85                         log.info("Skipping VPE " + equipData.equipmentId +
86                                 ": This request requires new multicast VRF, " +
87                                 "but there is already another multicast VRF for the same VPN: " + vrfName2 + ".");
88                         return false;
89                     }
90                 }
91             }
92         }
93
94         return true;
95     }
96
97     public void setResourceManager(ResourceManager resourceManager) {
98         this.resourceManager = resourceManager;
99     }
100 }