Fix issues related to checkstyle enforcement for module
[genius.git] / interfacemanager / interfacemanager-impl / src / main / java / org / opendaylight / genius / interfacemanager / servicebindings / flowbased / config / helpers / FlowBasedIngressServicesConfigBindHelper.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.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.opendaylight.genius.interfacemanager.servicebinding.rev160406.ServiceModeBase;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.ServicesInfo;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.services.info.BoundServices;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 public class FlowBasedIngressServicesConfigBindHelper implements FlowBasedServicesConfigAddable {
37     private static final Logger LOG = LoggerFactory.getLogger(FlowBasedIngressServicesConfigBindHelper.class);
38
39     private final InterfacemgrProvider interfaceMgrProvider;
40     private static volatile FlowBasedServicesConfigAddable flowBasedIngressServicesAddable;
41
42     private FlowBasedIngressServicesConfigBindHelper(InterfacemgrProvider interfaceMgrProvider) {
43         this.interfaceMgrProvider = interfaceMgrProvider;
44     }
45
46     public static void intitializeFlowBasedIngressServicesConfigAddHelper(InterfacemgrProvider interfaceMgrProvider) {
47         if (flowBasedIngressServicesAddable == null) {
48             synchronized (FlowBasedIngressServicesConfigBindHelper.class) {
49                 if (flowBasedIngressServicesAddable == null) {
50                     flowBasedIngressServicesAddable = new FlowBasedIngressServicesConfigBindHelper(
51                             interfaceMgrProvider);
52                 }
53             }
54         }
55     }
56
57     public static void clearFlowBasedIngressServicesConfigAddHelper() {
58         flowBasedIngressServicesAddable = null;
59     }
60
61     public static FlowBasedServicesConfigAddable getFlowBasedIngressServicesAddHelper() {
62         if (flowBasedIngressServicesAddable == null) {
63             LOG.error("OvsInterfaceConfigAdd Renderer is not initialized");
64         }
65         return flowBasedIngressServicesAddable;
66     }
67
68     @Override
69     public List<ListenableFuture<Void>> bindService(InstanceIdentifier<BoundServices> instanceIdentifier,
70             BoundServices boundServiceNew) {
71         List<ListenableFuture<Void>> futures = new ArrayList<>();
72         DataBroker dataBroker = interfaceMgrProvider.getDataBroker();
73         String interfaceName = InstanceIdentifier.keyOf(instanceIdentifier.firstIdentifierOf(ServicesInfo.class))
74                 .getInterfaceName();
75         Class<? extends ServiceModeBase> serviceMode = InstanceIdentifier
76                 .keyOf(instanceIdentifier.firstIdentifierOf(ServicesInfo.class)).getServiceMode();
77
78         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
79             .ietf.interfaces.rev140508.interfaces.state.Interface ifState = InterfaceManagerCommonUtils
80                 .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
112                 .ietf.interfaces.rev140508.interfaces.state.Interface ifState,
113             DataBroker dataBroker) {
114         List<ListenableFuture<Void>> futures = new ArrayList<>();
115         NodeConnectorId nodeConnectorId = FlowBasedServicesUtils.getNodeConnectorIdFromInterface(ifState);
116         long portNo = IfmUtil.getPortNumberFromNodeConnectorId(nodeConnectorId);
117         BigInteger dpId = IfmUtil.getDpnFromNodeConnectorId(nodeConnectorId);
118         WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
119         Interface iface = InterfaceManagerCommonUtils.getInterfaceFromConfigDS(ifState.getName(), dataBroker);
120         if (allServices.size() == 1) {
121             // If only one service present, install instructions in table 0.
122             List<MatchInfo> matches = null;
123             matches = FlowBasedServicesUtils.getMatchInfoForTunnelPortAtIngressTable(dpId, portNo);
124             FlowBasedServicesUtils.installInterfaceIngressFlow(dpId, iface, boundServiceNew, transaction, matches,
125                     ifState.getIfIndex(), NwConstants.VLAN_INTERFACE_INGRESS_TABLE);
126             if (transaction != null) {
127                 futures.add(transaction.submit());
128             }
129             return futures;
130         }
131
132         boolean isCurrentServiceHighestPriority = true;
133         Map<Short, BoundServices> tmpServicesMap = new ConcurrentHashMap<>();
134         short highestPriority = 0xFF;
135         for (BoundServices boundService : allServices) {
136             if (boundService.getServicePriority() < boundServiceNew.getServicePriority()) {
137                 isCurrentServiceHighestPriority = false;
138                 break;
139             }
140             if (!boundService.equals(boundServiceNew)) {
141                 tmpServicesMap.put(boundService.getServicePriority(), boundService);
142                 if (boundService.getServicePriority() < highestPriority) {
143                     highestPriority = boundService.getServicePriority();
144                 }
145             }
146         }
147
148         if (!isCurrentServiceHighestPriority) {
149             FlowBasedServicesUtils.installLPortDispatcherFlow(dpId, boundServiceNew, ifState.getName(), transaction,
150                     ifState.getIfIndex(), boundServiceNew.getServicePriority(),
151                     (short) (boundServiceNew.getServicePriority() + 1));
152         } else {
153             BoundServices serviceToReplace = tmpServicesMap.get(highestPriority);
154             FlowBasedServicesUtils.installLPortDispatcherFlow(dpId, serviceToReplace, ifState.getName(), transaction,
155                     ifState.getIfIndex(), serviceToReplace.getServicePriority(),
156                     (short) (serviceToReplace.getServicePriority() + 1));
157             List<MatchInfo> 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
181                 .ietf.interfaces.rev140508.interfaces.state.Interface ifState,
182             DataBroker dataBroker) {
183         List<ListenableFuture<Void>> futures = new ArrayList<>();
184         BigInteger dpId = FlowBasedServicesUtils.getDpnIdFromInterface(ifState);
185         WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
186         LOG.info("binding ingress service {} for vlan port: {}", boundServiceNew.getServiceName(), ifState.getName());
187         if (allServices.size() == 1) {
188             // calling LportDispatcherTableForService with current service index
189             // as 0 and next service index as some value since this is the only
190             // service bound.
191             FlowBasedServicesUtils.installLPortDispatcherFlow(dpId, boundServiceNew, ifState.getName(), transaction,
192                     ifState.getIfIndex(), NwConstants.DEFAULT_SERVICE_INDEX,
193                     (short) (boundServiceNew.getServicePriority() + 1));
194             if (transaction != null) {
195                 futures.add(transaction.submit());
196             }
197             return futures;
198         }
199         allServices.remove(boundServiceNew);
200         BoundServices[] highLowPriorityService = FlowBasedServicesUtils.getHighAndLowPriorityService(allServices,
201                 boundServiceNew);
202         BoundServices low = highLowPriorityService[0];
203         BoundServices high = highLowPriorityService[1];
204         BoundServices highest = FlowBasedServicesUtils.getHighestPriorityService(allServices);
205         short currentServiceIndex = NwConstants.DEFAULT_SERVICE_INDEX;
206         short nextServiceIndex = (short) (boundServiceNew.getServicePriority() + 1); // dummy
207                                                                                         // service
208                                                                                         // 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
213                 // changed.
214                 BoundServices lower = FlowBasedServicesUtils.getHighAndLowPriorityService(allServices, low)[0];
215                 short lowerServiceIndex = (short) (lower != null ? lower.getServicePriority()
216                         : low.getServicePriority() + 1);
217                 LOG.trace(
218                         "Installing ingress dispatcher table entry for existing service {} service match on "
219                                 + "service index {} update with service index {}",
220                         low, low.getServicePriority(), lowerServiceIndex);
221                 FlowBasedServicesUtils.installLPortDispatcherFlow(dpId, low, ifState.getName(), transaction,
222                         ifState.getIfIndex(), low.getServicePriority(), lowerServiceIndex);
223             } else {
224                 currentServiceIndex = boundServiceNew.getServicePriority();
225             }
226         }
227         if (high != null) {
228             currentServiceIndex = boundServiceNew.getServicePriority();
229             if (high.equals(highest)) {
230                 LOG.trace(
231                         "Installing ingress dispatcher table entry for existing service {} service match on "
232                                 + "service index {} update with service index {}",
233                         high, NwConstants.DEFAULT_SERVICE_INDEX, currentServiceIndex);
234                 FlowBasedServicesUtils.installLPortDispatcherFlow(dpId, high, ifState.getName(), transaction,
235                         ifState.getIfIndex(), NwConstants.DEFAULT_SERVICE_INDEX, currentServiceIndex);
236             } else {
237                 LOG.trace(
238                         "Installing ingress dispatcher table entry for existing service {} service match on "
239                                 + "service index {} update with service index {}",
240                         high, high.getServicePriority(), currentServiceIndex);
241                 FlowBasedServicesUtils.installLPortDispatcherFlow(dpId, high, ifState.getName(), transaction,
242                         ifState.getIfIndex(), high.getServicePriority(), currentServiceIndex);
243             }
244         }
245         LOG.trace("Installing ingress dispatcher table entry for new service match on service index {} update with "
246                 + "service index {}", currentServiceIndex, nextServiceIndex);
247         FlowBasedServicesUtils.installLPortDispatcherFlow(dpId, boundServiceNew, ifState.getName(), transaction,
248                 ifState.getIfIndex(), currentServiceIndex, nextServiceIndex);
249         futures.add(transaction.submit());
250         return futures;
251     }
252 }