Bind/Unbind Service should work irrespective of Port Status
[genius.git] / interfacemanager / interfacemanager-impl / src / main / java / org / opendaylight / genius / interfacemanager / servicebindings / flowbased / config / helpers / FlowBasedIngressServicesConfigUnbindHelper.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 java.math.BigInteger;
12 import java.util.ArrayList;
13 import java.util.List;
14 import java.util.Map;
15 import java.util.concurrent.ConcurrentHashMap;
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
18 import org.opendaylight.genius.interfacemanager.IfmUtil;
19 import org.opendaylight.genius.interfacemanager.InterfacemgrProvider;
20 import org.opendaylight.genius.interfacemanager.commons.InterfaceManagerCommonUtils;
21 import org.opendaylight.genius.interfacemanager.servicebindings.flowbased.config.factory.FlowBasedServicesConfigRemovable;
22 import org.opendaylight.genius.interfacemanager.servicebindings.flowbased.utilities.FlowBasedServicesUtils;
23 import org.opendaylight.genius.mdsalutil.MatchInfo;
24 import org.opendaylight.genius.mdsalutil.NwConstants;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.L2vlan;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.Tunnel;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.ServiceModeBase;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.ServicesInfo;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.services.info.BoundServices;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 public class FlowBasedIngressServicesConfigUnbindHelper implements FlowBasedServicesConfigRemovable {
38     private static final Logger LOG = LoggerFactory.getLogger(FlowBasedIngressServicesConfigUnbindHelper.class);
39
40     private final InterfacemgrProvider interfaceMgrProvider;
41     private static volatile FlowBasedServicesConfigRemovable flowBasedIngressServicesRemovable;
42
43     private FlowBasedIngressServicesConfigUnbindHelper(InterfacemgrProvider interfaceMgrProvider) {
44         this.interfaceMgrProvider = interfaceMgrProvider;
45     }
46
47     public static void intitializeFlowBasedIngressServicesConfigRemoveHelper(
48             InterfacemgrProvider interfaceMgrProvider) {
49         if (flowBasedIngressServicesRemovable == null) {
50             synchronized (FlowBasedIngressServicesConfigUnbindHelper.class) {
51                 if (flowBasedIngressServicesRemovable == null) {
52                     flowBasedIngressServicesRemovable = new FlowBasedIngressServicesConfigUnbindHelper(
53                             interfaceMgrProvider);
54                 }
55             }
56         }
57     }
58
59     public static FlowBasedServicesConfigRemovable getFlowBasedIngressServicesRemoveHelper() {
60         if (flowBasedIngressServicesRemovable == null) {
61             LOG.error("FlowBasedIngressBindHelper is not initialized");
62         }
63         return flowBasedIngressServicesRemovable;
64     }
65
66     public static void clearFlowBasedIngressServicesConfigUnbindHelper() {
67         flowBasedIngressServicesRemovable = null;
68     }
69
70     @Override
71     public List<ListenableFuture<Void>> unbindService(InstanceIdentifier<BoundServices> instanceIdentifier,
72                                                              BoundServices boundServiceOld) {
73
74         List<ListenableFuture<Void>> futures = new ArrayList<>();
75         DataBroker dataBroker = interfaceMgrProvider.getDataBroker();
76         String interfaceName =
77                 InstanceIdentifier.keyOf(instanceIdentifier.firstIdentifierOf(ServicesInfo.class)).getInterfaceName();
78         Class<? extends ServiceModeBase> serviceMode = InstanceIdentifier
79                 .keyOf(instanceIdentifier.firstIdentifierOf(ServicesInfo.class)).getServiceMode();
80
81         // Get the Parent ServiceInfo
82         ServicesInfo servicesInfo = FlowBasedServicesUtils.getServicesInfoForInterface(interfaceName, serviceMode,
83                 dataBroker);
84         if (servicesInfo == null) {
85             LOG.error("Reached Impossible part in the code for bound service: {}", boundServiceOld);
86             return futures;
87         }
88
89         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface
90             ifState = InterfaceManagerCommonUtils.getInterfaceStateFromOperDS(interfaceName, dataBroker);
91         if (ifState == null) {
92             LOG.info("Interface not operational, not unbinding Service for Interface: {}", interfaceName);
93             return futures;
94         }
95         List<BoundServices> boundServices = servicesInfo.getBoundServices();
96
97         // Split based on type of interface....
98         if (L2vlan.class.equals(ifState.getType())) {
99             return unbindServiceOnVlan(boundServiceOld, boundServices, ifState, dataBroker);
100         } else if (Tunnel.class.equals(ifState.getType())) {
101             return unbindServiceOnTunnel(boundServiceOld, boundServices, ifState, dataBroker);
102         }
103         return futures;
104     }
105
106     private static List<ListenableFuture<Void>> unbindServiceOnVlan(BoundServices boundServiceOld,
107             List<BoundServices> boundServices,
108             org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state
109             .Interface ifaceState, DataBroker dataBroker) {
110         LOG.info("unbinding ingress service {} for vlan port: {}", boundServiceOld.getServiceName(), ifaceState
111             .getName());
112         List<ListenableFuture<Void>> futures = new ArrayList<>();
113         WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
114         BigInteger dpId = FlowBasedServicesUtils.getDpnIdFromInterface(ifaceState);
115         if (boundServices.isEmpty()) {
116             // Remove default entry from Lport Dispatcher Table.
117             FlowBasedServicesUtils.removeLPortDispatcherFlow(dpId, ifaceState.getName(), boundServiceOld, tx,
118                     NwConstants.DEFAULT_SERVICE_INDEX);
119             if (tx != null) {
120                 futures.add(tx.submit());
121             }
122             return futures;
123         }
124         BoundServices[] highLow = FlowBasedServicesUtils.getHighAndLowPriorityService(boundServices, boundServiceOld);
125         BoundServices low = highLow[0];
126         BoundServices high = highLow[1];
127         // This means the one removed was the highest priority service
128         if (high == null) {
129             LOG.trace("Deleting ingress dispatcher table entry for service {}, match service index {}", boundServiceOld,
130                     NwConstants.DEFAULT_SERVICE_INDEX);
131             FlowBasedServicesUtils.removeLPortDispatcherFlow(dpId, ifaceState.getName(), boundServiceOld, tx,
132                     NwConstants.DEFAULT_SERVICE_INDEX);
133             if (low != null) {
134                 //delete the lower services flow entry.
135                 LOG.trace("Deleting ingress dispatcher table entry for lower service {}, match service index {}", low,
136                         low.getServicePriority());
137                 FlowBasedServicesUtils.removeLPortDispatcherFlow(dpId, ifaceState.getName(), low, tx,
138                         low.getServicePriority());
139                 BoundServices lower = FlowBasedServicesUtils.getHighAndLowPriorityService(boundServices, low)[0];
140                 short lowerServiceIndex = (short) (lower != null ? lower.getServicePriority()
141                         : low.getServicePriority() + 1);
142                 LOG.trace("Installing new ingress dispatcher table entry for lower service {}, match service index " +
143                         "{}, update service index {}",
144                         low, NwConstants.DEFAULT_SERVICE_INDEX, lowerServiceIndex);
145                 FlowBasedServicesUtils.installLPortDispatcherFlow(dpId, low, ifaceState.getName(), tx,
146                         ifaceState.getIfIndex(), NwConstants.DEFAULT_SERVICE_INDEX, lowerServiceIndex);
147             }
148         } else {
149             LOG.trace("Deleting ingress dispatcher table entry for service {}, match service index {}", boundServiceOld,
150                     boundServiceOld.getServicePriority());
151             FlowBasedServicesUtils.removeLPortDispatcherFlow(dpId, ifaceState.getName(), boundServiceOld, tx,
152                     boundServiceOld.getServicePriority());
153             short lowerServiceIndex = (short) (low != null ? low.getServicePriority()
154                     : boundServiceOld.getServicePriority() + 1);
155             BoundServices highest = FlowBasedServicesUtils.getHighestPriorityService(boundServices);
156             if (high.equals(highest)) {
157                 LOG.trace("Update the existing higher service {}, match service index {}, update service index {}",
158                         high, NwConstants.DEFAULT_SERVICE_INDEX, lowerServiceIndex);
159                 FlowBasedServicesUtils.installLPortDispatcherFlow(dpId, high, ifaceState.getName(), tx,
160                         ifaceState.getIfIndex(), NwConstants.DEFAULT_SERVICE_INDEX, lowerServiceIndex);
161             } else {
162                 LOG.trace("Update the existing higher service {}, match service index {}, update service index {}",
163                         high, high.getServicePriority(), lowerServiceIndex);
164                 FlowBasedServicesUtils.installLPortDispatcherFlow(dpId, high, ifaceState.getName(), tx,
165                         ifaceState.getIfIndex(), high.getServicePriority(), lowerServiceIndex);
166             }
167         }
168         futures.add(tx.submit());
169         return futures;
170     }
171
172     private static List<ListenableFuture<Void>> unbindServiceOnTunnel(BoundServices boundServiceOld,
173             List<BoundServices> boundServices,
174             org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state
175             .Interface ifState, DataBroker dataBroker) {
176         List<ListenableFuture<Void>> futures = new ArrayList<>();
177
178         Interface iface = InterfaceManagerCommonUtils.getInterfaceFromConfigDS(ifState.getName(), dataBroker);
179         WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
180         NodeConnectorId nodeConnectorId = FlowBasedServicesUtils.getNodeConnectorIdFromInterface(ifState);
181         long portNo = IfmUtil.getPortNumberFromNodeConnectorId(nodeConnectorId);
182         BigInteger dpId = IfmUtil.getDpnFromNodeConnectorId(nodeConnectorId);
183
184         if (boundServices.isEmpty()) {
185             // Remove entry from Ingress Table.
186             FlowBasedServicesUtils.removeIngressFlow(ifState.getName(), boundServiceOld, dpId, tx);
187             if (tx != null) {
188                 futures.add(tx.submit());
189             }
190             return futures;
191         }
192
193         Map<Short, BoundServices> tmpServicesMap = new ConcurrentHashMap<>();
194         short highestPriority = 0xFF;
195         for (BoundServices boundService : boundServices) {
196             tmpServicesMap.put(boundService.getServicePriority(), boundService);
197             if (boundService.getServicePriority() < highestPriority) {
198                 highestPriority = boundService.getServicePriority();
199             }
200         }
201
202         if (highestPriority < boundServiceOld.getServicePriority()) {
203             FlowBasedServicesUtils.removeLPortDispatcherFlow(dpId, ifState.getName(), boundServiceOld, tx,
204                     boundServiceOld.getServicePriority());
205             if (tx != null) {
206                 futures.add(tx.submit());
207             }
208             return futures;
209         }
210
211         List<MatchInfo> matches = null;
212         matches = FlowBasedServicesUtils.getMatchInfoForTunnelPortAtIngressTable(dpId, portNo);
213
214         BoundServices toBeMoved = tmpServicesMap.get(highestPriority);
215         FlowBasedServicesUtils.removeIngressFlow(iface.getName(), boundServiceOld, dpId, tx);
216         FlowBasedServicesUtils.installInterfaceIngressFlow(dpId, iface, toBeMoved, tx,
217                 matches, ifState.getIfIndex(), NwConstants.VLAN_INTERFACE_INGRESS_TABLE);
218         FlowBasedServicesUtils.removeLPortDispatcherFlow(dpId, iface.getName(), toBeMoved, tx,
219                 toBeMoved.getServicePriority());
220
221         if (tx != null) {
222             futures.add(tx.submit());
223         }
224         return futures;
225     }
226
227 }