Merge "Add GPE option to ITM config"
[genius.git] / interfacemanager / interfacemanager-impl / src / main / java / org / opendaylight / genius / interfacemanager / servicebindings / flowbased / state / helpers / FlowBasedEgressServicesStateBindHelper.java
1 /*
2  * Copyright (c) 2016, 2017 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.genius.interfacemanager.servicebindings.flowbased.state.helpers;
9
10 import com.google.common.util.concurrent.ListenableFuture;
11 import java.math.BigInteger;
12 import java.util.Collections;
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.IfmUtil;
17 import org.opendaylight.genius.interfacemanager.InterfacemgrProvider;
18 import org.opendaylight.genius.interfacemanager.commons.InterfaceManagerCommonUtils;
19 import org.opendaylight.genius.interfacemanager.servicebindings.flowbased.state.factory.FlowBasedServicesStateAddable;
20 import org.opendaylight.genius.interfacemanager.servicebindings.flowbased.utilities.FlowBasedServicesUtils;
21 import org.opendaylight.genius.mdsalutil.NwConstants;
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.state.Interface;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.services.info.BoundServices;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 public class FlowBasedEgressServicesStateBindHelper implements FlowBasedServicesStateAddable {
31     private static final Logger LOG = LoggerFactory.getLogger(FlowBasedEgressServicesStateBindHelper.class);
32
33     private final InterfacemgrProvider interfaceMgrProvider;
34     private static volatile FlowBasedServicesStateAddable flowBasedServicesStateAddable;
35
36     private FlowBasedEgressServicesStateBindHelper(InterfacemgrProvider interfaceMgrProvider) {
37         this.interfaceMgrProvider = interfaceMgrProvider;
38     }
39
40     public static void intitializeFlowBasedEgressServicesStateBindHelper(InterfacemgrProvider interfaceMgrProvider) {
41         if (flowBasedServicesStateAddable == null) {
42             synchronized (FlowBasedEgressServicesStateBindHelper.class) {
43                 if (flowBasedServicesStateAddable == null) {
44                     flowBasedServicesStateAddable = new FlowBasedEgressServicesStateBindHelper(interfaceMgrProvider);
45                 }
46             }
47         }
48     }
49
50     public static FlowBasedServicesStateAddable getFlowBasedEgressServicesStateBindHelper() {
51         if (flowBasedServicesStateAddable == null) {
52             LOG.error("OvsInterfaceConfigAdd Renderer is not initialized");
53         }
54         return flowBasedServicesStateAddable;
55     }
56
57     @Override
58     public void bindServicesOnInterface(List<ListenableFuture<Void>> futures,
59                                                                 Interface ifaceState, List<BoundServices> allServices) {
60         LOG.debug("binding services on interface {}", ifaceState.getName());
61         if (L2vlan.class.equals(ifaceState.getType()) || Tunnel.class.equals(ifaceState.getType())) {
62             bindServices(futures, allServices, ifaceState, interfaceMgrProvider.getDataBroker());
63         }
64     }
65
66     private static void bindServices(List<ListenableFuture<Void>> futures,
67                                                              List<BoundServices> allServices, Interface ifState,
68                                                              DataBroker dataBroker) {
69         LOG.info("bind all egress services for interface: {}", ifState.getName());
70
71         NodeConnectorId nodeConnectorId = FlowBasedServicesUtils.getNodeConnectorIdFromInterface(ifState);
72         BigInteger dpId = IfmUtil.getDpnFromNodeConnectorId(nodeConnectorId);
73         WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
74         Collections.sort(allServices, (serviceInfo1, serviceInfo2) -> serviceInfo1.getServicePriority()
75                 .compareTo(serviceInfo2.getServicePriority()));
76         BoundServices highestPriority = allServices.remove(0);
77         short nextServiceIndex = (short) (allServices.size() > 0 ? allServices.get(0).getServicePriority()
78                 : highestPriority.getServicePriority() + 1);
79         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
80             .ietf.interfaces.rev140508.interfaces.Interface iface = InterfaceManagerCommonUtils
81                 .getInterfaceFromConfigDS(ifState.getName(), dataBroker);
82         FlowBasedServicesUtils.installEgressDispatcherFlows(dpId, highestPriority, ifState.getName(), writeTransaction,
83                 ifState.getIfIndex(), NwConstants.DEFAULT_SERVICE_INDEX, nextServiceIndex, iface);
84         BoundServices prev = null;
85         for (BoundServices boundService : allServices) {
86             if (prev != null) {
87                 FlowBasedServicesUtils.installEgressDispatcherFlows(dpId, prev, ifState.getName(), writeTransaction,
88                         ifState.getIfIndex(), prev.getServicePriority(), boundService.getServicePriority(), iface);
89             }
90             prev = boundService;
91         }
92         if (prev != null) {
93             FlowBasedServicesUtils.installEgressDispatcherFlows(dpId, prev, ifState.getName(), writeTransaction,
94                     ifState.getIfIndex(),
95                     prev.getServicePriority(), (short) (prev.getServicePriority() + 1), iface);
96         }
97         futures.add(writeTransaction.submit());
98     }
99 }