Merge "Add egress split horizon drop flows for external interfaces"
[genius.git] / interfacemanager / interfacemanager-impl / src / main / java / org / opendaylight / genius / interfacemanager / servicebindings / flowbased / config / helpers / FlowBasedEgressServicesConfigUnbindHelper.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.config.helpers;
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.InterfacemgrProvider;
15 import org.opendaylight.genius.interfacemanager.commons.InterfaceManagerCommonUtils;
16 import org.opendaylight.genius.interfacemanager.servicebindings.flowbased.config.factory.FlowBasedServicesConfigRemovable;
17 import org.opendaylight.genius.interfacemanager.servicebindings.flowbased.utilities.FlowBasedServicesUtils;
18 import org.opendaylight.genius.mdsalutil.NwConstants;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.L2vlan;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.Tunnel;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.ServiceModeBase;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.ServicesInfo;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.services.info.BoundServices;
26 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 import java.math.BigInteger;
31 import java.util.ArrayList;
32 import java.util.List;
33
34 public class FlowBasedEgressServicesConfigUnbindHelper implements FlowBasedServicesConfigRemovable {
35     private static final Logger LOG = LoggerFactory.getLogger(FlowBasedEgressServicesConfigUnbindHelper.class);
36
37     private InterfacemgrProvider interfaceMgrProvider;
38     private static volatile FlowBasedServicesConfigRemovable flowBasedEgressServicesRemovable;
39
40     private FlowBasedEgressServicesConfigUnbindHelper(InterfacemgrProvider interfaceMgrProvider) {
41         this.interfaceMgrProvider = interfaceMgrProvider;
42     }
43
44     public static void intitializeFlowBasedEgressServicesConfigRemoveHelper(InterfacemgrProvider interfaceMgrProvider) {
45         if (flowBasedEgressServicesRemovable == null) {
46             synchronized (FlowBasedEgressServicesConfigUnbindHelper.class) {
47                 if (flowBasedEgressServicesRemovable == null) {
48                     flowBasedEgressServicesRemovable = new FlowBasedEgressServicesConfigUnbindHelper(interfaceMgrProvider);
49                 }
50             }
51         }
52     }
53
54     public static FlowBasedServicesConfigRemovable getFlowBasedEgressServicesRemoveHelper() {
55         if (flowBasedEgressServicesRemovable == null) {
56             LOG.error("FlowBasedIngressBindHelper`` is not initialized");
57         }
58         return flowBasedEgressServicesRemovable;
59     }
60
61     public List<ListenableFuture<Void>> unbindService(InstanceIdentifier<BoundServices> instanceIdentifier,
62                                                              BoundServices boundServiceOld) {
63         List<ListenableFuture<Void>> futures = new ArrayList<>();
64         DataBroker dataBroker = interfaceMgrProvider.getDataBroker();
65         String interfaceName =
66                 InstanceIdentifier.keyOf(instanceIdentifier.firstIdentifierOf(ServicesInfo.class)).getInterfaceName();
67         Class<? extends ServiceModeBase> serviceMode = InstanceIdentifier.keyOf(instanceIdentifier.firstIdentifierOf(ServicesInfo.class)).getServiceMode();
68
69         // Get the Parent ServiceInfo
70         ServicesInfo servicesInfo = FlowBasedServicesUtils.getServicesInfoForInterface(interfaceName, serviceMode, dataBroker);
71         if (servicesInfo == null) {
72             LOG.error("Reached Impossible part in the code for bound service: {}", boundServiceOld);
73             return futures;
74         }
75
76         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface ifState =
77                 InterfaceManagerCommonUtils.getInterfaceStateFromOperDS(interfaceName, dataBroker);
78         if (ifState == null || ifState.getOperStatus() == OperStatus.Down) {
79             LOG.info("Not unbinding Service since operstatus is DOWN for Interface: {}", interfaceName);
80             return futures;
81         }
82         List<BoundServices> boundServices = servicesInfo.getBoundServices();
83
84         // Split based on type of interface....
85         if (ifState.getType().isAssignableFrom(L2vlan.class)) {
86             return unbindServiceOnVlan(boundServiceOld, boundServices, ifState, dataBroker);
87         } else if (ifState.getType().isAssignableFrom(Tunnel.class)) {
88             return unbindServiceOnTunnel(boundServiceOld, boundServices, ifState, dataBroker);
89         }
90         return futures;
91     }
92
93     private static List<ListenableFuture<Void>> unbindServiceOnVlan(
94             BoundServices boundServiceOld,
95             List<BoundServices> boundServices, org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface ifaceState,
96             DataBroker dataBroker) {
97
98         List<ListenableFuture<Void>> futures = new ArrayList<>();
99         WriteTransaction t = dataBroker.newWriteOnlyTransaction();
100         Interface iface = InterfaceManagerCommonUtils.getInterfaceFromConfigDS(ifaceState.getName(), dataBroker);
101         BigInteger dpId = FlowBasedServicesUtils.getDpnIdFromInterface(ifaceState);
102         if (boundServices.isEmpty()) {
103             // Remove default entry from Lport Dispatcher Table.
104             FlowBasedServicesUtils.removeEgressDispatcherFlows(dpId, ifaceState.getName(), boundServiceOld, t, NwConstants.DEFAULT_SERVICE_INDEX);
105             if (t != null) {
106                 futures.add(t.submit());
107             }
108             return futures;
109         }
110         BoundServices[] highLow = FlowBasedServicesUtils.getHighAndLowPriorityService(boundServices, boundServiceOld);
111         BoundServices low = highLow[0];
112         BoundServices high = highLow[1];
113         // This means the one removed was the highest priority service
114         if (high == null) {
115             FlowBasedServicesUtils.removeEgressDispatcherFlows(dpId, ifaceState.getName(), boundServiceOld, t, NwConstants.DEFAULT_SERVICE_INDEX);
116             if (low != null) {
117                 //delete the lower services flow entry.
118                 FlowBasedServicesUtils.removeEgressDispatcherFlows(dpId, ifaceState.getName(), low, t, low.getServicePriority());
119                 BoundServices lower = FlowBasedServicesUtils.getHighAndLowPriorityService(boundServices, low)[0];
120                 short lowerServiceIndex = (short) ((lower!=null) ? lower.getServicePriority() : low.getServicePriority() + 1);
121                 FlowBasedServicesUtils.installEgressDispatcherFlows(dpId, low, ifaceState.getName(), t, ifaceState.getIfIndex(), NwConstants.DEFAULT_SERVICE_INDEX, lowerServiceIndex, iface);
122             }
123         } else {
124             FlowBasedServicesUtils.removeEgressDispatcherFlows(dpId, ifaceState.getName(), boundServiceOld, t, boundServiceOld.getServicePriority());
125             short lowerServiceIndex = (short) ((low!=null) ? low.getServicePriority() : boundServiceOld.getServicePriority() + 1);
126             BoundServices highest = FlowBasedServicesUtils.getHighestPriorityService(boundServices);
127             if (high.equals(highest)) {
128                 FlowBasedServicesUtils.installEgressDispatcherFlows(dpId, high, ifaceState.getName(),t, ifaceState.getIfIndex(), NwConstants.DEFAULT_SERVICE_INDEX, lowerServiceIndex, iface);
129             } else {
130                 FlowBasedServicesUtils.installEgressDispatcherFlows(dpId, high, ifaceState.getName(),t, ifaceState.getIfIndex(), high.getServicePriority(), lowerServiceIndex, iface);
131             }
132         }
133         futures.add(t.submit());
134         return futures;
135     }
136
137     private static List<ListenableFuture<Void>> unbindServiceOnTunnel(
138             BoundServices boundServiceOld,
139             List<BoundServices> boundServices, org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface ifState,
140             DataBroker dataBroker) {
141         List<ListenableFuture<Void>> futures = new ArrayList<>();
142
143         // FIXME : not yet supported
144         return futures;
145     }
146 }