Merge "Updated BgpManager for Be"
[vpnservice.git] / interfacemgr / interfacemgr-impl / src / main / java / org / opendaylight / vpnservice / interfacemgr / servicebindings / flowbased / statehelpers / FlowBasedServicesStateBindHelper.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.statehelpers;
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.vpnservice.mdsalutil.NwConstants;
19 import org.opendaylight.vpnservice.mdsalutil.interfaces.IMdsalApiManager;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.L2vlan;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.Tunnel;
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;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.servicebinding.rev151015.service.bindings.ServicesInfo;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.servicebinding.rev151015.service.bindings.services.info.BoundServices;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.IfL2vlan;
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
35 public class FlowBasedServicesStateBindHelper {
36     private static final Logger LOG = LoggerFactory.getLogger(FlowBasedServicesStateBindHelper.class);
37
38     public static List<ListenableFuture<Void>> bindServicesOnInterface(Interface ifaceState,
39                                                              DataBroker dataBroker) {
40         List<ListenableFuture<Void>> futures = new ArrayList<>();
41         WriteTransaction t = dataBroker.newWriteOnlyTransaction();
42         ServicesInfo servicesInfo = FlowBasedServicesUtils.getServicesInfoForInterface(ifaceState.getName(), dataBroker);
43         if (servicesInfo == null) {
44             return futures;
45         }
46
47         List<BoundServices> allServices = servicesInfo.getBoundServices();
48         if (allServices == null || allServices.isEmpty()) {
49             return futures;
50         }
51
52         BoundServices highestPriorityBoundService = null;
53         short highestPriority = 0xFF;
54         for (BoundServices boundService : allServices) {
55             if (boundService.getServicePriority() < highestPriority) {
56                 highestPriorityBoundService = boundService;
57                 highestPriority = boundService.getServicePriority();
58             }
59         }
60
61         InterfaceKey interfaceKey = new InterfaceKey(ifaceState.getName());
62         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface iface =
63                     InterfaceManagerCommonUtils.getInterfaceFromConfigDS(interfaceKey, dataBroker);
64
65         NodeConnectorId nodeConnectorId = FlowBasedServicesUtils.getNodeConnectorIdFromInterface(iface, dataBroker);
66         long portNo = Long.parseLong(IfmUtil.getPortNoFromNodeConnectorId(nodeConnectorId));
67         BigInteger dpId = new BigInteger(IfmUtil.getDpnFromNodeConnectorId(nodeConnectorId));
68         List<MatchInfo> matches = null;
69         if (iface.getType().isAssignableFrom(L2vlan.class)) {
70             matches = FlowBasedServicesUtils.getMatchInfoForVlanPortAtIngressTable(dpId, portNo, iface);
71         } else if (iface.getType().isAssignableFrom(Tunnel.class)){
72             matches = FlowBasedServicesUtils.getMatchInfoForTunnelPortAtIngressTable (dpId, portNo, iface);
73         }
74
75         if (matches != null) {
76             FlowBasedServicesUtils.installInterfaceIngressFlow(dpId, iface, highestPriorityBoundService,
77                     t, matches, ifaceState.getIfIndex(), NwConstants.VLAN_INTERFACE_INGRESS_TABLE);
78         }
79
80         for (BoundServices boundService : allServices) {
81             if (!boundService.equals(highestPriorityBoundService)) {
82                 FlowBasedServicesUtils.installLPortDispatcherFlow(dpId, boundService, iface, t, ifaceState.getIfIndex());
83             }
84         }
85
86         futures.add(t.submit());
87         return futures;
88     }
89 }