2 * Copyright (c) 2015 - 2016 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.confighelpers;
10 import java.math.BigInteger;
11 import java.util.ArrayList;
12 import java.util.List;
14 import java.util.concurrent.ConcurrentHashMap;
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.vpnservice.mdsalutil.MatchInfo;
23 import org.opendaylight.vpnservice.mdsalutil.NwConstants;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.L2vlan;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.Tunnel;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.servicebinding.rev151015.service.bindings.ServicesInfo;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.servicebinding.rev151015.service.bindings.services.info.BoundServices;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 import com.google.common.util.concurrent.ListenableFuture;
38 public class FlowBasedServicesConfigUnbindHelper {
39 private static final Logger LOG = LoggerFactory.getLogger(FlowBasedServicesConfigUnbindHelper.class);
41 public static List<ListenableFuture<Void>> unbindService(InstanceIdentifier<BoundServices> instanceIdentifier,
42 BoundServices boundServiceOld, DataBroker dataBroker) {
43 List<ListenableFuture<Void>> futures = new ArrayList<>();
44 String interfaceName =
45 InstanceIdentifier.keyOf(instanceIdentifier.firstIdentifierOf(ServicesInfo.class)).getInterfaceName();
47 // Get the Parent ServiceInfo
48 ServicesInfo servicesInfo = FlowBasedServicesUtils.getServicesInfoForInterface(interfaceName, dataBroker);
49 if (servicesInfo == null) {
50 LOG.error("Reached Impossible part in the code for bound service: {}", boundServiceOld);
54 InterfaceKey interfaceKey = new InterfaceKey(interfaceName);
55 Interface iface = InterfaceManagerCommonUtils.getInterfaceFromConfigDS(interfaceKey, dataBroker);
56 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface ifState =
57 InterfaceManagerCommonUtils.getInterfaceStateFromOperDS(interfaceName, dataBroker);
58 if (ifState == null || ifState.getOperStatus() == OperStatus.Down) {
59 LOG.info("Not unbinding Service since operstatus is DOWN for Interface: {}", interfaceName);
62 List<BoundServices> boundServices = servicesInfo.getBoundServices();
64 // Split based on type of interface....
65 if (iface.getType().isAssignableFrom(L2vlan.class)) {
66 return unbindServiceOnVlan(boundServiceOld, boundServices, iface, ifState.getIfIndex(), dataBroker);
67 } else if (iface.getType().isAssignableFrom(Tunnel.class)) {
68 return unbindServiceOnTunnel(boundServiceOld, boundServices, iface, ifState.getIfIndex(), dataBroker);
73 private static List<ListenableFuture<Void>> unbindServiceOnVlan(
74 BoundServices boundServiceOld,
75 List<BoundServices> boundServices, Interface iface, int ifIndex,
76 DataBroker dataBroker) {
78 List<ListenableFuture<Void>> futures = new ArrayList<>();
79 WriteTransaction t = dataBroker.newWriteOnlyTransaction();
80 NodeConnectorId nodeConnectorId = FlowBasedServicesUtils.getNodeConnectorIdFromInterface(iface, dataBroker);
81 BigInteger dpId = new BigInteger(IfmUtil.getDpnFromNodeConnectorId(nodeConnectorId));
82 if (boundServices.isEmpty()) {
83 // Remove default entry from Lport Dispatcher Table.
84 FlowBasedServicesUtils.removeLPortDispatcherFlow(dpId, iface.getName(), boundServiceOld, t, IfmConstants.DEFAULT_SERVICE_INDEX);
86 futures.add(t.submit());
90 BoundServices[] highLow = FlowBasedServicesUtils.getHighAndLowPriorityService(boundServices, boundServiceOld);
91 BoundServices low = highLow[0];
92 BoundServices high = highLow[1];
93 // This means the one removed was the highest priority service
95 LOG.trace("Deleting table entry for service {}, match service index {}", boundServiceOld, IfmConstants.DEFAULT_SERVICE_INDEX);
96 FlowBasedServicesUtils.removeLPortDispatcherFlow(dpId, iface.getName(), boundServiceOld, t, IfmConstants.DEFAULT_SERVICE_INDEX);
98 //delete the lower services flow entry.
99 LOG.trace("Deleting table entry for lower service {}, match service index {}", low, low.getServicePriority());
100 FlowBasedServicesUtils.removeLPortDispatcherFlow(dpId, iface.getName(), low, t, low.getServicePriority());
101 BoundServices lower = FlowBasedServicesUtils.getHighAndLowPriorityService(boundServices, low)[0];
102 short lowerServiceIndex = (short) ((lower!=null) ? lower.getServicePriority() : low.getServicePriority() + 1);
103 LOG.trace("Installing new entry for lower service {}, match service index {}, update service index {}", low, IfmConstants.DEFAULT_SERVICE_INDEX, lowerServiceIndex);
104 FlowBasedServicesUtils.installLPortDispatcherFlow(dpId, low, iface, t, ifIndex, IfmConstants.DEFAULT_SERVICE_INDEX, lowerServiceIndex);
107 LOG.trace("Deleting table entry for service {}, match service index {}", boundServiceOld, boundServiceOld.getServicePriority());
108 FlowBasedServicesUtils.removeLPortDispatcherFlow(dpId, iface.getName(), boundServiceOld, t, boundServiceOld.getServicePriority());
109 short lowerServiceIndex = (short) ((low!=null) ? low.getServicePriority() : boundServiceOld.getServicePriority() + 1);
110 BoundServices highest = FlowBasedServicesUtils.getHighestPriorityService(boundServices);
111 if (high.equals(highest)) {
112 LOG.trace("Update the existing higher service {}, match service index {}, update service index {}", high, IfmConstants.DEFAULT_SERVICE_INDEX, lowerServiceIndex);
113 FlowBasedServicesUtils.installLPortDispatcherFlow(dpId, high, iface, t, ifIndex, IfmConstants.DEFAULT_SERVICE_INDEX, lowerServiceIndex);
115 LOG.trace("Update the existing higher service {}, match service index {}, update service index {}", high, high.getServicePriority(), lowerServiceIndex);
116 FlowBasedServicesUtils.installLPortDispatcherFlow(dpId, high, iface, t, ifIndex, high.getServicePriority(), lowerServiceIndex);
119 futures.add(t.submit());
123 private static List<ListenableFuture<Void>> unbindServiceOnTunnel(
124 BoundServices boundServiceOld,
125 List<BoundServices> boundServices, Interface iface, int ifIndex,
126 DataBroker dataBroker) {
127 List<ListenableFuture<Void>> futures = new ArrayList<>();
129 WriteTransaction t = dataBroker.newWriteOnlyTransaction();
130 NodeConnectorId nodeConnectorId = FlowBasedServicesUtils.getNodeConnectorIdFromInterface(iface, dataBroker);
131 long portNo = Long.parseLong(IfmUtil.getPortNoFromNodeConnectorId(nodeConnectorId));
132 BigInteger dpId = new BigInteger(IfmUtil.getDpnFromNodeConnectorId(nodeConnectorId));
134 if (boundServices.isEmpty()) {
135 // Remove entry from Ingress Table.
136 FlowBasedServicesUtils.removeIngressFlow(iface.getName(), boundServiceOld, dpId, t);
138 futures.add(t.submit());
143 Map<Short, BoundServices> tmpServicesMap = new ConcurrentHashMap<>();
144 short highestPriority = 0xFF;
145 for (BoundServices boundService : boundServices) {
146 tmpServicesMap.put(boundService.getServicePriority(), boundService);
147 if (boundService.getServicePriority() < highestPriority) {
148 highestPriority = boundService.getServicePriority();
152 if (highestPriority < boundServiceOld.getServicePriority()) {
153 FlowBasedServicesUtils.removeLPortDispatcherFlow(dpId, iface.getName(), boundServiceOld, t, boundServiceOld.getServicePriority());
155 futures.add(t.submit());
160 List<MatchInfo> matches = null;
161 matches = FlowBasedServicesUtils.getMatchInfoForTunnelPortAtIngressTable (dpId, portNo, iface);
163 BoundServices toBeMoved = tmpServicesMap.get(highestPriority);
164 FlowBasedServicesUtils.removeIngressFlow(iface.getName(), boundServiceOld, dpId, t);
165 FlowBasedServicesUtils.installInterfaceIngressFlow(dpId, iface, toBeMoved, t,
166 matches, ifIndex, NwConstants.VLAN_INTERFACE_INGRESS_TABLE);
167 FlowBasedServicesUtils.removeLPortDispatcherFlow(dpId, iface.getName(), toBeMoved, t, toBeMoved.getServicePriority());
170 futures.add(t.submit());