Fix issues related to checkstyle enforcement for module
[genius.git] / interfacemanager / interfacemanager-impl / src / main / java / org / opendaylight / genius / interfacemanager / servicebindings / flowbased / config / helpers / FlowBasedEgressServicesConfigBindHelper.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 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.opendaylight.genius.interfacemanager.servicebinding.rev160406.ServiceModeBase;
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 public class FlowBasedEgressServicesConfigBindHelper implements FlowBasedServicesConfigAddable {
33     private static final Logger LOG = LoggerFactory.getLogger(FlowBasedEgressServicesConfigBindHelper.class);
34
35     private final InterfacemgrProvider interfaceMgrProvider;
36     private static volatile FlowBasedServicesConfigAddable flowBasedEgressServicesAddable;
37
38     private FlowBasedEgressServicesConfigBindHelper(InterfacemgrProvider interfaceMgrProvider) {
39         this.interfaceMgrProvider = interfaceMgrProvider;
40     }
41
42     public static void intitializeFlowBasedEgressServicesConfigAddHelper(InterfacemgrProvider interfaceMgrProvider) {
43         if (flowBasedEgressServicesAddable == null) {
44             synchronized (FlowBasedEgressServicesConfigBindHelper.class) {
45                 if (flowBasedEgressServicesAddable == null) {
46                     flowBasedEgressServicesAddable = new FlowBasedEgressServicesConfigBindHelper(interfaceMgrProvider);
47                 }
48             }
49         }
50     }
51
52     public static FlowBasedServicesConfigAddable getFlowBasedEgressServicesAddHelper() {
53         if (flowBasedEgressServicesAddable == null) {
54             LOG.error("OvsInterfaceConfigAdd Renderer is not initialized");
55         }
56         return flowBasedEgressServicesAddable;
57     }
58
59     @Override
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 = InstanceIdentifier.keyOf(instanceIdentifier.firstIdentifierOf(ServicesInfo.class))
65                 .getInterfaceName();
66         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
67             .ietf.interfaces.rev140508.interfaces.state.Interface ifState = InterfaceManagerCommonUtils
68                 .getInterfaceStateFromOperDS(interfaceName, dataBroker);
69         if (ifState == null) {
70             LOG.warn("Interface not operational, not binding Service for Interface: {}", interfaceName);
71             return futures;
72         }
73
74         Class<? extends ServiceModeBase> serviceMode = InstanceIdentifier
75                 .keyOf(instanceIdentifier.firstIdentifierOf(ServicesInfo.class)).getServiceMode();
76         // Get the Parent ServiceInfo
77         ServicesInfo servicesInfo = FlowBasedServicesUtils.getServicesInfoForInterface(interfaceName, serviceMode,
78                 dataBroker);
79         if (servicesInfo == null) {
80             LOG.error("Reached Impossible part 1 in the code during bind service for: {}", boundServiceNew);
81             return futures;
82         }
83
84         List<BoundServices> allServices = servicesInfo.getBoundServices();
85         if (allServices.isEmpty()) {
86             LOG.error("Reached Impossible part 2 in the code during bind service for: {}", boundServiceNew);
87             return futures;
88         }
89
90         if (L2vlan.class.equals(ifState.getType()) || Tunnel.class.equals(ifState.getType())) {
91             bindService(boundServiceNew, allServices, ifState, dataBroker);
92         }
93         return futures;
94     }
95
96     private static List<ListenableFuture<Void>> bindService(BoundServices boundServiceNew,
97             List<BoundServices> allServices,
98             org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
99                 .ietf.interfaces.rev140508.interfaces.state.Interface ifState,
100             DataBroker dataBroker) {
101         List<ListenableFuture<Void>> futures = new ArrayList<>();
102         BigInteger dpId = FlowBasedServicesUtils.getDpnIdFromInterface(ifState);
103         WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
104         Interface iface = InterfaceManagerCommonUtils.getInterfaceFromConfigDS(ifState.getName(), dataBroker);
105         LOG.info("binding egress service {} for interface: {}", boundServiceNew.getServiceName(), ifState.getName());
106         if (allServices.size() == 1) {
107             // calling LportDispatcherTableForService with current service index
108             // as 0 and next service index as
109             // some value since this is the only service bound.
110             FlowBasedServicesUtils.installEgressDispatcherFlows(dpId, boundServiceNew, ifState.getName(), transaction,
111                     ifState.getIfIndex(), NwConstants.DEFAULT_SERVICE_INDEX,
112                     (short) (boundServiceNew.getServicePriority() + 1), iface);
113             if (transaction != null) {
114                 futures.add(transaction.submit());
115             }
116             return futures;
117         }
118         allServices.remove(boundServiceNew);
119         BoundServices[] highLowPriorityService = FlowBasedServicesUtils.getHighAndLowPriorityService(allServices,
120                 boundServiceNew);
121         BoundServices low = highLowPriorityService[0];
122         BoundServices high = highLowPriorityService[1];
123         BoundServices highest = FlowBasedServicesUtils.getHighestPriorityService(allServices);
124         short currentServiceIndex = NwConstants.DEFAULT_SERVICE_INDEX;
125         short nextServiceIndex = ServiceIndex.getIndex(NwConstants.DEFAULT_EGRESS_SERVICE_NAME,
126                 NwConstants.DEFAULT_EGRESS_SERVICE_INDEX); // dummy service
127                                                             // 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
132                 // changed.
133                 BoundServices lower = FlowBasedServicesUtils.getHighAndLowPriorityService(allServices, low)[0];
134                 short lowerServiceIndex = (short) (lower != null ? lower.getServicePriority()
135                         : low.getServicePriority() + 1);
136                 LOG.trace(
137                         "Installing egress dispatcher table entry for existing service {} service match on "
138                                 + "service index {} update with service index {}",
139                         low, low.getServicePriority(), lowerServiceIndex);
140                 FlowBasedServicesUtils.installEgressDispatcherFlows(dpId, low, ifState.getName(), transaction,
141                         ifState.getIfIndex(), low.getServicePriority(), lowerServiceIndex, iface);
142             } else {
143                 currentServiceIndex = boundServiceNew.getServicePriority();
144             }
145         }
146         if (high != null) {
147             currentServiceIndex = boundServiceNew.getServicePriority();
148             if (high.equals(highest)) {
149                 LOG.trace(
150                         "Installing egress dispatcher table entry for existing service {} service match on "
151                                 + "service index {} update with service index {}",
152                         high, NwConstants.DEFAULT_SERVICE_INDEX, currentServiceIndex);
153                 FlowBasedServicesUtils.installEgressDispatcherFlows(dpId, high, ifState.getName(), transaction,
154                         ifState.getIfIndex(), NwConstants.DEFAULT_SERVICE_INDEX, currentServiceIndex, iface);
155             } else {
156                 LOG.trace(
157                         "Installing egress dispatcher table entry for existing service {} service match on "
158                                 + "service index {} update with service index {}",
159                         high, high.getServicePriority(), currentServiceIndex);
160                 FlowBasedServicesUtils.installEgressDispatcherFlows(dpId, high, ifState.getName(), transaction,
161                         ifState.getIfIndex(), high.getServicePriority(), currentServiceIndex, iface);
162             }
163         }
164         LOG.trace(
165                 "Installing egress dispatcher table entry "
166                 + "for new service match on service index {} update with service index {}",
167                 currentServiceIndex, nextServiceIndex);
168         FlowBasedServicesUtils.installEgressDispatcherFlows(dpId, boundServiceNew, ifState.getName(), transaction,
169                 ifState.getIfIndex(), currentServiceIndex, nextServiceIndex, iface);
170         futures.add(transaction.submit());
171         return futures;
172     }
173 }