ede481e1e88eee26fc6f127d475b73e8bf8b3ea3
[vpnservice.git] / interfacemgr / interfacemgr-impl / src / main / java / org / opendaylight / vpnservice / interfacemgr / servicebindings / flowbased / statehelpers / FlowBasedServicesStateUnbindHelper.java
1 /*
2  * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.vpnservice.interfacemgr.servicebindings.flowbased.statehelpers;
9
10 import java.math.BigInteger;
11 import java.util.ArrayList;
12 import java.util.Collections;
13 import java.util.Comparator;
14 import java.util.List;
15
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
18 import org.opendaylight.vpnservice.interfacemgr.IfmConstants;
19 import org.opendaylight.vpnservice.interfacemgr.IfmUtil;
20 import org.opendaylight.vpnservice.interfacemgr.commons.InterfaceManagerCommonUtils;
21 import org.opendaylight.vpnservice.interfacemgr.servicebindings.flowbased.utilities.FlowBasedServicesUtils;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.L2vlan;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.Tunnel;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.servicebinding.rev151015.service.bindings.ServicesInfo;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.servicebinding.rev151015.service.bindings.services.info.BoundServices;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 import com.google.common.util.concurrent.ListenableFuture;
33
34 public class FlowBasedServicesStateUnbindHelper {
35     private static final Logger LOG = LoggerFactory.getLogger(FlowBasedServicesStateUnbindHelper.class);
36
37     public static List<ListenableFuture<Void>> unbindServicesFromInterface(Interface ifaceState,
38                                                                            DataBroker dataBroker) {
39         List<ListenableFuture<Void>> futures = new ArrayList<>();
40
41         ServicesInfo servicesInfo = FlowBasedServicesUtils.getServicesInfoForInterface(ifaceState.getName(), dataBroker);
42         if (servicesInfo == null) {
43             return futures;
44         }
45
46         List<BoundServices> allServices = servicesInfo.getBoundServices();
47         if (allServices == null || allServices.isEmpty()) {
48             return futures;
49         }
50
51         InterfaceKey interfaceKey = new InterfaceKey(ifaceState.getName());
52         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface iface =
53                     InterfaceManagerCommonUtils.getInterfaceFromConfigDS(interfaceKey, dataBroker);
54
55         if (ifaceState.getType().isAssignableFrom(L2vlan.class)) {
56             return unbindServiceOnVlan(allServices, iface, ifaceState.getIfIndex(), dataBroker);
57         } else if (ifaceState.getType().isAssignableFrom(Tunnel.class)){
58              return unbindServiceOnTunnel(allServices, iface, ifaceState.getIfIndex(), dataBroker);
59         }
60         return futures;
61     }
62
63     private static List<ListenableFuture<Void>> unbindServiceOnTunnel(
64             List<BoundServices> allServices,
65             org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface iface,
66             Integer ifIndex, DataBroker dataBroker) {
67         List<ListenableFuture<Void>> futures = new ArrayList<>();
68         WriteTransaction t = dataBroker.newWriteOnlyTransaction();
69
70         NodeConnectorId nodeConnectorId = FlowBasedServicesUtils.getNodeConnectorIdFromInterface(iface, dataBroker);
71         if(nodeConnectorId == null){
72             return futures;
73         }
74         BoundServices highestPriorityBoundService = FlowBasedServicesUtils.getHighestPriorityService(allServices);
75
76         BigInteger dpId = new BigInteger(IfmUtil.getDpnFromNodeConnectorId(nodeConnectorId));
77         FlowBasedServicesUtils.removeIngressFlow(iface, highestPriorityBoundService, dpId, t);
78
79         for (BoundServices boundService : allServices) {
80             if (!boundService.equals(highestPriorityBoundService)) {
81                 FlowBasedServicesUtils.removeLPortDispatcherFlow(dpId, iface, boundService, t, boundService.getServicePriority());
82             }
83         }
84
85         futures.add(t.submit());
86         return futures;
87     }
88
89     private static List<ListenableFuture<Void>> unbindServiceOnVlan(
90             List<BoundServices> allServices,
91             org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface iface,
92             Integer ifIndex, DataBroker dataBroker) {
93         List<ListenableFuture<Void>> futures = new ArrayList<>();
94         WriteTransaction t = dataBroker.newWriteOnlyTransaction();
95         NodeConnectorId nodeConnectorId = FlowBasedServicesUtils.getNodeConnectorIdFromInterface(iface, dataBroker);
96         if (nodeConnectorId == null) {
97             return futures;
98         }
99         BigInteger dpId = new BigInteger(IfmUtil.getDpnFromNodeConnectorId(nodeConnectorId));
100         Collections.sort(allServices, new Comparator<BoundServices>() {
101             @Override
102             public int compare(BoundServices serviceInfo1, BoundServices serviceInfo2) {
103                 return serviceInfo2.getServicePriority().compareTo(serviceInfo1.getServicePriority());
104             }
105         });
106         BoundServices highestPriority = allServices.remove(0);
107         FlowBasedServicesUtils.removeLPortDispatcherFlow(dpId, iface, highestPriority, t, IfmConstants.DEFAULT_SERVICE_INDEX);
108         for (BoundServices boundService : allServices) {
109                 FlowBasedServicesUtils.removeLPortDispatcherFlow(dpId, iface, boundService, t, boundService.getServicePriority());
110         }
111         futures.add(t.submit());
112         return futures;
113
114     }
115
116 }