cd0c00d94d186675c0177b4de8b7f2c134eb18ee
[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 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.vpnservice.interfacemgr.IfmConstants;
14 import org.opendaylight.vpnservice.interfacemgr.IfmUtil;
15 import org.opendaylight.vpnservice.interfacemgr.commons.InterfaceManagerCommonUtils;
16 import org.opendaylight.vpnservice.interfacemgr.servicebindings.flowbased.utilities.FlowBasedServicesUtils;
17 import org.opendaylight.vpnservice.mdsalutil.MatchInfo;
18 import org.opendaylight.vpnservice.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.InterfaceKey;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.servicebinding.rev151015.service.bindings.ServicesInfo;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.servicebinding.rev151015.service.bindings.services.info.BoundServices;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.IfL2vlan;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 import java.math.BigInteger;
33 import java.util.ArrayList;
34 import java.util.List;
35 import java.util.Map;
36 import java.util.concurrent.ConcurrentHashMap;
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         WriteTransaction t = dataBroker.newWriteOnlyTransaction();
45
46         String interfaceName =
47                 InstanceIdentifier.keyOf(instanceIdentifier.firstIdentifierOf(ServicesInfo.class)).getInterfaceName();
48         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface ifState =
49                 InterfaceManagerCommonUtils.getInterfaceStateFromOperDS(interfaceName, dataBroker);
50         if (ifState == null || ifState.getOperStatus() == OperStatus.Down) {
51             LOG.info("Not unbinding Service since operstatus is {} for Interface: {}",
52                     ifState.getOperStatus(), interfaceName);
53             return futures;
54         }
55
56         // Get the Parent ServiceInfo
57         ServicesInfo servicesInfo = FlowBasedServicesUtils.getServicesInfoForInterface(interfaceName, dataBroker);
58         if (servicesInfo == null) {
59             LOG.error("Reached Impossible part in the code for bound service: {}", boundServiceOld);
60             return futures;
61         }
62
63         InterfaceKey interfaceKey = new InterfaceKey(interfaceName);
64         Interface iface = InterfaceManagerCommonUtils.getInterfaceFromConfigDS(interfaceKey, dataBroker);
65         NodeConnectorId nodeConnectorId = FlowBasedServicesUtils.getNodeConnectorIdFromInterface(iface, dataBroker);
66         long portNo = Long.parseLong(IfmUtil.getPortNoFromNodeConnectorId(nodeConnectorId));
67         BigInteger dpId = new BigInteger(IfmUtil.getDpnFromNodeConnectorId(nodeConnectorId));
68
69         List<BoundServices> boundServices = servicesInfo.getBoundServices();
70         if (boundServices.isEmpty()) {
71             // Remove entry from Ingress Table.
72             FlowBasedServicesUtils.removeIngressFlow(iface, boundServiceOld, dpId, t);
73             if (t != null) {
74                 futures.add(t.submit());
75             }
76             return futures;
77         }
78
79         Map<Short, BoundServices> tmpServicesMap = new ConcurrentHashMap<>();
80         short highestPriority = 0xFF;
81         for (BoundServices boundService : boundServices) {
82             tmpServicesMap.put(boundService.getServicePriority(), boundService);
83             if (boundService.getServicePriority() < highestPriority) {
84                 highestPriority = boundService.getServicePriority();
85             }
86         }
87
88         if (highestPriority < boundServiceOld.getServicePriority()) {
89             FlowBasedServicesUtils.removeLPortDispatcherFlow(dpId, iface, boundServiceOld, t);
90             if (t != null) {
91                 futures.add(t.submit());
92             }
93             return futures;
94         }
95
96         List<MatchInfo> matches = null;
97         if (iface.getType().isAssignableFrom(L2vlan.class)) {
98             matches = FlowBasedServicesUtils.getMatchInfoForVlanPortAtIngressTable(dpId, portNo, iface);
99         } else if (iface.getType().isAssignableFrom(Tunnel.class)){
100             matches = FlowBasedServicesUtils.getMatchInfoForTunnelPortAtIngressTable (dpId, portNo, iface);
101         }
102
103         BoundServices toBeMoved = tmpServicesMap.get(highestPriority);
104         FlowBasedServicesUtils.removeIngressFlow(iface, boundServiceOld, dpId, t);
105         FlowBasedServicesUtils.installInterfaceIngressFlow(dpId, iface, toBeMoved, t,
106                 matches, ifState.getIfIndex(), NwConstants.VLAN_INTERFACE_INGRESS_TABLE);
107         FlowBasedServicesUtils.removeLPortDispatcherFlow(dpId, iface, toBeMoved, t);
108
109         if (t != null) {
110             futures.add(t.submit());
111         }
112         return futures;
113     }
114 }