Bug 5199 : Fixed unbind & other issues in intrface
[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.servicebindings.flowbased.utilities.FlowBasedServicesUtils;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.L2vlan;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.Tunnel;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.servicebinding.rev151015.service.bindings.ServicesInfo;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.servicebinding.rev151015.service.bindings.services.info.BoundServices;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 import com.google.common.util.concurrent.ListenableFuture;
31
32 public class FlowBasedServicesStateUnbindHelper {
33     private static final Logger LOG = LoggerFactory.getLogger(FlowBasedServicesStateUnbindHelper.class);
34
35     public static List<ListenableFuture<Void>> unbindServicesFromInterface(Interface ifaceState,
36                                                                            DataBroker dataBroker) {
37         List<ListenableFuture<Void>> futures = new ArrayList<>();
38
39         ServicesInfo servicesInfo = FlowBasedServicesUtils.getServicesInfoForInterface(ifaceState.getName(), dataBroker);
40         if (servicesInfo == null) {
41             return futures;
42         }
43
44         List<BoundServices> allServices = servicesInfo.getBoundServices();
45         if (allServices == null || allServices.isEmpty()) {
46             return futures;
47         }
48
49         if (ifaceState.getType().isAssignableFrom(L2vlan.class)) {
50             return unbindServiceOnVlan(allServices, ifaceState, ifaceState.getIfIndex(), dataBroker);
51         } else if (ifaceState.getType().isAssignableFrom(Tunnel.class)){
52              return unbindServiceOnTunnel(allServices, ifaceState, ifaceState.getIfIndex(), dataBroker);
53         }
54         return futures;
55     }
56
57     private static List<ListenableFuture<Void>> unbindServiceOnTunnel(
58             List<BoundServices> allServices,
59             Interface iface,
60             Integer ifIndex, DataBroker dataBroker) {
61         List<ListenableFuture<Void>> futures = new ArrayList<>();
62         WriteTransaction t = dataBroker.newWriteOnlyTransaction();
63
64         List<String> ofportIds = iface.getLowerLayerIf();
65         NodeConnectorId nodeConnectorId = new NodeConnectorId(ofportIds.get(0));
66         if(nodeConnectorId == null){
67             return futures;
68         }
69         BoundServices highestPriorityBoundService = FlowBasedServicesUtils.getHighestPriorityService(allServices);
70
71         BigInteger dpId = new BigInteger(IfmUtil.getDpnFromNodeConnectorId(nodeConnectorId));
72         FlowBasedServicesUtils.removeIngressFlow(iface.getName(), highestPriorityBoundService, dpId, t);
73
74         for (BoundServices boundService : allServices) {
75             if (!boundService.equals(highestPriorityBoundService)) {
76                 FlowBasedServicesUtils.removeLPortDispatcherFlow(dpId, iface.getName(), boundService, t, boundService.getServicePriority());
77             }
78         }
79
80         futures.add(t.submit());
81         return futures;
82     }
83
84     private static List<ListenableFuture<Void>> unbindServiceOnVlan(
85             List<BoundServices> allServices, Interface ifaceState,
86             Integer ifIndex, DataBroker dataBroker) {
87         List<ListenableFuture<Void>> futures = new ArrayList<>();
88         WriteTransaction t = dataBroker.newWriteOnlyTransaction();
89         List<String> ofportIds = ifaceState.getLowerLayerIf();
90         NodeConnectorId nodeConnectorId = new NodeConnectorId(ofportIds.get(0));
91         if(nodeConnectorId == null){
92             return futures;
93         }
94         BigInteger dpId = new BigInteger(IfmUtil.getDpnFromNodeConnectorId(nodeConnectorId));
95         Collections.sort(allServices, new Comparator<BoundServices>() {
96             @Override
97             public int compare(BoundServices serviceInfo1, BoundServices serviceInfo2) {
98                 return serviceInfo1.getServicePriority().compareTo(serviceInfo2.getServicePriority());
99             }
100         });
101         BoundServices highestPriority = allServices.remove(0);
102         FlowBasedServicesUtils.removeLPortDispatcherFlow(dpId, ifaceState.getName(), highestPriority, t, IfmConstants.DEFAULT_SERVICE_INDEX);
103         for (BoundServices boundService : allServices) {
104                 FlowBasedServicesUtils.removeLPortDispatcherFlow(dpId, ifaceState.getName(), boundService, t, boundService.getServicePriority());
105         }
106         futures.add(t.submit());
107         return futures;
108
109     }
110
111 }