2 * Copyright (c) 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.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;
14 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
15 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
16 import org.opendaylight.genius.interfacemanager.InterfacemgrProvider;
17 import org.opendaylight.genius.interfacemanager.commons.InterfaceManagerCommonUtils;
18 import org.opendaylight.genius.interfacemanager.servicebindings.flowbased.config.factory.FlowBasedServicesConfigAddable;
19 import org.opendaylight.genius.interfacemanager.servicebindings.flowbased.utilities.FlowBasedServicesUtils;
20 import org.opendaylight.genius.mdsalutil.NwConstants;
21 import org.opendaylight.genius.utils.ServiceIndex;
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.Interface;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.ServiceModeBase;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.ServicesInfo;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.services.info.BoundServices;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 public class FlowBasedEgressServicesConfigBindHelper implements FlowBasedServicesConfigAddable {
34 private static final Logger LOG = LoggerFactory.getLogger(FlowBasedEgressServicesConfigBindHelper.class);
36 private InterfacemgrProvider interfaceMgrProvider;
37 private static volatile FlowBasedServicesConfigAddable flowBasedEgressServicesAddable;
39 private FlowBasedEgressServicesConfigBindHelper(InterfacemgrProvider interfaceMgrProvider) {
40 this.interfaceMgrProvider = interfaceMgrProvider;
43 public static void intitializeFlowBasedEgressServicesConfigAddHelper(InterfacemgrProvider interfaceMgrProvider) {
44 if (flowBasedEgressServicesAddable == null) {
45 synchronized (FlowBasedEgressServicesConfigBindHelper.class) {
46 if (flowBasedEgressServicesAddable == null) {
47 flowBasedEgressServicesAddable = new FlowBasedEgressServicesConfigBindHelper(interfaceMgrProvider);
53 public static FlowBasedServicesConfigAddable getFlowBasedEgressServicesAddHelper() {
54 if (flowBasedEgressServicesAddable == null) {
55 LOG.error("OvsInterfaceConfigAdd Renderer is not initialized");
57 return flowBasedEgressServicesAddable;
60 public List<ListenableFuture<Void>> bindService(InstanceIdentifier<BoundServices> instanceIdentifier,
61 BoundServices boundServiceNew) {
62 List<ListenableFuture<Void>> futures = new ArrayList<>();
63 DataBroker dataBroker = interfaceMgrProvider.getDataBroker();
64 String interfaceName =
65 InstanceIdentifier.keyOf(instanceIdentifier.firstIdentifierOf(ServicesInfo.class)).getInterfaceName();
66 Class<? extends ServiceModeBase> serviceMode = InstanceIdentifier.keyOf(instanceIdentifier.firstIdentifierOf(ServicesInfo.class)).getServiceMode();
68 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface ifState =
69 InterfaceManagerCommonUtils.getInterfaceStateFromOperDS(interfaceName, dataBroker);
70 if (ifState == null || ifState.getOperStatus() == OperStatus.Down) {
71 LOG.warn("Interface not up, not Binding Service for Interface: {}", interfaceName);
75 // Get the Parent ServiceInfo
76 ServicesInfo servicesInfo = FlowBasedServicesUtils.getServicesInfoForInterface(interfaceName, serviceMode, dataBroker);
77 if (servicesInfo == null) {
78 LOG.error("Reached Impossible part 1 in the code during bind service for: {}", boundServiceNew);
82 List<BoundServices> allServices = servicesInfo.getBoundServices();
83 if (allServices.isEmpty()) {
84 LOG.error("Reached Impossible part 2 in the code during bind service for: {}", boundServiceNew);
88 // Split based on type of interface....
89 if (ifState.getType().isAssignableFrom(L2vlan.class)) {
90 return bindServiceOnVlan(boundServiceNew, allServices, ifState, dataBroker);
91 } else if (ifState.getType().isAssignableFrom(Tunnel.class)) {
92 return bindServiceOnTunnel(boundServiceNew, allServices, ifState, dataBroker);
97 private static List<ListenableFuture<Void>> bindServiceOnTunnel(BoundServices boundServiceNew, List<BoundServices> allServices,
98 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface ifState, DataBroker dataBroker) {
99 // TODO - binding egress services on tunnels is not supported currently
103 private static List<ListenableFuture<Void>> bindServiceOnVlan(BoundServices boundServiceNew, List<BoundServices> allServices,
104 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface ifState, DataBroker dataBroker) {
105 List<ListenableFuture<Void>> futures = new ArrayList<>();
106 BigInteger dpId = FlowBasedServicesUtils.getDpnIdFromInterface(ifState);
107 WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
108 Interface iface = InterfaceManagerCommonUtils.getInterfaceFromConfigDS(ifState.getName(), dataBroker);
109 LOG.info("binding egress service for vlan port: {}", ifState.getName());
110 if (allServices.size() == 1) {
111 //calling LportDispatcherTableForService with current service index as 0 and next service index as
112 // some value since this is the only service bound.
113 FlowBasedServicesUtils.installEgressDispatcherFlows(dpId, boundServiceNew, ifState.getName(),
114 transaction, ifState.getIfIndex(), NwConstants.DEFAULT_SERVICE_INDEX, (short) (boundServiceNew.getServicePriority() + 1), iface);
115 if (transaction != null) {
116 futures.add(transaction.submit());
120 allServices.remove(boundServiceNew);
121 BoundServices[] highLowPriorityService = FlowBasedServicesUtils.getHighAndLowPriorityService(allServices, boundServiceNew);
122 BoundServices low = highLowPriorityService[0];
123 BoundServices high = highLowPriorityService[1];
124 BoundServices highest = FlowBasedServicesUtils.getHighestPriorityService(allServices);
125 short currentServiceIndex = NwConstants.DEFAULT_SERVICE_INDEX;
126 short nextServiceIndex = ServiceIndex.getIndex(NwConstants.DEFAULT_EGRESS_SERVICE_NAME, NwConstants.DEFAULT_EGRESS_SERVICE_INDEX); // dummy service index
128 nextServiceIndex = low.getServicePriority();
129 if (low.equals(highest)) {
130 //In this case the match criteria of existing service should be changed.
131 BoundServices lower = FlowBasedServicesUtils.getHighAndLowPriorityService(allServices, low)[0];
132 short lowerServiceIndex = (short) ((lower != null) ? lower.getServicePriority() : low.getServicePriority() + 1);
133 FlowBasedServicesUtils.installEgressDispatcherFlows(dpId, low, ifState.getName(), transaction, ifState.getIfIndex(),
134 low.getServicePriority(), lowerServiceIndex, iface);
136 currentServiceIndex = boundServiceNew.getServicePriority();
140 currentServiceIndex = boundServiceNew.getServicePriority();
141 if (high.equals(highest)) {
142 FlowBasedServicesUtils.installEgressDispatcherFlows(dpId, high, ifState.getName(), transaction, ifState.getIfIndex(), NwConstants.DEFAULT_SERVICE_INDEX, currentServiceIndex, iface);
144 FlowBasedServicesUtils.installEgressDispatcherFlows(dpId, high, ifState.getName(), transaction, ifState.getIfIndex(), high.getServicePriority(), currentServiceIndex, iface);
147 FlowBasedServicesUtils.installEgressDispatcherFlows(dpId, boundServiceNew, ifState.getName(), transaction, ifState.getIfIndex(), currentServiceIndex, nextServiceIndex, iface);
148 futures.add(transaction.submit());