556110e19d1f405dd0f7a007e68f7672d261cbf8
[genius.git] / interfacemanager / interfacemanager-impl / src / main / java / org / opendaylight / genius / interfacemanager / servicebindings / flowbased / confighelpers / FlowBasedServicesConfigBindHelper.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.confighelpers;
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.IfmConstants;
14 import org.opendaylight.genius.interfacemanager.IfmUtil;
15 import org.opendaylight.genius.interfacemanager.commons.InterfaceManagerCommonUtils;
16 import org.opendaylight.genius.interfacemanager.servicebindings.flowbased.utilities.FlowBasedServicesUtils;
17 import org.opendaylight.genius.mdsalutil.MatchInfo;
18 import org.opendaylight.genius.mdsalutil.NwConstants;
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.Interface;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.ServiceModeBase;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.ServicesInfo;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.services.info.BoundServices;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 import java.math.BigInteger;
33 import java.util.ArrayList;
34 import java.util.List;
35 import java.util.Map;
36 import java.util.concurrent.ConcurrentHashMap;
37
38 public class FlowBasedServicesConfigBindHelper {
39     private static final Logger LOG = LoggerFactory.getLogger(FlowBasedServicesConfigBindHelper.class);
40
41     public static List<ListenableFuture<Void>> bindService(InstanceIdentifier<BoundServices> instanceIdentifier,
42                                                            BoundServices boundServiceNew, DataBroker dataBroker) {
43         List<ListenableFuture<Void>> futures = new ArrayList<>();
44         String interfaceName =
45                 InstanceIdentifier.keyOf(instanceIdentifier.firstIdentifierOf(ServicesInfo.class)).getInterfaceName();
46         Class<? extends ServiceModeBase> serviceMode = InstanceIdentifier.keyOf(instanceIdentifier.firstIdentifierOf(ServicesInfo.class)).getServiceMode();
47
48         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface ifState =
49                 InterfaceManagerCommonUtils.getInterfaceStateFromOperDS(interfaceName, dataBroker);
50         if (ifState == null || ifState.getOperStatus() == OperStatus.Down) {
51             LOG.warn("Interface not up, not Binding Service for Interface: {}", interfaceName);
52             return futures;
53         }
54
55         // Get the Parent ServiceInfo
56
57         ServicesInfo servicesInfo = FlowBasedServicesUtils.getServicesInfoForInterface(interfaceName, serviceMode, dataBroker);
58         if (servicesInfo == null) {
59             LOG.error("Reached Impossible part 1 in the code during bind service for: {}", boundServiceNew);
60             return futures;
61         }
62
63         List<BoundServices> allServices = servicesInfo.getBoundServices();
64         if (allServices.isEmpty()) {
65             LOG.error("Reached Impossible part 2 in the code during bind service for: {}", boundServiceNew);
66             return futures;
67         }
68
69         // Split based on type of interface....
70         if (ifState.getType().isAssignableFrom(L2vlan.class)) {
71             return bindServiceOnVlan(boundServiceNew, allServices, ifState, dataBroker);
72         } else if (ifState.getType().isAssignableFrom(Tunnel.class)) {
73             return bindServiceOnTunnel(boundServiceNew, allServices, ifState, dataBroker);
74         }
75         return futures;
76     }
77
78     private static List<ListenableFuture<Void>> bindServiceOnTunnel(BoundServices boundServiceNew, List<BoundServices> allServices,
79                                                                     org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface ifState, DataBroker dataBroker) {
80         List<ListenableFuture<Void>> futures = new ArrayList<>();
81         NodeConnectorId nodeConnectorId = FlowBasedServicesUtils.getNodeConnectorIdFromInterface(ifState);
82         long portNo = Long.parseLong(IfmUtil.getPortNoFromNodeConnectorId(nodeConnectorId));
83         BigInteger dpId = new BigInteger(IfmUtil.getDpnFromNodeConnectorId(nodeConnectorId));
84         WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
85         Interface iface = InterfaceManagerCommonUtils.getInterfaceFromConfigDS(ifState.getName(), dataBroker);
86         if (allServices.size() == 1) {
87             // If only one service present, install instructions in table 0.
88             List<MatchInfo> matches = null;
89             matches = FlowBasedServicesUtils.getMatchInfoForTunnelPortAtIngressTable (dpId, portNo);
90             FlowBasedServicesUtils.installInterfaceIngressFlow(dpId, iface, boundServiceNew,
91                     transaction, matches, ifState.getIfIndex(), NwConstants.VLAN_INTERFACE_INGRESS_TABLE);
92             if (transaction != null) {
93                 futures.add(transaction.submit());
94             }
95             return futures;
96         }
97
98         boolean isCurrentServiceHighestPriority = true;
99         Map<Short, BoundServices> tmpServicesMap = new ConcurrentHashMap<>();
100         short highestPriority = 0xFF;
101         for (BoundServices boundService : allServices) {
102             if (boundService.getServicePriority() < boundServiceNew.getServicePriority()) {
103                 isCurrentServiceHighestPriority = false;
104                 break;
105             }
106             if (!boundService.equals(boundServiceNew)) {
107                 tmpServicesMap.put(boundService.getServicePriority(), boundService);
108                 if (boundService.getServicePriority() < highestPriority) {
109                     highestPriority = boundService.getServicePriority();
110                 }
111             }
112         }
113
114         if (!isCurrentServiceHighestPriority) {
115             FlowBasedServicesUtils.installLPortDispatcherFlow(dpId, boundServiceNew, ifState.getName(), transaction,
116                     ifState.getIfIndex(), boundServiceNew.getServicePriority(), (short) (boundServiceNew.getServicePriority()+1));
117         } else {
118             BoundServices serviceToReplace = tmpServicesMap.get(highestPriority);
119             FlowBasedServicesUtils.installLPortDispatcherFlow(dpId, serviceToReplace, ifState.getName(), transaction,
120                     ifState.getIfIndex(), boundServiceNew.getServicePriority(), (short) (boundServiceNew.getServicePriority()+1));
121             List<MatchInfo> matches = null;
122             matches = FlowBasedServicesUtils.getMatchInfoForTunnelPortAtIngressTable (dpId, portNo);
123
124             if (matches != null) {
125
126                 WriteTransaction removeFlowTransaction = dataBroker.newWriteOnlyTransaction();
127                 FlowBasedServicesUtils.removeIngressFlow(iface.getName(), serviceToReplace, dpId, removeFlowTransaction);
128                 futures.add(removeFlowTransaction.submit());
129
130                 WriteTransaction installFlowTransaction = dataBroker.newWriteOnlyTransaction();
131                 FlowBasedServicesUtils.installInterfaceIngressFlow(dpId, iface, boundServiceNew, installFlowTransaction,
132                         matches, ifState.getIfIndex(), NwConstants.VLAN_INTERFACE_INGRESS_TABLE);
133                 futures.add(installFlowTransaction.submit());
134             }
135         }
136
137         if (transaction != null) {
138             futures.add(transaction.submit());
139         }
140         return futures;
141     }
142
143     private static List<ListenableFuture<Void>> bindServiceOnVlan(BoundServices boundServiceNew, List<BoundServices> allServices,
144                                                                   org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface ifState, DataBroker dataBroker) {
145         List<ListenableFuture<Void>> futures = new ArrayList<>();
146         BigInteger dpId = FlowBasedServicesUtils.getDpnIdFromInterface(ifState);
147         WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
148         LOG.warn("binding service for vlan port: {}", ifState.getName());
149         if (allServices.size() == 1) {
150             //calling LportDispatcherTableForService with current service index as 0 and next service index as some value since this is the only service bound.
151             FlowBasedServicesUtils.installLPortDispatcherFlow(dpId, boundServiceNew, ifState.getName(),
152                     transaction, ifState.getIfIndex(), IfmConstants.DEFAULT_SERVICE_INDEX,(short) (boundServiceNew.getServicePriority() + 1));
153             if (transaction != null) {
154                 futures.add(transaction.submit());
155             }
156             return futures;
157         }
158         allServices.remove(boundServiceNew);
159         BoundServices[] highLowPriorityService = FlowBasedServicesUtils.getHighAndLowPriorityService(allServices, boundServiceNew);
160         BoundServices low = highLowPriorityService[0];
161         BoundServices high = highLowPriorityService[1];
162         BoundServices highest = FlowBasedServicesUtils.getHighestPriorityService(allServices);
163         short currentServiceIndex = IfmConstants.DEFAULT_SERVICE_INDEX;
164         short nextServiceIndex = (short) (boundServiceNew.getServicePriority() + 1); // dummy service index
165         if (low != null) {
166             nextServiceIndex = low.getServicePriority();
167             if (low.equals(highest)) {
168                 //In this case the match criteria of existing service should be changed.
169                 BoundServices lower = FlowBasedServicesUtils.getHighAndLowPriorityService(allServices, low)[0];
170                 short lowerServiceIndex = (short) ((lower!=null) ? lower.getServicePriority() : low.getServicePriority() + 1);
171                 LOG.trace("Installing table 30 entry for existing service {} service match on service index {} update with service index {}", low, low.getServicePriority(), lowerServiceIndex);
172                 FlowBasedServicesUtils.installLPortDispatcherFlow(dpId,low, ifState.getName(), transaction, ifState.getIfIndex(),low.getServicePriority(), lowerServiceIndex);
173             } else {
174                 currentServiceIndex = boundServiceNew.getServicePriority();
175             }
176         }
177         if (high != null) {
178             currentServiceIndex = boundServiceNew.getServicePriority();
179             if (high.equals(highest)) {
180                 LOG.trace("Installing table 30 entry for existing service {} service match on service index {} update with service index {}", high, IfmConstants.DEFAULT_SERVICE_INDEX, currentServiceIndex);
181                 FlowBasedServicesUtils.installLPortDispatcherFlow(dpId, high, ifState.getName(), transaction, ifState.getIfIndex(), IfmConstants.DEFAULT_SERVICE_INDEX, currentServiceIndex);
182             } else {
183                 LOG.trace("Installing table 30 entry for existing service {} service match on service index {} update with service index {}", high, high.getServicePriority(), currentServiceIndex);
184                 FlowBasedServicesUtils.installLPortDispatcherFlow(dpId, high, ifState.getName(), transaction, ifState.getIfIndex(), high.getServicePriority(), currentServiceIndex);
185             }
186         }
187         LOG.trace("Installing table 30 entry for new service match on service index {} update with service index {}", currentServiceIndex, nextServiceIndex);
188         FlowBasedServicesUtils.installLPortDispatcherFlow(dpId, boundServiceNew, ifState.getName(), transaction, ifState.getIfIndex(), currentServiceIndex, nextServiceIndex);
189         futures.add(transaction.submit());
190         return futures;
191     }
192 }