2 * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
8 package org.opendaylight.vpnservice.interfacemgr.servicebindings.flowbased.statehelpers;
10 import java.math.BigInteger;
11 import java.util.ArrayList;
12 import java.util.Collections;
13 import java.util.Comparator;
14 import java.util.List;
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.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.L2vlan;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.Tunnel;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.servicebinding.rev151015.service.bindings.ServicesInfo;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.servicebinding.rev151015.service.bindings.services.info.BoundServices;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 import com.google.common.util.concurrent.ListenableFuture;
34 public class FlowBasedServicesStateUnbindHelper {
35 private static final Logger LOG = LoggerFactory.getLogger(FlowBasedServicesStateUnbindHelper.class);
37 public static List<ListenableFuture<Void>> unbindServicesFromInterface(Interface ifaceState,
38 DataBroker dataBroker) {
39 List<ListenableFuture<Void>> futures = new ArrayList<>();
41 ServicesInfo servicesInfo = FlowBasedServicesUtils.getServicesInfoForInterface(ifaceState.getName(), dataBroker);
42 if (servicesInfo == null) {
46 List<BoundServices> allServices = servicesInfo.getBoundServices();
47 if (allServices == null || allServices.isEmpty()) {
51 InterfaceKey interfaceKey = new InterfaceKey(ifaceState.getName());
52 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface iface =
53 InterfaceManagerCommonUtils.getInterfaceFromConfigDS(interfaceKey, dataBroker);
55 if (ifaceState.getType().isAssignableFrom(L2vlan.class)) {
56 return unbindServiceOnVlan(allServices, iface, ifaceState.getIfIndex(), dataBroker);
57 } else if (ifaceState.getType().isAssignableFrom(Tunnel.class)){
58 return unbindServiceOnTunnel(allServices, iface, ifaceState.getIfIndex(), dataBroker);
63 private static List<ListenableFuture<Void>> unbindServiceOnTunnel(
64 List<BoundServices> allServices,
65 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface iface,
66 Integer ifIndex, DataBroker dataBroker) {
67 List<ListenableFuture<Void>> futures = new ArrayList<>();
68 WriteTransaction t = dataBroker.newWriteOnlyTransaction();
70 NodeConnectorId nodeConnectorId = FlowBasedServicesUtils.getNodeConnectorIdFromInterface(iface, dataBroker);
71 if(nodeConnectorId == null){
74 BoundServices highestPriorityBoundService = FlowBasedServicesUtils.getHighestPriorityService(allServices);
76 BigInteger dpId = new BigInteger(IfmUtil.getDpnFromNodeConnectorId(nodeConnectorId));
77 FlowBasedServicesUtils.removeIngressFlow(iface, highestPriorityBoundService, dpId, t);
79 for (BoundServices boundService : allServices) {
80 if (!boundService.equals(highestPriorityBoundService)) {
81 FlowBasedServicesUtils.removeLPortDispatcherFlow(dpId, iface, boundService, t, boundService.getServicePriority());
85 futures.add(t.submit());
89 private static List<ListenableFuture<Void>> unbindServiceOnVlan(
90 List<BoundServices> allServices,
91 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface iface,
92 Integer ifIndex, DataBroker dataBroker) {
93 List<ListenableFuture<Void>> futures = new ArrayList<>();
94 WriteTransaction t = dataBroker.newWriteOnlyTransaction();
95 NodeConnectorId nodeConnectorId = FlowBasedServicesUtils.getNodeConnectorIdFromInterface(iface, dataBroker);
96 if (nodeConnectorId == null) {
99 BigInteger dpId = new BigInteger(IfmUtil.getDpnFromNodeConnectorId(nodeConnectorId));
100 Collections.sort(allServices, new Comparator<BoundServices>() {
102 public int compare(BoundServices serviceInfo1, BoundServices serviceInfo2) {
103 return serviceInfo2.getServicePriority().compareTo(serviceInfo1.getServicePriority());
106 BoundServices highestPriority = allServices.remove(0);
107 FlowBasedServicesUtils.removeLPortDispatcherFlow(dpId, iface, highestPriority, t, IfmConstants.DEFAULT_SERVICE_INDEX);
108 for (BoundServices boundService : allServices) {
109 FlowBasedServicesUtils.removeLPortDispatcherFlow(dpId, iface, boundService, t, boundService.getServicePriority());
111 futures.add(t.submit());