Merge "Bug 7466 - NPE thrown for interface without lport tag"
[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 || ifState.getOperStatus() == OperStatus.Down) {
82             LOG.warn("Interface not up, 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         if (ifState.getType() == null) {
101             return futures;
102         }
103         // Split based on type of interface...
104         if (ifState.getType().isAssignableFrom(L2vlan.class)) {
105             return bindServiceOnVlan(boundServiceNew, allServices, ifState, dataBroker);
106         } else if (ifState.getType().isAssignableFrom(Tunnel.class)) {
107             return bindServiceOnTunnel(boundServiceNew, allServices, ifState, dataBroker);
108         }
109         return futures;
110     }
111
112     private static List<ListenableFuture<Void>> bindServiceOnTunnel(BoundServices boundServiceNew,
113             List<BoundServices> allServices,
114             org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state
115             .Interface ifState, DataBroker dataBroker) {
116         List<ListenableFuture<Void>> futures = new ArrayList<>();
117         NodeConnectorId nodeConnectorId = FlowBasedServicesUtils.getNodeConnectorIdFromInterface(ifState);
118         long portNo = IfmUtil.getPortNumberFromNodeConnectorId(nodeConnectorId);
119         BigInteger dpId = IfmUtil.getDpnFromNodeConnectorId(nodeConnectorId);
120         WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
121         Interface iface = InterfaceManagerCommonUtils.getInterfaceFromConfigDS(ifState.getName(), dataBroker);
122         if (allServices.size() == 1) {
123             // If only one service present, install instructions in table 0.
124             List<MatchInfo> matches = null;
125             matches = FlowBasedServicesUtils.getMatchInfoForTunnelPortAtIngressTable(dpId, portNo);
126             FlowBasedServicesUtils.installInterfaceIngressFlow(dpId, iface, boundServiceNew,
127                     transaction, matches, ifState.getIfIndex(), NwConstants.VLAN_INTERFACE_INGRESS_TABLE);
128             if (transaction != null) {
129                 futures.add(transaction.submit());
130             }
131             return futures;
132         }
133
134         boolean isCurrentServiceHighestPriority = true;
135         Map<Short, BoundServices> tmpServicesMap = new ConcurrentHashMap<>();
136         short highestPriority = 0xFF;
137         for (BoundServices boundService : allServices) {
138             if (boundService.getServicePriority() < boundServiceNew.getServicePriority()) {
139                 isCurrentServiceHighestPriority = false;
140                 break;
141             }
142             if (!boundService.equals(boundServiceNew)) {
143                 tmpServicesMap.put(boundService.getServicePriority(), boundService);
144                 if (boundService.getServicePriority() < highestPriority) {
145                     highestPriority = boundService.getServicePriority();
146                 }
147             }
148         }
149
150         if (!isCurrentServiceHighestPriority) {
151             FlowBasedServicesUtils.installLPortDispatcherFlow(dpId, boundServiceNew, ifState.getName(), transaction,
152                     ifState.getIfIndex(), boundServiceNew.getServicePriority(),
153                     (short) (boundServiceNew.getServicePriority() + 1));
154         } else {
155             BoundServices serviceToReplace = tmpServicesMap.get(highestPriority);
156             FlowBasedServicesUtils.installLPortDispatcherFlow(dpId, serviceToReplace, ifState.getName(), transaction,
157                     ifState.getIfIndex(), serviceToReplace.getServicePriority(),
158                     (short) (serviceToReplace.getServicePriority() + 1));
159             List<MatchInfo> matches = null;
160             matches = FlowBasedServicesUtils.getMatchInfoForTunnelPortAtIngressTable(dpId, portNo);
161
162             if (matches != null) {
163                 WriteTransaction removeFlowTransaction = dataBroker.newWriteOnlyTransaction();
164                 FlowBasedServicesUtils.removeIngressFlow(iface.getName(), serviceToReplace, dpId,
165                         removeFlowTransaction);
166                 futures.add(removeFlowTransaction.submit());
167
168                 WriteTransaction installFlowTransaction = dataBroker.newWriteOnlyTransaction();
169                 FlowBasedServicesUtils.installInterfaceIngressFlow(dpId, iface, boundServiceNew, installFlowTransaction,
170                         matches, ifState.getIfIndex(), NwConstants.VLAN_INTERFACE_INGRESS_TABLE);
171                 futures.add(installFlowTransaction.submit());
172             }
173         }
174
175         if (transaction != null) {
176             futures.add(transaction.submit());
177         }
178         return futures;
179     }
180
181     private static List<ListenableFuture<Void>> bindServiceOnVlan(BoundServices boundServiceNew,
182             List<BoundServices> allServices,
183             org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state
184             .Interface ifState, DataBroker dataBroker) {
185         List<ListenableFuture<Void>> futures = new ArrayList<>();
186         BigInteger dpId = FlowBasedServicesUtils.getDpnIdFromInterface(ifState);
187         WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
188         LOG.info("binding service for vlan port: {}", ifState.getName());
189         if (allServices.size() == 1) {
190             // calling LportDispatcherTableForService with current service index
191             // as 0 and next service index as some value since this is the only
192             // service bound.
193             FlowBasedServicesUtils.installLPortDispatcherFlow(dpId, boundServiceNew, ifState.getName(), transaction,
194                     ifState.getIfIndex(), NwConstants.DEFAULT_SERVICE_INDEX,
195                     (short) (boundServiceNew.getServicePriority() + 1));
196             if (transaction != null) {
197                 futures.add(transaction.submit());
198             }
199             return futures;
200         }
201         allServices.remove(boundServiceNew);
202         BoundServices[] highLowPriorityService = FlowBasedServicesUtils.getHighAndLowPriorityService(allServices,
203                 boundServiceNew);
204         BoundServices low = highLowPriorityService[0];
205         BoundServices high = highLowPriorityService[1];
206         BoundServices highest = FlowBasedServicesUtils.getHighestPriorityService(allServices);
207         short currentServiceIndex = NwConstants.DEFAULT_SERVICE_INDEX;
208         short nextServiceIndex = (short) (boundServiceNew.getServicePriority() + 1); // dummy service index
209         if (low != null) {
210             nextServiceIndex = low.getServicePriority();
211             if (low.equals(highest)) {
212                 //In this case the match criteria of existing service should be changed.
213                 BoundServices lower = FlowBasedServicesUtils.getHighAndLowPriorityService(allServices, low)[0];
214                 short lowerServiceIndex = (short) (lower != null ? lower.getServicePriority()
215                         : low.getServicePriority() + 1);
216                 LOG.trace("Installing table 17 entry for existing service {} service match on "
217                         + "service index {} update with service index {}",
218                         low, low.getServicePriority(), lowerServiceIndex);
219                 FlowBasedServicesUtils.installLPortDispatcherFlow(dpId, low, ifState.getName(), transaction,
220                         ifState.getIfIndex(), low.getServicePriority(), lowerServiceIndex);
221             } else {
222                 currentServiceIndex = boundServiceNew.getServicePriority();
223             }
224         }
225         if (high != null) {
226             currentServiceIndex = boundServiceNew.getServicePriority();
227             if (high.equals(highest)) {
228                 LOG.trace("Installing table 17 entry for existing service {} service match on "
229                         + "service index {} update with service index {}",
230                         high, NwConstants.DEFAULT_SERVICE_INDEX, currentServiceIndex);
231                 FlowBasedServicesUtils.installLPortDispatcherFlow(dpId, high, ifState.getName(), transaction,
232                         ifState.getIfIndex(), NwConstants.DEFAULT_SERVICE_INDEX, currentServiceIndex);
233             } else {
234                 LOG.trace("Installing table 17 entry for existing service {} service match on "
235                         + "service index {} update with service index {}",
236                         high, high.getServicePriority(), currentServiceIndex);
237                 FlowBasedServicesUtils.installLPortDispatcherFlow(dpId, high, ifState.getName(), transaction,
238                         ifState.getIfIndex(), high.getServicePriority(), currentServiceIndex);
239             }
240         }
241         LOG.trace("Installing table 17 entry for new service match on service index {} update with service index {}",
242                 currentServiceIndex, nextServiceIndex);
243         FlowBasedServicesUtils.installLPortDispatcherFlow(dpId, boundServiceNew, ifState.getName(), transaction,
244                 ifState.getIfIndex(), currentServiceIndex, nextServiceIndex);
245         futures.add(transaction.submit());
246         return futures;
247     }
248 }