Fixing NPE found out during bindService Testing
[vpnservice.git] / interfacemgr / interfacemgr-impl / src / main / java / org / opendaylight / vpnservice / interfacemgr / servicebindings / flowbased / confighelpers / FlowBasedServicesConfigBindHelper.java
1 /*
2  * Copyright (c) 2015 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.vpnservice.interfacemgr.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.vpnservice.interfacemgr.IfmConstants;
14 import org.opendaylight.vpnservice.interfacemgr.IfmUtil;
15 import org.opendaylight.vpnservice.interfacemgr.commons.InterfaceManagerCommonUtils;
16 import org.opendaylight.vpnservice.interfacemgr.servicebindings.flowbased.utilities.FlowBasedServicesUtils;
17 import org.opendaylight.vpnservice.mdsalutil.MatchInfo;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.L2vlan;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.Tunnel;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.servicebinding.rev151015.service.bindings.ServicesInfo;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.servicebinding.rev151015.service.bindings.services.info.BoundServices;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.IfL2vlan;
27 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 import java.math.BigInteger;
32 import java.util.ArrayList;
33 import java.util.List;
34 import java.util.Map;
35 import java.util.concurrent.ConcurrentHashMap;
36
37 public class FlowBasedServicesConfigBindHelper {
38     private static final Logger LOG = LoggerFactory.getLogger(FlowBasedServicesConfigBindHelper.class);
39
40     public static List<ListenableFuture<Void>> bindService(InstanceIdentifier<BoundServices> instanceIdentifier,
41                                                            BoundServices boundServiceNew, DataBroker dataBroker) {
42         List<ListenableFuture<Void>> futures = new ArrayList<>();
43         WriteTransaction t = dataBroker.newWriteOnlyTransaction();
44         String interfaceName =
45                 InstanceIdentifier.keyOf(instanceIdentifier.firstIdentifierOf(ServicesInfo.class)).getInterfaceName();
46
47         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface ifState =
48                 InterfaceManagerCommonUtils.getInterfaceStateFromOperDS(interfaceName, dataBroker);
49         if (ifState == null || ifState.getOperStatus() == OperStatus.Down) {
50             LOG.info("Not Binding Service since for Interface: {}", interfaceName);
51             return futures;
52         }
53
54         // Get the Parent ServiceInfo
55         ServicesInfo servicesInfo = FlowBasedServicesUtils.getServicesInfoForInterface(interfaceName, dataBroker);
56         if (servicesInfo == null) {
57             LOG.error("Reached Impossible part 1 in the code during bind service for: {}", boundServiceNew);
58             return futures;
59         }
60
61         InterfaceKey interfaceKey = new InterfaceKey(interfaceName);
62         Interface iface = InterfaceManagerCommonUtils.getInterfaceFromConfigDS(interfaceKey, dataBroker);
63
64         List<BoundServices> allServices = servicesInfo.getBoundServices();
65         if (allServices.isEmpty()) {
66             LOG.error("Reached Impossible part 2 in the code during bind service for: {}", boundServiceNew);
67             return futures;
68         }
69
70         NodeConnectorId nodeConnectorId = FlowBasedServicesUtils.getNodeConnectorIdFromInterface(iface, dataBroker);
71         long portNo = Long.parseLong(IfmUtil.getPortNoFromNodeConnectorId(nodeConnectorId));
72         BigInteger dpId = new BigInteger(IfmUtil.getDpnFromNodeConnectorId(nodeConnectorId));
73
74         Long lportTag = FlowBasedServicesUtils.getLPortTag(iface, dataBroker);
75         if (allServices.size() == 1) {
76             // If only one service present, install instructions in table 0.
77             int vlanId = 0;
78             List<MatchInfo> matches = null;
79             if (iface.getType().isAssignableFrom(L2vlan.class)) {
80                 IfL2vlan l2vlan = iface.getAugmentation(IfL2vlan.class);
81                 if( l2vlan != null){
82                     vlanId = l2vlan.getVlanId().getValue();
83                 }
84                 matches = FlowBasedServicesUtils.getMatchInfoForVlanPortAtIngressTable(dpId, portNo, vlanId);
85             } else if (iface.getType().isAssignableFrom(Tunnel.class)){
86                 matches = FlowBasedServicesUtils.getMatchInfoForTunnelPortAtIngressTable (dpId, portNo, iface);
87             }
88
89             if (matches != null) {
90                 FlowBasedServicesUtils.installInterfaceIngressFlow(dpId, vlanId, boundServiceNew,
91                         dataBroker, t, matches, lportTag.intValue(), IfmConstants.VLAN_INTERFACE_INGRESS_TABLE);
92             }
93
94             if (t != null) {
95                 futures.add(t.submit());
96             }
97             return futures;
98         }
99
100         boolean isCurrentServiceHighestPriority = true;
101         Map<Short, BoundServices> tmpServicesMap = new ConcurrentHashMap<>();
102         short highestPriority = 0xFF;
103         for (BoundServices boundService : allServices) {
104             if (boundService.getServicePriority() < boundServiceNew.getServicePriority()) {
105                 isCurrentServiceHighestPriority = false;
106                 break;
107             }
108             if (!boundService.equals(boundServiceNew)) {
109                 tmpServicesMap.put(boundService.getServicePriority(), boundService);
110                 if (boundService.getServicePriority() < highestPriority) {
111                     highestPriority = boundService.getServicePriority();
112                 }
113             }
114             LOG.error("Reached unexpected part 1 of the code when handling bind service for interface: {}, when binding" +
115                     "service: {}", iface, boundServiceNew);
116         }
117
118         if (!isCurrentServiceHighestPriority) {
119             FlowBasedServicesUtils.installLPortDispatcherFlow(dpId, boundServiceNew, iface, dataBroker,  t,
120                     lportTag.intValue());
121         } else {
122             BoundServices serviceToReplace = tmpServicesMap.get(highestPriority);
123             FlowBasedServicesUtils.installLPortDispatcherFlow(dpId, serviceToReplace, iface, dataBroker, t,
124                     lportTag.intValue());
125             int vlanId = 0;
126             List<MatchInfo> matches = null;
127             if (iface.getType().isAssignableFrom(L2vlan.class)) {
128                 vlanId = iface.getAugmentation(IfL2vlan.class).getVlanId().getValue();
129                 matches = FlowBasedServicesUtils.getMatchInfoForVlanPortAtIngressTable(dpId, portNo, vlanId);
130             } else if (iface.getType().isAssignableFrom(Tunnel.class)){
131                 matches = FlowBasedServicesUtils.getMatchInfoForTunnelPortAtIngressTable (dpId, portNo, iface);
132             }
133
134             if (matches != null) {
135                 FlowBasedServicesUtils.removeIngressFlow(iface, serviceToReplace, dpId, dataBroker, t);
136                 FlowBasedServicesUtils.installInterfaceIngressFlow(dpId, vlanId, boundServiceNew, dataBroker, t,
137                         matches, lportTag.intValue(), IfmConstants.VLAN_INTERFACE_INGRESS_TABLE);
138             }
139         }
140
141         if (t != null) {
142             futures.add(t.submit());
143         }
144         return futures;
145     }
146 }