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