Vpnmanager and fibmanager changes
[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.interfaces.IMdsalApiManager;
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.InterfaceKey;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
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.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 import java.math.BigInteger;
31 import java.util.ArrayList;
32 import java.util.List;
33
34 public class FlowBasedServicesStateBindHelper {
35     private static final Logger LOG = LoggerFactory.getLogger(FlowBasedServicesStateBindHelper.class);
36
37     public static List<ListenableFuture<Void>> bindServicesOnInterface(Interface ifaceState,
38                                                              DataBroker dataBroker) {
39         List<ListenableFuture<Void>> futures = new ArrayList<>();
40         WriteTransaction t = dataBroker.newWriteOnlyTransaction();
41         ServicesInfo servicesInfo = FlowBasedServicesUtils.getServicesInfoForInterface(ifaceState.getName(), dataBroker);
42         if (servicesInfo == null) {
43             return futures;
44         }
45
46         List<BoundServices> allServices = servicesInfo.getBoundServices();
47         if (allServices == null || allServices.isEmpty()) {
48             return futures;
49         }
50
51         BoundServices highestPriorityBoundService = null;
52         short highestPriority = 0xFF;
53         for (BoundServices boundService : allServices) {
54             if (boundService.getServicePriority() < highestPriority) {
55                 highestPriorityBoundService = boundService;
56                 highestPriority = boundService.getServicePriority();
57             }
58         }
59
60         InterfaceKey interfaceKey = new InterfaceKey(ifaceState.getName());
61         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface iface =
62                     InterfaceManagerCommonUtils.getInterfaceFromConfigDS(interfaceKey, dataBroker);
63
64         NodeConnectorId nodeConnectorId = FlowBasedServicesUtils.getNodeConnectorIdFromInterface(iface, dataBroker);
65         long portNo = Long.parseLong(IfmUtil.getPortNoFromNodeConnectorId(nodeConnectorId));
66         BigInteger dpId = new BigInteger(IfmUtil.getDpnFromNodeConnectorId(nodeConnectorId));
67         Long lportTag = FlowBasedServicesUtils.getLPortTag(iface, dataBroker);
68         int vlanId = 0;
69         List<MatchInfo> matches = null;
70         if (iface.getType().isAssignableFrom(L2vlan.class)) {
71             IfL2vlan l2vlan = iface.getAugmentation(IfL2vlan.class);
72             if(l2vlan != null) {
73                 vlanId = l2vlan.getVlanId().getValue();
74             }
75             matches = FlowBasedServicesUtils.getMatchInfoForVlanPortAtIngressTable(dpId, portNo, vlanId);
76         } else if (iface.getType().isAssignableFrom(Tunnel.class)){
77             matches = FlowBasedServicesUtils.getMatchInfoForTunnelPortAtIngressTable (dpId, portNo, iface);
78         }
79
80         if (matches != null) {
81             FlowBasedServicesUtils.installInterfaceIngressFlow(dpId, iface.getName(), vlanId, highestPriorityBoundService,
82                     dataBroker, t, matches, lportTag.intValue(), IfmConstants.VLAN_INTERFACE_INGRESS_TABLE);
83         }
84
85         for (BoundServices boundService : allServices) {
86             if (!boundService.equals(highestPriorityBoundService)) {
87                 FlowBasedServicesUtils.installLPortDispatcherFlow(dpId, boundService, iface,
88                          dataBroker, t, lportTag.intValue());
89             }
90         }
91
92         futures.add(t.submit());
93         return futures;
94     }
95 }