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.state.helpers;
10 import com.google.common.util.concurrent.ListenableFuture;
11 import java.math.BigInteger;
12 import java.util.ArrayList;
13 import java.util.Collections;
14 import java.util.List;
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
17 import org.opendaylight.genius.interfacemanager.IfmUtil;
18 import org.opendaylight.genius.interfacemanager.InterfacemgrProvider;
19 import org.opendaylight.genius.interfacemanager.commons.InterfaceManagerCommonUtils;
20 import org.opendaylight.genius.interfacemanager.servicebindings.flowbased.state.factory.FlowBasedServicesStateAddable;
21 import org.opendaylight.genius.interfacemanager.servicebindings.flowbased.utilities.FlowBasedServicesUtils;
22 import org.opendaylight.genius.mdsalutil.NwConstants;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.L2vlan;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.Tunnel;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.ServiceModeEgress;
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.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 public class FlowBasedEgressServicesStateBindHelper implements FlowBasedServicesStateAddable{
34 private static final Logger LOG = LoggerFactory.getLogger(FlowBasedEgressServicesStateBindHelper.class);
36 private InterfacemgrProvider interfaceMgrProvider;
37 private static volatile FlowBasedServicesStateAddable flowBasedServicesStateAddable;
39 private FlowBasedEgressServicesStateBindHelper(InterfacemgrProvider interfaceMgrProvider) {
40 this.interfaceMgrProvider = interfaceMgrProvider;
43 public static void intitializeFlowBasedEgressServicesStateBindHelper(InterfacemgrProvider interfaceMgrProvider) {
44 if (flowBasedServicesStateAddable == null) {
45 synchronized (FlowBasedEgressServicesStateBindHelper.class) {
46 if (flowBasedServicesStateAddable == null) {
47 flowBasedServicesStateAddable = new FlowBasedEgressServicesStateBindHelper(interfaceMgrProvider);
53 public static FlowBasedServicesStateAddable getFlowBasedEgressServicesStateBindHelper() {
54 if (flowBasedServicesStateAddable == null) {
55 LOG.error("OvsInterfaceConfigAdd Renderer is not initialized");
57 return flowBasedServicesStateAddable;
60 public List<ListenableFuture<Void>> bindServicesOnInterface(Interface ifaceState) {
61 List<ListenableFuture<Void>> futures = new ArrayList<>();
62 LOG.debug("binding services on interface {}", ifaceState.getName());
63 DataBroker dataBroker = interfaceMgrProvider.getDataBroker();
64 ServicesInfo servicesInfo = FlowBasedServicesUtils.getServicesInfoForInterface(ifaceState.getName(), ServiceModeEgress.class, dataBroker);
65 if (servicesInfo == null) {
66 LOG.trace("service info is null for interface {}", ifaceState.getName());
70 List<BoundServices> allServices = servicesInfo.getBoundServices();
71 if (allServices == null || allServices.isEmpty()) {
72 LOG.trace("bound services is empty for interface {}", ifaceState.getName());
76 if (L2vlan.class.equals(ifaceState.getType())
77 || Tunnel.class.equals(ifaceState.getType())) {
78 return bindServices(allServices, ifaceState, dataBroker);
83 private static List<ListenableFuture<Void>> bindServices(
84 List<BoundServices> allServices,
85 Interface ifState, DataBroker dataBroker) {
86 LOG.info("bind all egress services for interface: {}", ifState.getName());
87 List<ListenableFuture<Void>> futures = new ArrayList<>();
88 NodeConnectorId nodeConnectorId = FlowBasedServicesUtils.getNodeConnectorIdFromInterface(ifState);
89 BigInteger dpId = IfmUtil.getDpnFromNodeConnectorId(nodeConnectorId);
90 WriteTransaction t = dataBroker.newWriteOnlyTransaction();
91 Collections.sort(allServices,
92 (serviceInfo1, serviceInfo2) -> serviceInfo1.getServicePriority().compareTo(serviceInfo2.getServicePriority()));
93 BoundServices highestPriority = allServices.remove(0);
94 short nextServiceIndex = (short) (allServices.size() > 0 ? allServices.get(0).getServicePriority() : highestPriority.getServicePriority() + 1);
95 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface iface = InterfaceManagerCommonUtils.getInterfaceFromConfigDS(ifState.getName(), dataBroker);
96 FlowBasedServicesUtils.installEgressDispatcherFlows(dpId, highestPriority, ifState.getName(), t, ifState.getIfIndex(), NwConstants.DEFAULT_SERVICE_INDEX, nextServiceIndex, iface);
97 BoundServices prev = null;
98 for (BoundServices boundService : allServices) {
100 FlowBasedServicesUtils.installEgressDispatcherFlows(dpId, prev, ifState.getName(), t, ifState.getIfIndex(), prev.getServicePriority(), boundService.getServicePriority(), iface);
105 FlowBasedServicesUtils.installEgressDispatcherFlows(dpId, prev, ifState.getName(), t, ifState.getIfIndex(), prev.getServicePriority(), (short) (prev.getServicePriority()+1), iface);
107 futures.add(t.submit());