aa0b9bc29641e23ce0ff5566fa66668206a2b2fd
[vpnservice.git] / interfacemgr / interfacemgr-impl / src / main / java / org / opendaylight / vpnservice / interfacemgr / servicebindings / flowbased / statehelpers / FlowBasedServicesStateUnbindHelper.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.statehelpers;
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.IfmUtil;
14 import org.opendaylight.vpnservice.interfacemgr.commons.InterfaceManagerCommonUtils;
15 import org.opendaylight.vpnservice.interfacemgr.servicebindings.flowbased.utilities.FlowBasedServicesUtils;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.servicebinding.rev151015.service.bindings.ServicesInfo;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.servicebinding.rev151015.service.bindings.services.info.BoundServices;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 import java.math.BigInteger;
25 import java.util.ArrayList;
26 import java.util.List;
27
28 public class FlowBasedServicesStateUnbindHelper {
29     private static final Logger LOG = LoggerFactory.getLogger(FlowBasedServicesStateUnbindHelper.class);
30
31     public static List<ListenableFuture<Void>> unbindServicesFromInterface(Interface ifaceState,
32                                                                            DataBroker dataBroker) {
33         List<ListenableFuture<Void>> futures = new ArrayList<>();
34         WriteTransaction t = dataBroker.newWriteOnlyTransaction();
35
36         ServicesInfo servicesInfo = FlowBasedServicesUtils.getServicesInfoForInterface(ifaceState.getName(), dataBroker);
37         if (servicesInfo == null) {
38             return futures;
39         }
40
41         List<BoundServices> allServices = servicesInfo.getBoundServices();
42         if (allServices == null || allServices.isEmpty()) {
43             return futures;
44         }
45
46         BoundServices highestPriorityBoundService = null;
47         short highestPriority = 0xFF;
48         for (BoundServices boundService : allServices) {
49             if (boundService.getServicePriority() < highestPriority) {
50                 highestPriorityBoundService = boundService;
51                 highestPriority = boundService.getServicePriority();
52             }
53         }
54
55         InterfaceKey interfaceKey = new InterfaceKey(ifaceState.getName());
56         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface iface =
57                 InterfaceManagerCommonUtils.getInterfaceFromConfigDS(interfaceKey, dataBroker);
58
59         NodeConnectorId nodeConnectorId = FlowBasedServicesUtils.getNodeConnectorIdFromInterface(iface, dataBroker);
60         if(nodeConnectorId == null){
61             return futures;
62         }
63         BigInteger dpId = new BigInteger(IfmUtil.getDpnFromNodeConnectorId(nodeConnectorId));
64         FlowBasedServicesUtils.removeIngressFlow(iface, highestPriorityBoundService, dpId, t);
65
66         for (BoundServices boundService : allServices) {
67             if (!boundService.equals(highestPriorityBoundService)) {
68                 FlowBasedServicesUtils.removeLPortDispatcherFlow(dpId, iface, boundService, t);
69             }
70         }
71
72         return futures;
73     }
74 }