2 * Copyright (c) 2016, 2017 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.genius.interfacemanager.servicebindings.flowbased.config.helpers;
10 import com.google.common.util.concurrent.ListenableFuture;
11 import java.math.BigInteger;
12 import java.util.ArrayList;
13 import java.util.List;
15 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.genius.interfacemanager.IfmUtil;
19 import org.opendaylight.genius.interfacemanager.InterfacemgrProvider;
20 import org.opendaylight.genius.interfacemanager.commons.InterfaceManagerCommonUtils;
21 import org.opendaylight.genius.interfacemanager.servicebindings.flowbased.config.factory.FlowBasedServicesConfigAddable;
22 import org.opendaylight.genius.interfacemanager.servicebindings.flowbased.utilities.FlowBasedServicesUtils;
23 import org.opendaylight.genius.mdsalutil.MatchInfo;
24 import org.opendaylight.genius.mdsalutil.NwConstants;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.L2vlan;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.Tunnel;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.ServiceModeBase;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.ServicesInfo;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.services.info.BoundServices;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 public class FlowBasedIngressServicesConfigBindHelper implements FlowBasedServicesConfigAddable {
37 private static final Logger LOG = LoggerFactory.getLogger(FlowBasedIngressServicesConfigBindHelper.class);
39 private final InterfacemgrProvider interfaceMgrProvider;
40 private static volatile FlowBasedServicesConfigAddable flowBasedIngressServicesAddable;
42 private FlowBasedIngressServicesConfigBindHelper(InterfacemgrProvider interfaceMgrProvider) {
43 this.interfaceMgrProvider = interfaceMgrProvider;
46 public static void intitializeFlowBasedIngressServicesConfigAddHelper(InterfacemgrProvider interfaceMgrProvider) {
47 if (flowBasedIngressServicesAddable == null) {
48 synchronized (FlowBasedIngressServicesConfigBindHelper.class) {
49 if (flowBasedIngressServicesAddable == null) {
50 flowBasedIngressServicesAddable = new FlowBasedIngressServicesConfigBindHelper(
51 interfaceMgrProvider);
57 public static void clearFlowBasedIngressServicesConfigAddHelper() {
58 flowBasedIngressServicesAddable = null;
61 public static FlowBasedServicesConfigAddable getFlowBasedIngressServicesAddHelper() {
62 if (flowBasedIngressServicesAddable == null) {
63 LOG.error("OvsInterfaceConfigAdd Renderer is not initialized");
65 return flowBasedIngressServicesAddable;
69 public List<ListenableFuture<Void>> bindService(InstanceIdentifier<BoundServices> instanceIdentifier,
70 BoundServices boundServiceNew) {
71 List<ListenableFuture<Void>> futures = new ArrayList<>();
72 DataBroker dataBroker = interfaceMgrProvider.getDataBroker();
73 String interfaceName = InstanceIdentifier.keyOf(instanceIdentifier.firstIdentifierOf(ServicesInfo.class))
75 Class<? extends ServiceModeBase> serviceMode = InstanceIdentifier
76 .keyOf(instanceIdentifier.firstIdentifierOf(ServicesInfo.class)).getServiceMode();
78 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
79 .ietf.interfaces.rev140508.interfaces.state.Interface ifState = InterfaceManagerCommonUtils
80 .getInterfaceStateFromOperDS(interfaceName, dataBroker);
81 if (ifState == null) {
82 LOG.warn("Interface not operational, not binding Service for Interface: {}", interfaceName);
86 // Get the Parent ServiceInfo
88 ServicesInfo servicesInfo = FlowBasedServicesUtils.getServicesInfoForInterface(interfaceName, serviceMode,
90 if (servicesInfo == null) {
91 LOG.error("Reached Impossible part 1 in the code during bind service for: {}", boundServiceNew);
95 List<BoundServices> allServices = servicesInfo.getBoundServices();
96 if (allServices.isEmpty()) {
97 LOG.error("Reached Impossible part 2 in the code during bind service for: {}", boundServiceNew);
100 // Split based on type of interface...
101 if (L2vlan.class.equals(ifState.getType())) {
102 return bindServiceOnVlan(boundServiceNew, allServices, ifState, dataBroker);
103 } else if (Tunnel.class.equals(ifState.getType())) {
104 return bindServiceOnTunnel(boundServiceNew, allServices, ifState, dataBroker);
109 private static List<ListenableFuture<Void>> bindServiceOnTunnel(BoundServices boundServiceNew,
110 List<BoundServices> allServices,
111 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
112 .ietf.interfaces.rev140508.interfaces.state.Interface ifState,
113 DataBroker dataBroker) {
114 List<ListenableFuture<Void>> futures = new ArrayList<>();
115 NodeConnectorId nodeConnectorId = FlowBasedServicesUtils.getNodeConnectorIdFromInterface(ifState);
116 long portNo = IfmUtil.getPortNumberFromNodeConnectorId(nodeConnectorId);
117 BigInteger dpId = IfmUtil.getDpnFromNodeConnectorId(nodeConnectorId);
118 WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
119 Interface iface = InterfaceManagerCommonUtils.getInterfaceFromConfigDS(ifState.getName(), dataBroker);
120 if (allServices.size() == 1) {
121 // If only one service present, install instructions in table 0.
122 List<MatchInfo> matches = null;
123 matches = FlowBasedServicesUtils.getMatchInfoForTunnelPortAtIngressTable(dpId, portNo);
124 FlowBasedServicesUtils.installInterfaceIngressFlow(dpId, iface, boundServiceNew, transaction, matches,
125 ifState.getIfIndex(), NwConstants.VLAN_INTERFACE_INGRESS_TABLE);
126 if (transaction != null) {
127 futures.add(transaction.submit());
132 boolean isCurrentServiceHighestPriority = true;
133 Map<Short, BoundServices> tmpServicesMap = new ConcurrentHashMap<>();
134 short highestPriority = 0xFF;
135 for (BoundServices boundService : allServices) {
136 if (boundService.getServicePriority() < boundServiceNew.getServicePriority()) {
137 isCurrentServiceHighestPriority = false;
140 if (!boundService.equals(boundServiceNew)) {
141 tmpServicesMap.put(boundService.getServicePriority(), boundService);
142 if (boundService.getServicePriority() < highestPriority) {
143 highestPriority = boundService.getServicePriority();
148 if (!isCurrentServiceHighestPriority) {
149 FlowBasedServicesUtils.installLPortDispatcherFlow(dpId, boundServiceNew, ifState.getName(), transaction,
150 ifState.getIfIndex(), boundServiceNew.getServicePriority(),
151 (short) (boundServiceNew.getServicePriority() + 1));
153 BoundServices serviceToReplace = tmpServicesMap.get(highestPriority);
154 FlowBasedServicesUtils.installLPortDispatcherFlow(dpId, serviceToReplace, ifState.getName(), transaction,
155 ifState.getIfIndex(), serviceToReplace.getServicePriority(),
156 (short) (serviceToReplace.getServicePriority() + 1));
157 List<MatchInfo> matches = FlowBasedServicesUtils.getMatchInfoForTunnelPortAtIngressTable(dpId, portNo);
159 if (matches != null) {
160 WriteTransaction removeFlowTransaction = dataBroker.newWriteOnlyTransaction();
161 FlowBasedServicesUtils.removeIngressFlow(iface.getName(), serviceToReplace, dpId,
162 removeFlowTransaction);
163 futures.add(removeFlowTransaction.submit());
165 WriteTransaction installFlowTransaction = dataBroker.newWriteOnlyTransaction();
166 FlowBasedServicesUtils.installInterfaceIngressFlow(dpId, iface, boundServiceNew, installFlowTransaction,
167 matches, ifState.getIfIndex(), NwConstants.VLAN_INTERFACE_INGRESS_TABLE);
168 futures.add(installFlowTransaction.submit());
172 if (transaction != null) {
173 futures.add(transaction.submit());
178 private static List<ListenableFuture<Void>> bindServiceOnVlan(BoundServices boundServiceNew,
179 List<BoundServices> allServices,
180 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
181 .ietf.interfaces.rev140508.interfaces.state.Interface ifState,
182 DataBroker dataBroker) {
183 List<ListenableFuture<Void>> futures = new ArrayList<>();
184 BigInteger dpId = FlowBasedServicesUtils.getDpnIdFromInterface(ifState);
185 WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
186 LOG.info("binding ingress service {} for vlan port: {}", boundServiceNew.getServiceName(), ifState.getName());
187 if (allServices.size() == 1) {
188 // calling LportDispatcherTableForService with current service index
189 // as 0 and next service index as some value since this is the only
191 FlowBasedServicesUtils.installLPortDispatcherFlow(dpId, boundServiceNew, ifState.getName(), transaction,
192 ifState.getIfIndex(), NwConstants.DEFAULT_SERVICE_INDEX,
193 (short) (boundServiceNew.getServicePriority() + 1));
194 if (transaction != null) {
195 futures.add(transaction.submit());
199 allServices.remove(boundServiceNew);
200 BoundServices[] highLowPriorityService = FlowBasedServicesUtils.getHighAndLowPriorityService(allServices,
202 BoundServices low = highLowPriorityService[0];
203 BoundServices high = highLowPriorityService[1];
204 BoundServices highest = FlowBasedServicesUtils.getHighestPriorityService(allServices);
205 short currentServiceIndex = NwConstants.DEFAULT_SERVICE_INDEX;
206 short nextServiceIndex = (short) (boundServiceNew.getServicePriority() + 1); // dummy
210 nextServiceIndex = low.getServicePriority();
211 if (low.equals(highest)) {
212 // In this case the match criteria of existing service should be
214 BoundServices lower = FlowBasedServicesUtils.getHighAndLowPriorityService(allServices, low)[0];
215 short lowerServiceIndex = (short) (lower != null ? lower.getServicePriority()
216 : low.getServicePriority() + 1);
218 "Installing ingress dispatcher table entry for existing service {} service match on "
219 + "service index {} update with service index {}",
220 low, low.getServicePriority(), lowerServiceIndex);
221 FlowBasedServicesUtils.installLPortDispatcherFlow(dpId, low, ifState.getName(), transaction,
222 ifState.getIfIndex(), low.getServicePriority(), lowerServiceIndex);
224 currentServiceIndex = boundServiceNew.getServicePriority();
228 currentServiceIndex = boundServiceNew.getServicePriority();
229 if (high.equals(highest)) {
231 "Installing ingress dispatcher table entry for existing service {} service match on "
232 + "service index {} update with service index {}",
233 high, NwConstants.DEFAULT_SERVICE_INDEX, currentServiceIndex);
234 FlowBasedServicesUtils.installLPortDispatcherFlow(dpId, high, ifState.getName(), transaction,
235 ifState.getIfIndex(), NwConstants.DEFAULT_SERVICE_INDEX, currentServiceIndex);
238 "Installing ingress dispatcher table entry for existing service {} service match on "
239 + "service index {} update with service index {}",
240 high, high.getServicePriority(), currentServiceIndex);
241 FlowBasedServicesUtils.installLPortDispatcherFlow(dpId, high, ifState.getName(), transaction,
242 ifState.getIfIndex(), high.getServicePriority(), currentServiceIndex);
245 LOG.trace("Installing ingress dispatcher table entry for new service match on service index {} update with "
246 + "service index {}", currentServiceIndex, nextServiceIndex);
247 FlowBasedServicesUtils.installLPortDispatcherFlow(dpId, boundServiceNew, ifState.getName(), transaction,
248 ifState.getIfIndex(), currentServiceIndex, nextServiceIndex);
249 futures.add(transaction.submit());