Merge "Adding srcMac to odl-arputil since ARP src mac is currently set to the OF...
[genius.git] / interfacemanager / interfacemanager-impl / src / main / java / org / opendaylight / genius / interfacemanager / servicebindings / flowbased / config / helpers / FlowBasedEgressServicesConfigBindHelper.java
1 /*
2  * Copyright (c) 2016 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.config.helpers;
9
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.InterfacemgrProvider;
14 import org.opendaylight.genius.interfacemanager.commons.InterfaceManagerCommonUtils;
15 import org.opendaylight.genius.interfacemanager.servicebindings.flowbased.config.factory.FlowBasedServicesConfigAddable;
16 import org.opendaylight.genius.interfacemanager.servicebindings.flowbased.utilities.FlowBasedServicesUtils;
17 import org.opendaylight.genius.mdsalutil.NwConstants;
18 import org.opendaylight.genius.utils.ServiceIndex;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.L2vlan;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.Tunnel;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.ServiceModeBase;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.ServicesInfo;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.services.info.BoundServices;
25 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 import java.math.BigInteger;
30 import java.util.ArrayList;
31 import java.util.List;
32
33 public class FlowBasedEgressServicesConfigBindHelper implements FlowBasedServicesConfigAddable {
34     private static final Logger LOG = LoggerFactory.getLogger(FlowBasedEgressServicesConfigBindHelper.class);
35
36     private InterfacemgrProvider interfaceMgrProvider;
37     private static volatile FlowBasedServicesConfigAddable flowBasedEgressServicesAddable;
38
39     private FlowBasedEgressServicesConfigBindHelper(InterfacemgrProvider interfaceMgrProvider) {
40         this.interfaceMgrProvider = interfaceMgrProvider;
41     }
42
43     public static void intitializeFlowBasedEgressServicesConfigAddHelper(InterfacemgrProvider interfaceMgrProvider) {
44         if (flowBasedEgressServicesAddable == null) {
45             synchronized (FlowBasedEgressServicesConfigBindHelper.class) {
46                 if (flowBasedEgressServicesAddable == null) {
47                     flowBasedEgressServicesAddable = new FlowBasedEgressServicesConfigBindHelper(interfaceMgrProvider);
48                 }
49             }
50         }
51     }
52
53     public static FlowBasedServicesConfigAddable getFlowBasedEgressServicesAddHelper() {
54         if (flowBasedEgressServicesAddable == null) {
55             LOG.error("OvsInterfaceConfigAdd Renderer is not initialized");
56         }
57         return flowBasedEgressServicesAddable;
58     }
59
60     public List<ListenableFuture<Void>> bindService(InstanceIdentifier<BoundServices> instanceIdentifier,
61                                                     BoundServices boundServiceNew) {
62         List<ListenableFuture<Void>> futures = new ArrayList<>();
63         DataBroker dataBroker = interfaceMgrProvider.getDataBroker();
64         String interfaceName =
65                 InstanceIdentifier.keyOf(instanceIdentifier.firstIdentifierOf(ServicesInfo.class)).getInterfaceName();
66         Class<? extends ServiceModeBase> serviceMode = InstanceIdentifier.keyOf(instanceIdentifier.firstIdentifierOf(ServicesInfo.class)).getServiceMode();
67
68         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface ifState =
69                 InterfaceManagerCommonUtils.getInterfaceStateFromOperDS(interfaceName, dataBroker);
70         if (ifState == null || ifState.getOperStatus() == OperStatus.Down) {
71             LOG.warn("Interface not up, not Binding Service for Interface: {}", interfaceName);
72             return futures;
73         }
74
75         // Get the Parent ServiceInfo
76         ServicesInfo servicesInfo = FlowBasedServicesUtils.getServicesInfoForInterface(interfaceName, serviceMode, dataBroker);
77         if (servicesInfo == null) {
78             LOG.error("Reached Impossible part 1 in the code during bind service for: {}", boundServiceNew);
79             return futures;
80         }
81
82         List<BoundServices> allServices = servicesInfo.getBoundServices();
83         if (allServices.isEmpty()) {
84             LOG.error("Reached Impossible part 2 in the code during bind service for: {}", boundServiceNew);
85             return futures;
86         }
87
88         // Split based on type of interface....
89         if (ifState.getType().isAssignableFrom(L2vlan.class)) {
90             return bindServiceOnVlan(boundServiceNew, allServices, ifState, dataBroker);
91         } else if (ifState.getType().isAssignableFrom(Tunnel.class)) {
92             return bindServiceOnTunnel(boundServiceNew, allServices, ifState, dataBroker);
93         }
94         return futures;
95     }
96
97     private static List<ListenableFuture<Void>> bindServiceOnTunnel(BoundServices boundServiceNew, List<BoundServices> allServices,
98                                                                     org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface ifState, DataBroker dataBroker) {
99         // TODO - binding egress services on tunnels is not supported currently
100         return null;
101     }
102
103     private static List<ListenableFuture<Void>> bindServiceOnVlan(BoundServices boundServiceNew, List<BoundServices> allServices,
104                                                                   org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface ifState, DataBroker dataBroker) {
105         List<ListenableFuture<Void>> futures = new ArrayList<>();
106         BigInteger dpId = FlowBasedServicesUtils.getDpnIdFromInterface(ifState);
107         WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
108         LOG.info("binding egress service for vlan port: {}", ifState.getName());
109         if (allServices.size() == 1) {
110             //calling LportDispatcherTableForService with current service index as 0 and next service index as
111             // some value since this is the only service bound.
112             FlowBasedServicesUtils.installEgressDispatcherFlow(dpId, boundServiceNew, ifState.getName(),
113                     transaction, ifState.getIfIndex(), NwConstants.DEFAULT_SERVICE_INDEX, (short) (boundServiceNew.getServicePriority() + 1));
114             if (transaction != null) {
115                 futures.add(transaction.submit());
116             }
117             return futures;
118         }
119         allServices.remove(boundServiceNew);
120         BoundServices[] highLowPriorityService = FlowBasedServicesUtils.getHighAndLowPriorityService(allServices, boundServiceNew);
121         BoundServices low = highLowPriorityService[0];
122         BoundServices high = highLowPriorityService[1];
123         BoundServices highest = FlowBasedServicesUtils.getHighestPriorityService(allServices);
124         short currentServiceIndex = NwConstants.DEFAULT_SERVICE_INDEX;
125         short nextServiceIndex = ServiceIndex.getIndex(NwConstants.DEFAULT_EGRESS_SERVICE_NAME, NwConstants.DEFAULT_EGRESS_SERVICE_INDEX); // dummy service index
126         if (low != null) {
127             nextServiceIndex = low.getServicePriority();
128             if (low.equals(highest)) {
129                 //In this case the match criteria of existing service should be changed.
130                 BoundServices lower = FlowBasedServicesUtils.getHighAndLowPriorityService(allServices, low)[0];
131                 short lowerServiceIndex = (short) ((lower != null) ? lower.getServicePriority() : low.getServicePriority() + 1);
132                 FlowBasedServicesUtils.installEgressDispatcherFlow(dpId, low, ifState.getName(), transaction, ifState.getIfIndex(), low.getServicePriority(), lowerServiceIndex);
133             } else {
134                 currentServiceIndex = boundServiceNew.getServicePriority();
135             }
136         }
137         if (high != null) {
138             currentServiceIndex = boundServiceNew.getServicePriority();
139             if (high.equals(highest)) {
140                 FlowBasedServicesUtils.installEgressDispatcherFlow(dpId, high, ifState.getName(), transaction, ifState.getIfIndex(), NwConstants.DEFAULT_SERVICE_INDEX, currentServiceIndex);
141             } else {
142                 FlowBasedServicesUtils.installEgressDispatcherFlow(dpId, high, ifState.getName(), transaction, ifState.getIfIndex(), high.getServicePriority(), currentServiceIndex);
143             }
144         }
145         FlowBasedServicesUtils.installEgressDispatcherFlow(dpId, boundServiceNew, ifState.getName(), transaction, ifState.getIfIndex(), currentServiceIndex, nextServiceIndex);
146         futures.add(transaction.submit());
147         return futures;
148     }
149 }