Merge "Add support for IPv6 VXLAN tunnels"
[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 java.math.BigInteger;
12 import java.util.ArrayList;
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.InterfacemgrProvider;
17 import org.opendaylight.genius.interfacemanager.commons.InterfaceManagerCommonUtils;
18 import org.opendaylight.genius.interfacemanager.servicebindings.flowbased.config.factory.FlowBasedServicesConfigAddable;
19 import org.opendaylight.genius.interfacemanager.servicebindings.flowbased.utilities.FlowBasedServicesUtils;
20 import org.opendaylight.genius.mdsalutil.NwConstants;
21 import org.opendaylight.genius.utils.ServiceIndex;
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.Interface;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.ServiceModeBase;
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.yangtools.yang.binding.InstanceIdentifier;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
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         Interface iface = InterfaceManagerCommonUtils.getInterfaceFromConfigDS(ifState.getName(), dataBroker);
109         LOG.info("binding egress service {} for vlan port: {}", boundServiceNew.getServiceName(), ifState.getName());
110         if (allServices.size() == 1) {
111             //calling LportDispatcherTableForService with current service index as 0 and next service index as
112             // some value since this is the only service bound.
113             FlowBasedServicesUtils.installEgressDispatcherFlows(dpId, boundServiceNew, ifState.getName(),
114                     transaction, ifState.getIfIndex(), NwConstants.DEFAULT_SERVICE_INDEX, (short) (boundServiceNew.getServicePriority() + 1), iface);
115             if (transaction != null) {
116                 futures.add(transaction.submit());
117             }
118             return futures;
119         }
120         allServices.remove(boundServiceNew);
121         BoundServices[] highLowPriorityService = FlowBasedServicesUtils.getHighAndLowPriorityService(allServices, boundServiceNew);
122         BoundServices low = highLowPriorityService[0];
123         BoundServices high = highLowPriorityService[1];
124         BoundServices highest = FlowBasedServicesUtils.getHighestPriorityService(allServices);
125         short currentServiceIndex = NwConstants.DEFAULT_SERVICE_INDEX;
126         short nextServiceIndex = ServiceIndex.getIndex(NwConstants.DEFAULT_EGRESS_SERVICE_NAME, NwConstants.DEFAULT_EGRESS_SERVICE_INDEX); // dummy service index
127         if (low != null) {
128             nextServiceIndex = low.getServicePriority();
129             if (low.equals(highest)) {
130                 //In this case the match criteria of existing service should be changed.
131                 BoundServices lower = FlowBasedServicesUtils.getHighAndLowPriorityService(allServices, low)[0];
132                 short lowerServiceIndex = (short) ((lower != null) ? lower.getServicePriority() : low.getServicePriority() + 1);
133                 LOG.trace("Installing egress dispatcher table entry for existing service {} service match on "
134                         + "service index {} update with service index {}",
135                     low, low.getServicePriority(), lowerServiceIndex);
136                 FlowBasedServicesUtils.installEgressDispatcherFlows(dpId, low, ifState.getName(), transaction, ifState.getIfIndex(),
137                         low.getServicePriority(), lowerServiceIndex, iface);
138             } else {
139                 currentServiceIndex = boundServiceNew.getServicePriority();
140             }
141         }
142         if (high != null) {
143             currentServiceIndex = boundServiceNew.getServicePriority();
144             if (high.equals(highest)) {
145                 LOG.trace("Installing egress dispatcher table entry for existing service {} service match on "
146                         + "service index {} update with service index {}",
147                     high, NwConstants.DEFAULT_SERVICE_INDEX, currentServiceIndex);
148                 FlowBasedServicesUtils.installEgressDispatcherFlows(dpId, high, ifState.getName(), transaction, ifState.getIfIndex(), NwConstants.DEFAULT_SERVICE_INDEX, currentServiceIndex, iface);
149             } else {
150                 LOG.trace("Installing egress dispatcher table entry for existing service {} service match on "
151                         + "service index {} update with service index {}",
152                     high, high.getServicePriority(), currentServiceIndex);
153                 FlowBasedServicesUtils.installEgressDispatcherFlows(dpId, high, ifState.getName(), transaction, ifState.getIfIndex(), high.getServicePriority(), currentServiceIndex, iface);
154             }
155         }
156         LOG.trace("Installing egress dispatcher table entry for new service match on service index {} update with service index {}",
157             currentServiceIndex, nextServiceIndex);
158         FlowBasedServicesUtils.installEgressDispatcherFlows(dpId, boundServiceNew, ifState.getName(), transaction, ifState.getIfIndex(), currentServiceIndex, nextServiceIndex, iface);
159         futures.add(transaction.submit());
160         return futures;
161     }
162 }