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.servicebindings.flowbased.utilities.FlowBasedServicesUtils;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.L2vlan;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.Tunnel;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
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.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 import com.google.common.util.concurrent.ListenableFuture;
32 public class FlowBasedServicesStateUnbindHelper {
33 private static final Logger LOG = LoggerFactory.getLogger(FlowBasedServicesStateUnbindHelper.class);
35 public static List<ListenableFuture<Void>> unbindServicesFromInterface(Interface ifaceState,
36 DataBroker dataBroker) {
37 List<ListenableFuture<Void>> futures = new ArrayList<>();
39 ServicesInfo servicesInfo = FlowBasedServicesUtils.getServicesInfoForInterface(ifaceState.getName(), dataBroker);
40 if (servicesInfo == null) {
44 List<BoundServices> allServices = servicesInfo.getBoundServices();
45 if (allServices == null || allServices.isEmpty()) {
49 if (ifaceState.getType().isAssignableFrom(L2vlan.class)) {
50 return unbindServiceOnVlan(allServices, ifaceState, ifaceState.getIfIndex(), dataBroker);
51 } else if (ifaceState.getType().isAssignableFrom(Tunnel.class)){
52 return unbindServiceOnTunnel(allServices, ifaceState, ifaceState.getIfIndex(), dataBroker);
57 private static List<ListenableFuture<Void>> unbindServiceOnTunnel(
58 List<BoundServices> allServices,
60 Integer ifIndex, DataBroker dataBroker) {
61 List<ListenableFuture<Void>> futures = new ArrayList<>();
62 WriteTransaction t = dataBroker.newWriteOnlyTransaction();
64 List<String> ofportIds = iface.getLowerLayerIf();
65 NodeConnectorId nodeConnectorId = new NodeConnectorId(ofportIds.get(0));
66 if(nodeConnectorId == null){
69 BoundServices highestPriorityBoundService = FlowBasedServicesUtils.getHighestPriorityService(allServices);
71 BigInteger dpId = new BigInteger(IfmUtil.getDpnFromNodeConnectorId(nodeConnectorId));
72 FlowBasedServicesUtils.removeIngressFlow(iface.getName(), highestPriorityBoundService, dpId, t);
74 for (BoundServices boundService : allServices) {
75 if (!boundService.equals(highestPriorityBoundService)) {
76 FlowBasedServicesUtils.removeLPortDispatcherFlow(dpId, iface.getName(), boundService, t, boundService.getServicePriority());
80 futures.add(t.submit());
84 private static List<ListenableFuture<Void>> unbindServiceOnVlan(
85 List<BoundServices> allServices, Interface ifaceState,
86 Integer ifIndex, DataBroker dataBroker) {
87 List<ListenableFuture<Void>> futures = new ArrayList<>();
88 WriteTransaction t = dataBroker.newWriteOnlyTransaction();
89 List<String> ofportIds = ifaceState.getLowerLayerIf();
90 NodeConnectorId nodeConnectorId = new NodeConnectorId(ofportIds.get(0));
91 if(nodeConnectorId == null){
94 BigInteger dpId = new BigInteger(IfmUtil.getDpnFromNodeConnectorId(nodeConnectorId));
95 Collections.sort(allServices, new Comparator<BoundServices>() {
97 public int compare(BoundServices serviceInfo1, BoundServices serviceInfo2) {
98 return serviceInfo1.getServicePriority().compareTo(serviceInfo2.getServicePriority());
101 BoundServices highestPriority = allServices.remove(0);
102 FlowBasedServicesUtils.removeLPortDispatcherFlow(dpId, ifaceState.getName(), highestPriority, t, IfmConstants.DEFAULT_SERVICE_INDEX);
103 for (BoundServices boundService : allServices) {
104 FlowBasedServicesUtils.removeLPortDispatcherFlow(dpId, ifaceState.getName(), boundService, t, boundService.getServicePriority());
106 futures.add(t.submit());