L2 Gw create changes related to ITM Tunnels creation in neutronvpn module
[vpnservice.git] / interfacemgr / interfacemgr-impl / src / main / java / org / opendaylight / vpnservice / interfacemgr / servicebindings / flowbased / confighelpers / FlowBasedServicesConfigUnbindHelper.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.confighelpers;
9
10 import java.math.BigInteger;
11 import java.util.ArrayList;
12 import java.util.List;
13 import java.util.Map;
14 import java.util.concurrent.ConcurrentHashMap;
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.vpnservice.mdsalutil.MatchInfo;
23 import org.opendaylight.vpnservice.mdsalutil.NwConstants;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.L2vlan;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.Tunnel;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
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.inventory.rev130819.NodeConnectorId;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.servicebinding.rev151015.service.bindings.ServicesInfo;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.servicebinding.rev151015.service.bindings.services.info.BoundServices;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 import com.google.common.util.concurrent.ListenableFuture;
37
38 public class FlowBasedServicesConfigUnbindHelper {
39     private static final Logger LOG = LoggerFactory.getLogger(FlowBasedServicesConfigUnbindHelper.class);
40
41     public static List<ListenableFuture<Void>> unbindService(InstanceIdentifier<BoundServices> instanceIdentifier,
42                                                              BoundServices boundServiceOld, DataBroker dataBroker) {
43         List<ListenableFuture<Void>> futures = new ArrayList<>();
44         String interfaceName =
45                 InstanceIdentifier.keyOf(instanceIdentifier.firstIdentifierOf(ServicesInfo.class)).getInterfaceName();
46
47         // Get the Parent ServiceInfo
48         ServicesInfo servicesInfo = FlowBasedServicesUtils.getServicesInfoForInterface(interfaceName, dataBroker);
49         if (servicesInfo == null) {
50             LOG.error("Reached Impossible part in the code for bound service: {}", boundServiceOld);
51             return futures;
52         }
53
54         InterfaceKey interfaceKey = new InterfaceKey(interfaceName);
55         Interface iface = InterfaceManagerCommonUtils.getInterfaceFromConfigDS(interfaceKey, dataBroker);
56         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface ifState =
57                 InterfaceManagerCommonUtils.getInterfaceStateFromOperDS(interfaceName, dataBroker);
58         if (ifState == null || ifState.getOperStatus() == OperStatus.Down) {
59             LOG.info("Not unbinding Service since operstatus is {} for Interface: {}",
60                     ifState.getOperStatus(), interfaceName);
61             return futures;
62         }
63         List<BoundServices> boundServices = servicesInfo.getBoundServices();
64
65         // Split based on type of interface....
66         if (iface.getType().isAssignableFrom(L2vlan.class)) {
67             return unbindServiceOnVlan(boundServiceOld, boundServices, iface, ifState.getIfIndex(), dataBroker);
68         } else if (iface.getType().isAssignableFrom(Tunnel.class)) {
69            return unbindServiceOnTunnel(boundServiceOld, boundServices, iface, ifState.getIfIndex(), dataBroker);
70         }
71         return futures;
72     }
73
74     private static List<ListenableFuture<Void>> unbindServiceOnVlan(
75             BoundServices boundServiceOld,
76             List<BoundServices> boundServices, Interface iface, int ifIndex,
77             DataBroker dataBroker) {
78
79         List<ListenableFuture<Void>> futures = new ArrayList<>();
80         WriteTransaction t = dataBroker.newWriteOnlyTransaction();
81         NodeConnectorId nodeConnectorId = FlowBasedServicesUtils.getNodeConnectorIdFromInterface(iface, dataBroker);
82         BigInteger dpId = new BigInteger(IfmUtil.getDpnFromNodeConnectorId(nodeConnectorId));
83         if (boundServices.isEmpty()) {
84             // Remove default entry from Lport Dispatcher Table.
85             FlowBasedServicesUtils.removeLPortDispatcherFlow(dpId, iface.getName(), boundServiceOld, t, IfmConstants.DEFAULT_SERVICE_INDEX);
86             if (t != null) {
87                 futures.add(t.submit());
88             }
89             return futures;
90         }
91         BoundServices[] highLow = FlowBasedServicesUtils.getHighAndLowPriorityService(boundServices, boundServiceOld);
92         BoundServices low = highLow[0];
93         BoundServices high = highLow[1];
94         // This means the one removed was the highest priority service
95         if (high == null) {
96             LOG.trace("Deleting table entry for service {}, match service index {}", boundServiceOld, IfmConstants.DEFAULT_SERVICE_INDEX);
97             FlowBasedServicesUtils.removeLPortDispatcherFlow(dpId, iface.getName(), boundServiceOld, t, IfmConstants.DEFAULT_SERVICE_INDEX);
98             if (low != null) {
99                 //delete the lower services flow entry.
100                 LOG.trace("Deleting table entry for lower service {}, match service index {}", low, low.getServicePriority());
101                 FlowBasedServicesUtils.removeLPortDispatcherFlow(dpId, iface.getName(), low, t, low.getServicePriority());
102                 BoundServices lower = FlowBasedServicesUtils.getHighAndLowPriorityService(boundServices, low)[0];
103                 short lowerServiceIndex = (short) ((lower!=null) ? lower.getServicePriority() : low.getServicePriority() + 1);
104                 LOG.trace("Installing new entry for lower service {}, match service index {}, update service index {}", low, IfmConstants.DEFAULT_SERVICE_INDEX, lowerServiceIndex);
105                 FlowBasedServicesUtils.installLPortDispatcherFlow(dpId, low, iface, t, ifIndex, IfmConstants.DEFAULT_SERVICE_INDEX, lowerServiceIndex);
106             }
107         } else {
108             LOG.trace("Deleting table entry for service {}, match service index {}", boundServiceOld, boundServiceOld.getServicePriority());
109             FlowBasedServicesUtils.removeLPortDispatcherFlow(dpId, iface.getName(), boundServiceOld, t, boundServiceOld.getServicePriority());
110             short lowerServiceIndex = (short) ((low!=null) ? low.getServicePriority() : boundServiceOld.getServicePriority() + 1);
111             BoundServices highest = FlowBasedServicesUtils.getHighestPriorityService(boundServices);
112             if (high.equals(highest)) {
113                 LOG.trace("Update the existing higher service {}, match service index {}, update service index {}", high, IfmConstants.DEFAULT_SERVICE_INDEX, lowerServiceIndex);
114                 FlowBasedServicesUtils.installLPortDispatcherFlow(dpId, high, iface, t, ifIndex, IfmConstants.DEFAULT_SERVICE_INDEX, lowerServiceIndex);
115             } else {
116                 LOG.trace("Update the existing higher service {}, match service index {}, update service index {}", high, high.getServicePriority(), lowerServiceIndex);
117                 FlowBasedServicesUtils.installLPortDispatcherFlow(dpId, high, iface, t, ifIndex, high.getServicePriority(), lowerServiceIndex);
118             }
119         }
120         futures.add(t.submit());
121         return futures;
122     }
123
124     private static List<ListenableFuture<Void>> unbindServiceOnTunnel(
125             BoundServices boundServiceOld,
126             List<BoundServices> boundServices, Interface iface, int ifIndex,
127             DataBroker dataBroker) {
128         List<ListenableFuture<Void>> futures = new ArrayList<>();
129
130         WriteTransaction t = dataBroker.newWriteOnlyTransaction();
131         NodeConnectorId nodeConnectorId = FlowBasedServicesUtils.getNodeConnectorIdFromInterface(iface, dataBroker);
132         long portNo = Long.parseLong(IfmUtil.getPortNoFromNodeConnectorId(nodeConnectorId));
133         BigInteger dpId = new BigInteger(IfmUtil.getDpnFromNodeConnectorId(nodeConnectorId));
134
135         if (boundServices.isEmpty()) {
136             // Remove entry from Ingress Table.
137             FlowBasedServicesUtils.removeIngressFlow(iface.getName(), boundServiceOld, dpId, t);
138             if (t != null) {
139                 futures.add(t.submit());
140             }
141             return futures;
142         }
143
144         Map<Short, BoundServices> tmpServicesMap = new ConcurrentHashMap<>();
145         short highestPriority = 0xFF;
146         for (BoundServices boundService : boundServices) {
147             tmpServicesMap.put(boundService.getServicePriority(), boundService);
148             if (boundService.getServicePriority() < highestPriority) {
149                 highestPriority = boundService.getServicePriority();
150             }
151         }
152
153         if (highestPriority < boundServiceOld.getServicePriority()) {
154             FlowBasedServicesUtils.removeLPortDispatcherFlow(dpId, iface.getName(), boundServiceOld, t, boundServiceOld.getServicePriority());
155             if (t != null) {
156                 futures.add(t.submit());
157             }
158             return futures;
159         }
160
161         List<MatchInfo> matches = null;
162         matches = FlowBasedServicesUtils.getMatchInfoForTunnelPortAtIngressTable (dpId, portNo, iface);
163
164         BoundServices toBeMoved = tmpServicesMap.get(highestPriority);
165         FlowBasedServicesUtils.removeIngressFlow(iface.getName(), boundServiceOld, dpId, t);
166         FlowBasedServicesUtils.installInterfaceIngressFlow(dpId, iface, toBeMoved, t,
167                 matches, ifIndex, NwConstants.VLAN_INTERFACE_INGRESS_TABLE);
168         FlowBasedServicesUtils.removeLPortDispatcherFlow(dpId, iface.getName(), toBeMoved, t, toBeMoved.getServicePriority());
169
170         if (t != null) {
171             futures.add(t.submit());
172         }
173         return futures;
174     }
175
176 }