Merge "Spec to support load balancing and high availability of multiple VxLAN tunnels"
[genius.git] / interfacemanager / interfacemanager-impl / src / main / java / org / opendaylight / genius / interfacemanager / servicebindings / flowbased / config / helpers / FlowBasedIngressServicesConfigBindHelper.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 java.util.Map;
15 import java.util.concurrent.ConcurrentHashMap;
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
18 import org.opendaylight.genius.interfacemanager.IfmUtil;
19 import org.opendaylight.genius.interfacemanager.InterfacemgrProvider;
20 import org.opendaylight.genius.interfacemanager.commons.InterfaceManagerCommonUtils;
21 import org.opendaylight.genius.interfacemanager.servicebindings.flowbased.config.factory.FlowBasedServicesConfigAddable;
22 import org.opendaylight.genius.interfacemanager.servicebindings.flowbased.utilities.FlowBasedServicesUtils;
23 import org.opendaylight.genius.mdsalutil.MatchInfo;
24 import org.opendaylight.genius.mdsalutil.NwConstants;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.L2vlan;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.Tunnel;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.ServiceModeBase;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.ServicesInfo;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.services.info.BoundServices;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 public class FlowBasedIngressServicesConfigBindHelper implements FlowBasedServicesConfigAddable {
38     private static final Logger LOG = LoggerFactory.getLogger(FlowBasedIngressServicesConfigBindHelper.class);
39
40     private final InterfacemgrProvider interfaceMgrProvider;
41     private static volatile FlowBasedServicesConfigAddable flowBasedIngressServicesAddable;
42
43     private FlowBasedIngressServicesConfigBindHelper(InterfacemgrProvider interfaceMgrProvider) {
44         this.interfaceMgrProvider = interfaceMgrProvider;
45     }
46
47     public static void intitializeFlowBasedIngressServicesConfigAddHelper(InterfacemgrProvider interfaceMgrProvider) {
48         if (flowBasedIngressServicesAddable == null) {
49             synchronized (FlowBasedIngressServicesConfigBindHelper.class) {
50                 if (flowBasedIngressServicesAddable == null) {
51                     flowBasedIngressServicesAddable = new FlowBasedIngressServicesConfigBindHelper(
52                             interfaceMgrProvider);
53                 }
54             }
55         }
56     }
57
58     public static void clearFlowBasedIngressServicesConfigAddHelper() {
59         flowBasedIngressServicesAddable = null;
60     }
61
62     public static FlowBasedServicesConfigAddable getFlowBasedIngressServicesAddHelper() {
63         if (flowBasedIngressServicesAddable == null) {
64             LOG.error("OvsInterfaceConfigAdd Renderer is not initialized");
65         }
66         return flowBasedIngressServicesAddable;
67     }
68
69     @Override
70     public List<ListenableFuture<Void>> bindService(InstanceIdentifier<BoundServices> instanceIdentifier,
71                                                            BoundServices boundServiceNew) {
72         List<ListenableFuture<Void>> futures = new ArrayList<>();
73         DataBroker dataBroker = interfaceMgrProvider.getDataBroker();
74         String interfaceName =
75                 InstanceIdentifier.keyOf(instanceIdentifier.firstIdentifierOf(ServicesInfo.class)).getInterfaceName();
76         Class<? extends ServiceModeBase> serviceMode = InstanceIdentifier
77                 .keyOf(instanceIdentifier.firstIdentifierOf(ServicesInfo.class)).getServiceMode();
78
79         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state
80             .Interface ifState = InterfaceManagerCommonUtils.getInterfaceStateFromOperDS(interfaceName, dataBroker);
81         if (ifState == null) {
82             LOG.warn("Interface not operational, not binding Service for Interface: {}", interfaceName);
83             return futures;
84         }
85
86         // Get the Parent ServiceInfo
87
88         ServicesInfo servicesInfo = FlowBasedServicesUtils.getServicesInfoForInterface(interfaceName, serviceMode,
89                 dataBroker);
90         if (servicesInfo == null) {
91             LOG.error("Reached Impossible part 1 in the code during bind service for: {}", boundServiceNew);
92             return futures;
93         }
94
95         List<BoundServices> allServices = servicesInfo.getBoundServices();
96         if (allServices.isEmpty()) {
97             LOG.error("Reached Impossible part 2 in the code during bind service for: {}", boundServiceNew);
98             return futures;
99         }
100         // Split based on type of interface...
101         if (L2vlan.class.equals(ifState.getType())) {
102             return bindServiceOnVlan(boundServiceNew, allServices, ifState, dataBroker);
103         } else if (Tunnel.class.equals(ifState.getType())) {
104             return bindServiceOnTunnel(boundServiceNew, allServices, ifState, dataBroker);
105         }
106         return futures;
107     }
108
109     private static List<ListenableFuture<Void>> bindServiceOnTunnel(BoundServices boundServiceNew,
110             List<BoundServices> allServices,
111             org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state
112             .Interface ifState, DataBroker dataBroker) {
113         List<ListenableFuture<Void>> futures = new ArrayList<>();
114         NodeConnectorId nodeConnectorId = FlowBasedServicesUtils.getNodeConnectorIdFromInterface(ifState);
115         long portNo = IfmUtil.getPortNumberFromNodeConnectorId(nodeConnectorId);
116         BigInteger dpId = IfmUtil.getDpnFromNodeConnectorId(nodeConnectorId);
117         WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
118         Interface iface = InterfaceManagerCommonUtils.getInterfaceFromConfigDS(ifState.getName(), dataBroker);
119         if (allServices.size() == 1) {
120             // If only one service present, install instructions in table 0.
121             List<MatchInfo> matches = null;
122             matches = FlowBasedServicesUtils.getMatchInfoForTunnelPortAtIngressTable(dpId, portNo);
123             FlowBasedServicesUtils.installInterfaceIngressFlow(dpId, iface, boundServiceNew,
124                     transaction, matches, ifState.getIfIndex(), NwConstants.VLAN_INTERFACE_INGRESS_TABLE);
125             if (transaction != null) {
126                 futures.add(transaction.submit());
127             }
128             return futures;
129         }
130
131         boolean isCurrentServiceHighestPriority = true;
132         Map<Short, BoundServices> tmpServicesMap = new ConcurrentHashMap<>();
133         short highestPriority = 0xFF;
134         for (BoundServices boundService : allServices) {
135             if (boundService.getServicePriority() < boundServiceNew.getServicePriority()) {
136                 isCurrentServiceHighestPriority = false;
137                 break;
138             }
139             if (!boundService.equals(boundServiceNew)) {
140                 tmpServicesMap.put(boundService.getServicePriority(), boundService);
141                 if (boundService.getServicePriority() < highestPriority) {
142                     highestPriority = boundService.getServicePriority();
143                 }
144             }
145         }
146
147         if (!isCurrentServiceHighestPriority) {
148             FlowBasedServicesUtils.installLPortDispatcherFlow(dpId, boundServiceNew, ifState.getName(), transaction,
149                     ifState.getIfIndex(), boundServiceNew.getServicePriority(),
150                     (short) (boundServiceNew.getServicePriority() + 1));
151         } else {
152             BoundServices serviceToReplace = tmpServicesMap.get(highestPriority);
153             FlowBasedServicesUtils.installLPortDispatcherFlow(dpId, serviceToReplace, ifState.getName(), transaction,
154                     ifState.getIfIndex(), serviceToReplace.getServicePriority(),
155                     (short) (serviceToReplace.getServicePriority() + 1));
156             List<MatchInfo> matches = null;
157             matches = FlowBasedServicesUtils.getMatchInfoForTunnelPortAtIngressTable(dpId, portNo);
158
159             if (matches != null) {
160                 WriteTransaction removeFlowTransaction = dataBroker.newWriteOnlyTransaction();
161                 FlowBasedServicesUtils.removeIngressFlow(iface.getName(), serviceToReplace, dpId,
162                         removeFlowTransaction);
163                 futures.add(removeFlowTransaction.submit());
164
165                 WriteTransaction installFlowTransaction = dataBroker.newWriteOnlyTransaction();
166                 FlowBasedServicesUtils.installInterfaceIngressFlow(dpId, iface, boundServiceNew, installFlowTransaction,
167                         matches, ifState.getIfIndex(), NwConstants.VLAN_INTERFACE_INGRESS_TABLE);
168                 futures.add(installFlowTransaction.submit());
169             }
170         }
171
172         if (transaction != null) {
173             futures.add(transaction.submit());
174         }
175         return futures;
176     }
177
178     private static List<ListenableFuture<Void>> bindServiceOnVlan(BoundServices boundServiceNew,
179             List<BoundServices> allServices,
180             org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state
181             .Interface ifState, DataBroker dataBroker) {
182         List<ListenableFuture<Void>> futures = new ArrayList<>();
183         BigInteger dpId = FlowBasedServicesUtils.getDpnIdFromInterface(ifState);
184         WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
185         LOG.info("binding ingress service {} for vlan port: {}", boundServiceNew.getServiceName(), ifState.getName());
186         if (allServices.size() == 1) {
187             // calling LportDispatcherTableForService with current service index
188             // as 0 and next service index as some value since this is the only
189             // service bound.
190             FlowBasedServicesUtils.installLPortDispatcherFlow(dpId, boundServiceNew, ifState.getName(), transaction,
191                     ifState.getIfIndex(), NwConstants.DEFAULT_SERVICE_INDEX,
192                     (short) (boundServiceNew.getServicePriority() + 1));
193             if (transaction != null) {
194                 futures.add(transaction.submit());
195             }
196             return futures;
197         }
198         allServices.remove(boundServiceNew);
199         BoundServices[] highLowPriorityService = FlowBasedServicesUtils.getHighAndLowPriorityService(allServices,
200                 boundServiceNew);
201         BoundServices low = highLowPriorityService[0];
202         BoundServices high = highLowPriorityService[1];
203         BoundServices highest = FlowBasedServicesUtils.getHighestPriorityService(allServices);
204         short currentServiceIndex = NwConstants.DEFAULT_SERVICE_INDEX;
205         short nextServiceIndex = (short) (boundServiceNew.getServicePriority() + 1); // dummy service index
206         if (low != null) {
207             nextServiceIndex = low.getServicePriority();
208             if (low.equals(highest)) {
209                 //In this case the match criteria of existing service should be changed.
210                 BoundServices lower = FlowBasedServicesUtils.getHighAndLowPriorityService(allServices, low)[0];
211                 short lowerServiceIndex = (short) (lower != null ? lower.getServicePriority()
212                         : low.getServicePriority() + 1);
213                 LOG.trace("Installing ingress dispatcher table entry for existing service {} service match on "
214                         + "service index {} update with service index {}",
215                         low, low.getServicePriority(), lowerServiceIndex);
216                 FlowBasedServicesUtils.installLPortDispatcherFlow(dpId, low, ifState.getName(), transaction,
217                         ifState.getIfIndex(), low.getServicePriority(), lowerServiceIndex);
218             } else {
219                 currentServiceIndex = boundServiceNew.getServicePriority();
220             }
221         }
222         if (high != null) {
223             currentServiceIndex = boundServiceNew.getServicePriority();
224             if (high.equals(highest)) {
225                 LOG.trace("Installing ingress dispatcher table entry for existing service {} service match on "
226                         + "service index {} update with service index {}",
227                         high, NwConstants.DEFAULT_SERVICE_INDEX, currentServiceIndex);
228                 FlowBasedServicesUtils.installLPortDispatcherFlow(dpId, high, ifState.getName(), transaction,
229                         ifState.getIfIndex(), NwConstants.DEFAULT_SERVICE_INDEX, currentServiceIndex);
230             } else {
231                 LOG.trace("Installing ingress dispatcher table entry for existing service {} service match on "
232                         + "service index {} update with service index {}",
233                         high, high.getServicePriority(), currentServiceIndex);
234                 FlowBasedServicesUtils.installLPortDispatcherFlow(dpId, high, ifState.getName(), transaction,
235                         ifState.getIfIndex(), high.getServicePriority(), currentServiceIndex);
236             }
237         }
238         LOG.trace("Installing ingress dispatcher table entry for new service match on service index {} update with " +
239                 "service index {}",
240                 currentServiceIndex, nextServiceIndex);
241         FlowBasedServicesUtils.installLPortDispatcherFlow(dpId, boundServiceNew, ifState.getName(), transaction,
242                 ifState.getIfIndex(), currentServiceIndex, nextServiceIndex);
243         futures.add(transaction.submit());
244         return futures;
245     }
246 }