Supporting DHCP as a service
[genius.git] / interfacemanager / interfacemanager-impl / src / main / java / org / opendaylight / genius / interfacemanager / servicebindings / flowbased / statehelpers / FlowBasedServicesStateUnbindHelper.java
1 /*
2  * Copyright (c) 2016 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.genius.interfacemanager.servicebindings.flowbased.statehelpers;
9
10 import com.google.common.util.concurrent.ListenableFuture;
11 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
12 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
13 import org.opendaylight.genius.interfacemanager.IfmConstants;
14 import org.opendaylight.genius.interfacemanager.IfmUtil;
15 import org.opendaylight.genius.interfacemanager.servicebindings.flowbased.utilities.FlowBasedServicesUtils;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.L2vlan;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.Tunnel;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.ServiceModeBase;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.ServicesInfo;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.services.info.BoundServices;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 import java.math.BigInteger;
27 import java.util.ArrayList;
28 import java.util.Collections;
29 import java.util.Comparator;
30 import java.util.List;
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, Class<? extends ServiceModeBase> serviceMode,
36                                                                            DataBroker dataBroker) {
37         List<ListenableFuture<Void>> futures = new ArrayList<>();
38         LOG.debug("unbinding services on interface {}", ifaceState.getName());
39         ServicesInfo servicesInfo = FlowBasedServicesUtils.getServicesInfoForInterface(ifaceState.getName(), serviceMode, dataBroker);
40         if (servicesInfo == null) {
41             LOG.trace("service info is null for interface {}", ifaceState.getName());
42             return futures;
43         }
44
45         List<BoundServices> allServices = servicesInfo.getBoundServices();
46         if (allServices == null || allServices.isEmpty()) {
47             LOG.trace("bound services is empty for interface {}", ifaceState.getName());
48             return futures;
49         }
50
51         if(ifaceState.getType() == null) {
52             return futures;
53         }
54
55         if (ifaceState.getType().isAssignableFrom(L2vlan.class)) {
56             return unbindServiceOnVlan(allServices, ifaceState, ifaceState.getIfIndex(), dataBroker);
57         } else if (ifaceState.getType().isAssignableFrom(Tunnel.class)){
58             return unbindServiceOnTunnel(allServices, ifaceState, ifaceState.getIfIndex(), dataBroker);
59         }
60         return futures;
61     }
62
63     private static List<ListenableFuture<Void>> unbindServiceOnTunnel(
64             List<BoundServices> allServices,
65             Interface iface,
66             Integer ifIndex, DataBroker dataBroker) {
67         List<ListenableFuture<Void>> futures = new ArrayList<>();
68         WriteTransaction t = dataBroker.newWriteOnlyTransaction();
69
70         List<String> ofportIds = iface.getLowerLayerIf();
71         NodeConnectorId nodeConnectorId = new NodeConnectorId(ofportIds.get(0));
72         if(nodeConnectorId == null){
73             return futures;
74         }
75         BoundServices highestPriorityBoundService = FlowBasedServicesUtils.getHighestPriorityService(allServices);
76
77         BigInteger dpId = new BigInteger(IfmUtil.getDpnFromNodeConnectorId(nodeConnectorId));
78         FlowBasedServicesUtils.removeIngressFlow(iface.getName(), highestPriorityBoundService, dpId, t);
79
80         for (BoundServices boundService : allServices) {
81             if (!boundService.equals(highestPriorityBoundService)) {
82                 FlowBasedServicesUtils.removeLPortDispatcherFlow(dpId, iface.getName(), boundService, t, boundService.getServicePriority());
83             }
84         }
85
86         futures.add(t.submit());
87         return futures;
88     }
89
90     private static List<ListenableFuture<Void>> unbindServiceOnVlan(
91             List<BoundServices> allServices, Interface ifaceState,
92             Integer ifIndex, DataBroker dataBroker) {
93         List<ListenableFuture<Void>> futures = new ArrayList<>();
94         WriteTransaction t = dataBroker.newWriteOnlyTransaction();
95         List<String> ofportIds = ifaceState.getLowerLayerIf();
96         NodeConnectorId nodeConnectorId = new NodeConnectorId(ofportIds.get(0));
97         if(nodeConnectorId == null){
98             return futures;
99         }
100         BigInteger dpId = new BigInteger(IfmUtil.getDpnFromNodeConnectorId(nodeConnectorId));
101         Collections.sort(allServices, new Comparator<BoundServices>() {
102             @Override
103             public int compare(BoundServices serviceInfo1, BoundServices serviceInfo2) {
104                 return serviceInfo1.getServicePriority().compareTo(serviceInfo2.getServicePriority());
105             }
106         });
107         BoundServices highestPriority = allServices.remove(0);
108         FlowBasedServicesUtils.removeLPortDispatcherFlow(dpId, ifaceState.getName(), highestPriority, t, IfmConstants.DEFAULT_SERVICE_INDEX);
109         for (BoundServices boundService : allServices) {
110             FlowBasedServicesUtils.removeLPortDispatcherFlow(dpId, ifaceState.getName(), boundService, t, boundService.getServicePriority());
111         }
112         futures.add(t.submit());
113         return futures;
114
115     }
116
117 }