Merge "Bind/Unbind Service should work irrespective of Port Status"
[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         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface ifState =
67                 InterfaceManagerCommonUtils.getInterfaceStateFromOperDS(interfaceName, dataBroker);
68         if (ifState == null) {
69             LOG.warn("Interface not operational, not binding Service for Interface: {}", interfaceName);
70             return futures;
71         }
72
73         Class<? extends ServiceModeBase> serviceMode =  InstanceIdentifier.keyOf(instanceIdentifier.firstIdentifierOf
74             (ServicesInfo.class)).getServiceMode();
75         // Get the Parent ServiceInfo
76         ServicesInfo servicesInfo = FlowBasedServicesUtils.getServicesInfoForInterface(interfaceName, serviceMode,
77             dataBroker);
78         if (servicesInfo == null) {
79             LOG.error("Reached Impossible part 1 in the code during bind service for: {}", boundServiceNew);
80             return futures;
81         }
82
83         List<BoundServices> allServices = servicesInfo.getBoundServices();
84         if (allServices.isEmpty()) {
85             LOG.error("Reached Impossible part 2 in the code during bind service for: {}", boundServiceNew);
86             return futures;
87         }
88
89         // Split based on type of interface....
90         if (L2vlan.class.equals(ifState.getType())) {
91             return bindServiceOnVlan(boundServiceNew, allServices, ifState, dataBroker);
92         } else if (Tunnel.class.equals(ifState.getType())) {
93             return bindServiceOnTunnel(boundServiceNew, allServices, ifState, dataBroker);
94         }
95         return futures;
96     }
97
98     private static List<ListenableFuture<Void>> bindServiceOnTunnel(BoundServices boundServiceNew, List<BoundServices> allServices,
99                                                                     org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface ifState, DataBroker dataBroker) {
100         // TODO - binding egress services on tunnels is not supported currently
101         return null;
102     }
103
104     private static List<ListenableFuture<Void>> bindServiceOnVlan(BoundServices boundServiceNew, List<BoundServices> allServices,
105                                                                   org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface ifState, DataBroker dataBroker) {
106         List<ListenableFuture<Void>> futures = new ArrayList<>();
107         BigInteger dpId = FlowBasedServicesUtils.getDpnIdFromInterface(ifState);
108         WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
109         Interface iface = InterfaceManagerCommonUtils.getInterfaceFromConfigDS(ifState.getName(), dataBroker);
110         LOG.info("binding egress service {} for vlan port: {}", boundServiceNew.getServiceName(), ifState.getName());
111         if (allServices.size() == 1) {
112             //calling LportDispatcherTableForService with current service index as 0 and next service index as
113             // some value since this is the only service bound.
114             FlowBasedServicesUtils.installEgressDispatcherFlows(dpId, boundServiceNew, ifState.getName(),
115                     transaction, ifState.getIfIndex(), NwConstants.DEFAULT_SERVICE_INDEX, (short) (boundServiceNew.getServicePriority() + 1), iface);
116             if (transaction != null) {
117                 futures.add(transaction.submit());
118             }
119             return futures;
120         }
121         allServices.remove(boundServiceNew);
122         BoundServices[] highLowPriorityService = FlowBasedServicesUtils.getHighAndLowPriorityService(allServices, boundServiceNew);
123         BoundServices low = highLowPriorityService[0];
124         BoundServices high = highLowPriorityService[1];
125         BoundServices highest = FlowBasedServicesUtils.getHighestPriorityService(allServices);
126         short currentServiceIndex = NwConstants.DEFAULT_SERVICE_INDEX;
127         short nextServiceIndex = ServiceIndex.getIndex(NwConstants.DEFAULT_EGRESS_SERVICE_NAME, NwConstants.DEFAULT_EGRESS_SERVICE_INDEX); // dummy service index
128         if (low != null) {
129             nextServiceIndex = low.getServicePriority();
130             if (low.equals(highest)) {
131                 //In this case the match criteria of existing service should be changed.
132                 BoundServices lower = FlowBasedServicesUtils.getHighAndLowPriorityService(allServices, low)[0];
133                 short lowerServiceIndex = (short) ((lower != null) ? lower.getServicePriority() : low.getServicePriority() + 1);
134                 LOG.trace("Installing egress dispatcher table entry for existing service {} service match on "
135                         + "service index {} update with service index {}",
136                     low, low.getServicePriority(), lowerServiceIndex);
137                 FlowBasedServicesUtils.installEgressDispatcherFlows(dpId, low, ifState.getName(), transaction, ifState.getIfIndex(),
138                         low.getServicePriority(), lowerServiceIndex, iface);
139             } else {
140                 currentServiceIndex = boundServiceNew.getServicePriority();
141             }
142         }
143         if (high != null) {
144             currentServiceIndex = boundServiceNew.getServicePriority();
145             if (high.equals(highest)) {
146                 LOG.trace("Installing egress dispatcher table entry for existing service {} service match on "
147                         + "service index {} update with service index {}",
148                     high, NwConstants.DEFAULT_SERVICE_INDEX, currentServiceIndex);
149                 FlowBasedServicesUtils.installEgressDispatcherFlows(dpId, high, ifState.getName(), transaction, ifState.getIfIndex(), NwConstants.DEFAULT_SERVICE_INDEX, currentServiceIndex, iface);
150             } else {
151                 LOG.trace("Installing egress dispatcher table entry for existing service {} service match on "
152                         + "service index {} update with service index {}",
153                     high, high.getServicePriority(), currentServiceIndex);
154                 FlowBasedServicesUtils.installEgressDispatcherFlows(dpId, high, ifState.getName(), transaction, ifState.getIfIndex(), high.getServicePriority(), currentServiceIndex, iface);
155             }
156         }
157         LOG.trace("Installing egress dispatcher table entry for new service match on service index {} update with service index {}",
158             currentServiceIndex, nextServiceIndex);
159         FlowBasedServicesUtils.installEgressDispatcherFlows(dpId, boundServiceNew, ifState.getName(), transaction, ifState.getIfIndex(), currentServiceIndex, nextServiceIndex, iface);
160         futures.add(transaction.submit());
161         return futures;
162     }
163 }