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.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 {} for Interface: {}",
60 ifState.getOperStatus(), interfaceName);
63 List<BoundServices> boundServices = servicesInfo.getBoundServices();
65 // Split based on type of interface....
66 if (iface.getType().isAssignableFrom(L2vlan.class)) {
67 return unbindServiceOnVlan(boundServiceOld, boundServices, iface, ifState.getIfIndex(), dataBroker);
68 } else if (iface.getType().isAssignableFrom(Tunnel.class)) {
69 return unbindServiceOnTunnel(boundServiceOld, boundServices, iface, ifState.getIfIndex(), dataBroker);
74 private static List<ListenableFuture<Void>> unbindServiceOnVlan(
75 BoundServices boundServiceOld,
76 List<BoundServices> boundServices, Interface iface, int ifIndex,
77 DataBroker dataBroker) {
79 List<ListenableFuture<Void>> futures = new ArrayList<>();
80 WriteTransaction t = dataBroker.newWriteOnlyTransaction();
81 NodeConnectorId nodeConnectorId = FlowBasedServicesUtils.getNodeConnectorIdFromInterface(iface, dataBroker);
82 BigInteger dpId = new BigInteger(IfmUtil.getDpnFromNodeConnectorId(nodeConnectorId));
83 if (boundServices.isEmpty()) {
84 // Remove default entry from Lport Dispatcher Table.
85 FlowBasedServicesUtils.removeLPortDispatcherFlow(dpId, iface, boundServiceOld, t, IfmConstants.DEFAULT_SERVICE_INDEX);
87 futures.add(t.submit());
91 BoundServices[] highLow = FlowBasedServicesUtils.getHighAndLowPriorityService(boundServices, boundServiceOld);
92 BoundServices low = highLow[0];
93 BoundServices high = highLow[1];
94 // This means the one removed was the highest priority service
96 LOG.trace("Deleting table entry for service {}, match service index {}", boundServiceOld, IfmConstants.DEFAULT_SERVICE_INDEX);
97 FlowBasedServicesUtils.removeLPortDispatcherFlow(dpId, iface, boundServiceOld, t, IfmConstants.DEFAULT_SERVICE_INDEX);
99 //delete the lower services flow entry.
100 LOG.trace("Deleting table entry for lower service {}, match service index {}", low, low.getServicePriority());
101 FlowBasedServicesUtils.removeLPortDispatcherFlow(dpId, iface, low, t, low.getServicePriority());
102 BoundServices lower = FlowBasedServicesUtils.getHighAndLowPriorityService(boundServices, low)[0];
103 short lowerServiceIndex = (short) ((lower!=null) ? lower.getServicePriority() : low.getServicePriority() + 1);
104 LOG.trace("Installing new entry for lower service {}, match service index {}, update service index {}", low, IfmConstants.DEFAULT_SERVICE_INDEX, lowerServiceIndex);
105 FlowBasedServicesUtils.installLPortDispatcherFlow(dpId, low, iface, t, ifIndex, IfmConstants.DEFAULT_SERVICE_INDEX, lowerServiceIndex);
108 LOG.trace("Deleting table entry for service {}, match service index {}", boundServiceOld, boundServiceOld.getServicePriority());
109 FlowBasedServicesUtils.removeLPortDispatcherFlow(dpId, iface, boundServiceOld, t, boundServiceOld.getServicePriority());
110 short lowerServiceIndex = (short) ((low!=null) ? low.getServicePriority() : boundServiceOld.getServicePriority() + 1);
111 BoundServices highest = FlowBasedServicesUtils.getHighestPriorityService(boundServices);
112 if (high.equals(highest)) {
113 LOG.trace("Update the existing higher service {}, match service index {}, update service index {}", high, IfmConstants.DEFAULT_SERVICE_INDEX, lowerServiceIndex);
114 FlowBasedServicesUtils.installLPortDispatcherFlow(dpId, high, iface, t, ifIndex, IfmConstants.DEFAULT_SERVICE_INDEX, lowerServiceIndex);
116 LOG.trace("Update the existing higher service {}, match service index {}, update service index {}", high, high.getServicePriority(), lowerServiceIndex);
117 FlowBasedServicesUtils.installLPortDispatcherFlow(dpId, high, iface, t, ifIndex, high.getServicePriority(), lowerServiceIndex);
120 futures.add(t.submit());
124 private static List<ListenableFuture<Void>> unbindServiceOnTunnel(
125 BoundServices boundServiceOld,
126 List<BoundServices> boundServices, Interface iface, int ifIndex,
127 DataBroker dataBroker) {
128 List<ListenableFuture<Void>> futures = new ArrayList<>();
130 WriteTransaction t = dataBroker.newWriteOnlyTransaction();
131 NodeConnectorId nodeConnectorId = FlowBasedServicesUtils.getNodeConnectorIdFromInterface(iface, dataBroker);
132 long portNo = Long.parseLong(IfmUtil.getPortNoFromNodeConnectorId(nodeConnectorId));
133 BigInteger dpId = new BigInteger(IfmUtil.getDpnFromNodeConnectorId(nodeConnectorId));
135 if (boundServices.isEmpty()) {
136 // Remove entry from Ingress Table.
137 FlowBasedServicesUtils.removeIngressFlow(iface, boundServiceOld, dpId, t);
139 futures.add(t.submit());
144 Map<Short, BoundServices> tmpServicesMap = new ConcurrentHashMap<>();
145 short highestPriority = 0xFF;
146 for (BoundServices boundService : boundServices) {
147 tmpServicesMap.put(boundService.getServicePriority(), boundService);
148 if (boundService.getServicePriority() < highestPriority) {
149 highestPriority = boundService.getServicePriority();
153 if (highestPriority < boundServiceOld.getServicePriority()) {
154 FlowBasedServicesUtils.removeLPortDispatcherFlow(dpId, iface, boundServiceOld, t, boundServiceOld.getServicePriority());
156 futures.add(t.submit());
161 List<MatchInfo> matches = null;
162 matches = FlowBasedServicesUtils.getMatchInfoForTunnelPortAtIngressTable (dpId, portNo, iface);
164 BoundServices toBeMoved = tmpServicesMap.get(highestPriority);
165 FlowBasedServicesUtils.removeIngressFlow(iface, boundServiceOld, dpId, t);
166 FlowBasedServicesUtils.installInterfaceIngressFlow(dpId, iface, toBeMoved, t,
167 matches, ifIndex, NwConstants.VLAN_INTERFACE_INGRESS_TABLE);
168 FlowBasedServicesUtils.removeLPortDispatcherFlow(dpId, iface, toBeMoved, t, toBeMoved.getServicePriority());
171 futures.add(t.submit());