Bug7451 - Maintaining state information for bound-services in DS
[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.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.service.bindings.services.info.BoundServices;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 public class FlowBasedEgressServicesStateBindHelper implements FlowBasedServicesStateAddable {
32     private static final Logger LOG = LoggerFactory.getLogger(FlowBasedEgressServicesStateBindHelper.class);
33
34     private final InterfacemgrProvider interfaceMgrProvider;
35     private static volatile FlowBasedServicesStateAddable flowBasedServicesStateAddable;
36
37     private FlowBasedEgressServicesStateBindHelper(InterfacemgrProvider interfaceMgrProvider) {
38         this.interfaceMgrProvider = interfaceMgrProvider;
39     }
40
41     public static void intitializeFlowBasedEgressServicesStateBindHelper(InterfacemgrProvider interfaceMgrProvider) {
42         if (flowBasedServicesStateAddable == null) {
43             synchronized (FlowBasedEgressServicesStateBindHelper.class) {
44                 if (flowBasedServicesStateAddable == null) {
45                     flowBasedServicesStateAddable = new FlowBasedEgressServicesStateBindHelper(interfaceMgrProvider);
46                 }
47             }
48         }
49     }
50
51     public static FlowBasedServicesStateAddable getFlowBasedEgressServicesStateBindHelper() {
52         if (flowBasedServicesStateAddable == null) {
53             LOG.error("OvsInterfaceConfigAdd Renderer is not initialized");
54         }
55         return flowBasedServicesStateAddable;
56     }
57
58     @Override
59     public List<ListenableFuture<Void>> bindServicesOnInterface(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             return bindServices(allServices, ifaceState, interfaceMgrProvider.getDataBroker());
63         }
64         return null;
65     }
66
67     private static List<ListenableFuture<Void>> bindServices(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         List<ListenableFuture<Void>> futures = new ArrayList<>();
98         futures.add(writeTransaction.submit());
99         return futures;
100     }
101 }