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.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 final 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;
61 public List<ListenableFuture<Void>> bindServicesOnInterface(Interface ifaceState) {
62 List<ListenableFuture<Void>> futures = new ArrayList<>();
63 LOG.debug("binding services on interface {}", ifaceState.getName());
64 DataBroker dataBroker = interfaceMgrProvider.getDataBroker();
65 ServicesInfo servicesInfo = FlowBasedServicesUtils.getServicesInfoForInterface(ifaceState.getName(),
66 ServiceModeEgress.class, dataBroker);
67 if (servicesInfo == null) {
68 LOG.trace("service info is null for interface {}", ifaceState.getName());
72 List<BoundServices> allServices = servicesInfo.getBoundServices();
73 if (allServices == null || allServices.isEmpty()) {
74 LOG.trace("bound services is empty for interface {}", ifaceState.getName());
78 if (L2vlan.class.equals(ifaceState.getType()) || Tunnel.class.equals(ifaceState.getType())) {
79 return bindServices(allServices, ifaceState, dataBroker);
84 private static List<ListenableFuture<Void>> bindServices(List<BoundServices> allServices, Interface ifState,
85 DataBroker dataBroker) {
86 LOG.info("bind all egress services for interface: {}", ifState.getName());
88 NodeConnectorId nodeConnectorId = FlowBasedServicesUtils.getNodeConnectorIdFromInterface(ifState);
89 BigInteger dpId = IfmUtil.getDpnFromNodeConnectorId(nodeConnectorId);
90 WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
91 Collections.sort(allServices, (serviceInfo1, serviceInfo2) -> serviceInfo1.getServicePriority()
92 .compareTo(serviceInfo2.getServicePriority()));
93 BoundServices highestPriority = allServices.remove(0);
94 short nextServiceIndex = (short) (allServices.size() > 0 ? allServices.get(0).getServicePriority()
95 : highestPriority.getServicePriority() + 1);
96 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
97 .ietf.interfaces.rev140508.interfaces.Interface iface = InterfaceManagerCommonUtils
98 .getInterfaceFromConfigDS(ifState.getName(), dataBroker);
99 FlowBasedServicesUtils.installEgressDispatcherFlows(dpId, highestPriority, ifState.getName(), writeTransaction,
100 ifState.getIfIndex(), NwConstants.DEFAULT_SERVICE_INDEX, nextServiceIndex, iface);
101 BoundServices prev = null;
102 for (BoundServices boundService : allServices) {
104 FlowBasedServicesUtils.installEgressDispatcherFlows(dpId, prev, ifState.getName(), writeTransaction,
105 ifState.getIfIndex(), prev.getServicePriority(), boundService.getServicePriority(), iface);
110 FlowBasedServicesUtils.installEgressDispatcherFlows(dpId, prev, ifState.getName(), writeTransaction,
111 ifState.getIfIndex(),
112 prev.getServicePriority(), (short) (prev.getServicePriority() + 1), iface);
114 List<ListenableFuture<Void>> futures = new ArrayList<>();
115 futures.add(writeTransaction.submit());