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 org.opendaylight.controller.md.sal.binding.api.DataBroker;
12 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
13 import org.opendaylight.genius.interfacemanager.IfmConstants;
14 import org.opendaylight.genius.interfacemanager.IfmUtil;
15 import org.opendaylight.genius.interfacemanager.InterfacemgrProvider;
16 import org.opendaylight.genius.interfacemanager.commons.InterfaceManagerCommonUtils;
17 import org.opendaylight.genius.interfacemanager.servicebindings.flowbased.state.factory.FlowBasedServicesStateAddable;
18 import org.opendaylight.genius.interfacemanager.servicebindings.flowbased.utilities.FlowBasedServicesUtils;
19 import org.opendaylight.genius.mdsalutil.MatchInfo;
20 import org.opendaylight.genius.mdsalutil.NwConstants;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.L2vlan;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.Tunnel;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.ServiceModeBase;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.ServiceModeEgress;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.ServicesInfo;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.services.info.BoundServices;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 import java.math.BigInteger;
33 import java.util.ArrayList;
34 import java.util.Collections;
35 import java.util.Comparator;
36 import java.util.List;
38 public class FlowBasedEgressServicesStateBindHelper implements FlowBasedServicesStateAddable{
39 private static final Logger LOG = LoggerFactory.getLogger(FlowBasedEgressServicesStateBindHelper.class);
41 private InterfacemgrProvider interfaceMgrProvider;
42 private static volatile FlowBasedServicesStateAddable flowBasedServicesStateAddable;
44 private FlowBasedEgressServicesStateBindHelper(InterfacemgrProvider interfaceMgrProvider) {
45 this.interfaceMgrProvider = interfaceMgrProvider;
48 public static void intitializeFlowBasedEgressServicesStateBindHelper(InterfacemgrProvider interfaceMgrProvider) {
49 if (flowBasedServicesStateAddable == null) {
50 synchronized (FlowBasedEgressServicesStateBindHelper.class) {
51 if (flowBasedServicesStateAddable == null) {
52 flowBasedServicesStateAddable = new FlowBasedEgressServicesStateBindHelper(interfaceMgrProvider);
58 public static FlowBasedServicesStateAddable getFlowBasedEgressServicesStateBindHelper() {
59 if (flowBasedServicesStateAddable == null) {
60 LOG.error("OvsInterfaceConfigAdd Renderer is not initialized");
62 return flowBasedServicesStateAddable;
65 public List<ListenableFuture<Void>> bindServicesOnInterface(Interface ifaceState) {
66 List<ListenableFuture<Void>> futures = new ArrayList<>();
67 if(ifaceState.getType() == null) {
70 LOG.debug("binding services on interface {}", ifaceState.getName());
71 DataBroker dataBroker = interfaceMgrProvider.getDataBroker();
72 ServicesInfo servicesInfo = FlowBasedServicesUtils.getServicesInfoForInterface(ifaceState.getName(), ServiceModeEgress.class, dataBroker);
73 if (servicesInfo == null) {
74 LOG.trace("service info is null for interface {}", ifaceState.getName());
78 List<BoundServices> allServices = servicesInfo.getBoundServices();
79 if (allServices == null || allServices.isEmpty()) {
80 LOG.trace("bound services is empty for interface {}", ifaceState.getName());
84 if (ifaceState.getType().isAssignableFrom(L2vlan.class)) {
85 return bindServiceOnVlan(allServices, ifaceState, dataBroker);
86 } else if (ifaceState.getType().isAssignableFrom(Tunnel.class)){
87 return bindServiceOnTunnel(allServices, ifaceState, dataBroker);
92 private static List<ListenableFuture<Void>> bindServiceOnTunnel(
93 List<BoundServices> allServices, Interface ifState, DataBroker dataBroker) {
94 List<ListenableFuture<Void>> futures = new ArrayList<>();
95 // FIXME : not supported yet
99 private static List<ListenableFuture<Void>> bindServiceOnVlan(
100 List<BoundServices> allServices,
101 Interface ifState, DataBroker dataBroker) {
102 List<ListenableFuture<Void>> futures = new ArrayList<>();
103 NodeConnectorId nodeConnectorId = FlowBasedServicesUtils.getNodeConnectorIdFromInterface(ifState);
104 BigInteger dpId = IfmUtil.getDpnFromNodeConnectorId(nodeConnectorId);
105 WriteTransaction t = dataBroker.newWriteOnlyTransaction();
106 Collections.sort(allServices, new Comparator<BoundServices>() {
108 public int compare(BoundServices serviceInfo1, BoundServices serviceInfo2) {
109 return serviceInfo1.getServicePriority().compareTo(serviceInfo2.getServicePriority());
112 BoundServices highestPriority = allServices.remove(0);
113 short nextServiceIndex = (short) (allServices.size() > 0 ? allServices.get(0).getServicePriority() : highestPriority.getServicePriority() + 1);
114 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface iface = InterfaceManagerCommonUtils.getInterfaceFromConfigDS(ifState.getName(), dataBroker);
115 FlowBasedServicesUtils.installEgressDispatcherFlows(dpId, highestPriority, ifState.getName(), t, ifState.getIfIndex(), NwConstants.DEFAULT_SERVICE_INDEX, nextServiceIndex, iface);
116 BoundServices prev = null;
117 for (BoundServices boundService : allServices) {
119 FlowBasedServicesUtils.installEgressDispatcherFlows(dpId, prev, ifState.getName(), t, ifState.getIfIndex(), prev.getServicePriority(), boundService.getServicePriority(), iface);
124 FlowBasedServicesUtils.installEgressDispatcherFlows(dpId, prev, ifState.getName(), t, ifState.getIfIndex(), prev.getServicePriority(), (short) (prev.getServicePriority()+1), iface);
126 futures.add(t.submit());