Merge "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 - 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.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 DOWN for Interface: {}", interfaceName);
60             return futures;
61         }
62         List<BoundServices> boundServices = servicesInfo.getBoundServices();
63
64         // Split based on type of interface....
65         if (iface.getType().isAssignableFrom(L2vlan.class)) {
66             return unbindServiceOnVlan(boundServiceOld, boundServices, iface, ifState.getIfIndex(), dataBroker);
67         } else if (iface.getType().isAssignableFrom(Tunnel.class)) {
68            return unbindServiceOnTunnel(boundServiceOld, boundServices, iface, ifState.getIfIndex(), dataBroker);
69         }
70         return futures;
71     }
72
73     private static List<ListenableFuture<Void>> unbindServiceOnVlan(
74             BoundServices boundServiceOld,
75             List<BoundServices> boundServices, Interface iface, int ifIndex,
76             DataBroker dataBroker) {
77
78         List<ListenableFuture<Void>> futures = new ArrayList<>();
79         WriteTransaction t = dataBroker.newWriteOnlyTransaction();
80         NodeConnectorId nodeConnectorId = FlowBasedServicesUtils.getNodeConnectorIdFromInterface(iface, dataBroker);
81         BigInteger dpId = new BigInteger(IfmUtil.getDpnFromNodeConnectorId(nodeConnectorId));
82         if (boundServices.isEmpty()) {
83             // Remove default entry from Lport Dispatcher Table.
84             FlowBasedServicesUtils.removeLPortDispatcherFlow(dpId, iface.getName(), boundServiceOld, t, IfmConstants.DEFAULT_SERVICE_INDEX);
85             if (t != null) {
86                 futures.add(t.submit());
87             }
88             return futures;
89         }
90         BoundServices[] highLow = FlowBasedServicesUtils.getHighAndLowPriorityService(boundServices, boundServiceOld);
91         BoundServices low = highLow[0];
92         BoundServices high = highLow[1];
93         // This means the one removed was the highest priority service
94         if (high == null) {
95             LOG.trace("Deleting table entry for service {}, match service index {}", boundServiceOld, IfmConstants.DEFAULT_SERVICE_INDEX);
96             FlowBasedServicesUtils.removeLPortDispatcherFlow(dpId, iface.getName(), boundServiceOld, t, IfmConstants.DEFAULT_SERVICE_INDEX);
97             if (low != null) {
98                 //delete the lower services flow entry.
99                 LOG.trace("Deleting table entry for lower service {}, match service index {}", low, low.getServicePriority());
100                 FlowBasedServicesUtils.removeLPortDispatcherFlow(dpId, iface.getName(), low, t, low.getServicePriority());
101                 BoundServices lower = FlowBasedServicesUtils.getHighAndLowPriorityService(boundServices, low)[0];
102                 short lowerServiceIndex = (short) ((lower!=null) ? lower.getServicePriority() : low.getServicePriority() + 1);
103                 LOG.trace("Installing new entry for lower service {}, match service index {}, update service index {}", low, IfmConstants.DEFAULT_SERVICE_INDEX, lowerServiceIndex);
104                 FlowBasedServicesUtils.installLPortDispatcherFlow(dpId, low, iface, t, ifIndex, IfmConstants.DEFAULT_SERVICE_INDEX, lowerServiceIndex);
105             }
106         } else {
107             LOG.trace("Deleting table entry for service {}, match service index {}", boundServiceOld, boundServiceOld.getServicePriority());
108             FlowBasedServicesUtils.removeLPortDispatcherFlow(dpId, iface.getName(), boundServiceOld, t, boundServiceOld.getServicePriority());
109             short lowerServiceIndex = (short) ((low!=null) ? low.getServicePriority() : boundServiceOld.getServicePriority() + 1);
110             BoundServices highest = FlowBasedServicesUtils.getHighestPriorityService(boundServices);
111             if (high.equals(highest)) {
112                 LOG.trace("Update the existing higher service {}, match service index {}, update service index {}", high, IfmConstants.DEFAULT_SERVICE_INDEX, lowerServiceIndex);
113                 FlowBasedServicesUtils.installLPortDispatcherFlow(dpId, high, iface, t, ifIndex, IfmConstants.DEFAULT_SERVICE_INDEX, lowerServiceIndex);
114             } else {
115                 LOG.trace("Update the existing higher service {}, match service index {}, update service index {}", high, high.getServicePriority(), lowerServiceIndex);
116                 FlowBasedServicesUtils.installLPortDispatcherFlow(dpId, high, iface, t, ifIndex, high.getServicePriority(), lowerServiceIndex);
117             }
118         }
119         futures.add(t.submit());
120         return futures;
121     }
122
123     private static List<ListenableFuture<Void>> unbindServiceOnTunnel(
124             BoundServices boundServiceOld,
125             List<BoundServices> boundServices, Interface iface, int ifIndex,
126             DataBroker dataBroker) {
127         List<ListenableFuture<Void>> futures = new ArrayList<>();
128
129         WriteTransaction t = dataBroker.newWriteOnlyTransaction();
130         NodeConnectorId nodeConnectorId = FlowBasedServicesUtils.getNodeConnectorIdFromInterface(iface, dataBroker);
131         long portNo = Long.parseLong(IfmUtil.getPortNoFromNodeConnectorId(nodeConnectorId));
132         BigInteger dpId = new BigInteger(IfmUtil.getDpnFromNodeConnectorId(nodeConnectorId));
133
134         if (boundServices.isEmpty()) {
135             // Remove entry from Ingress Table.
136             FlowBasedServicesUtils.removeIngressFlow(iface.getName(), boundServiceOld, dpId, t);
137             if (t != null) {
138                 futures.add(t.submit());
139             }
140             return futures;
141         }
142
143         Map<Short, BoundServices> tmpServicesMap = new ConcurrentHashMap<>();
144         short highestPriority = 0xFF;
145         for (BoundServices boundService : boundServices) {
146             tmpServicesMap.put(boundService.getServicePriority(), boundService);
147             if (boundService.getServicePriority() < highestPriority) {
148                 highestPriority = boundService.getServicePriority();
149             }
150         }
151
152         if (highestPriority < boundServiceOld.getServicePriority()) {
153             FlowBasedServicesUtils.removeLPortDispatcherFlow(dpId, iface.getName(), boundServiceOld, t, boundServiceOld.getServicePriority());
154             if (t != null) {
155                 futures.add(t.submit());
156             }
157             return futures;
158         }
159
160         List<MatchInfo> matches = null;
161         matches = FlowBasedServicesUtils.getMatchInfoForTunnelPortAtIngressTable (dpId, portNo, iface);
162
163         BoundServices toBeMoved = tmpServicesMap.get(highestPriority);
164         FlowBasedServicesUtils.removeIngressFlow(iface.getName(), boundServiceOld, dpId, t);
165         FlowBasedServicesUtils.installInterfaceIngressFlow(dpId, iface, toBeMoved, t,
166                 matches, ifIndex, NwConstants.VLAN_INTERFACE_INGRESS_TABLE);
167         FlowBasedServicesUtils.removeLPortDispatcherFlow(dpId, iface.getName(), toBeMoved, t, toBeMoved.getServicePriority());
168
169         if (t != null) {
170             futures.add(t.submit());
171         }
172         return futures;
173     }
174
175 }