263e865669219adc4d02062ddc05c8fb71b2f276
[genius.git] / interfacemanager / interfacemanager-impl / src / main / java / org / opendaylight / genius / interfacemanager / servicebindings / flowbased / state / helpers / FlowBasedIngressServicesStateBindHelper.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.state.helpers;
9
10 import com.google.common.util.concurrent.ListenableFuture;
11 import java.util.Comparator;
12 import java.util.List;
13 import javax.inject.Inject;
14 import javax.inject.Singleton;
15 import org.apache.aries.blueprint.annotation.service.Reference;
16 import org.opendaylight.genius.infra.Datastore.Configuration;
17 import org.opendaylight.genius.infra.TypedReadWriteTransaction;
18 import org.opendaylight.genius.interfacemanager.IfmUtil;
19 import org.opendaylight.genius.interfacemanager.commons.InterfaceManagerCommonUtils;
20 import org.opendaylight.genius.interfacemanager.servicebindings.flowbased.utilities.FlowBasedServicesUtils;
21 import org.opendaylight.genius.mdsalutil.MatchInfo;
22 import org.opendaylight.genius.mdsalutil.NwConstants;
23 import org.opendaylight.mdsal.binding.api.DataBroker;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev170119.L2vlan;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev170119.Tunnel;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.services.info.BoundServices;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
29 import org.opendaylight.yangtools.yang.common.Uint64;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 @Singleton
34 public class FlowBasedIngressServicesStateBindHelper extends AbstractFlowBasedServicesStateBindHelper {
35
36     private static final Logger LOG = LoggerFactory.getLogger(FlowBasedIngressServicesStateBindHelper.class);
37
38     private final InterfaceManagerCommonUtils interfaceManagerCommonUtils;
39
40     @Inject
41     public FlowBasedIngressServicesStateBindHelper(@Reference final DataBroker dataBroker,
42             final InterfaceManagerCommonUtils interfaceManagerCommonUtils) {
43         super(dataBroker);
44         this.interfaceManagerCommonUtils = interfaceManagerCommonUtils;
45     }
46
47     @Override
48     public void bindServicesOnInterface(TypedReadWriteTransaction<Configuration> tx, List<BoundServices> allServices,
49             Interface ifaceState) {
50         LOG.debug("binding services on interface {}", ifaceState.getName());
51         if (L2vlan.class.equals(ifaceState.getType())) {
52             bindServiceOnVlan(tx, allServices, ifaceState);
53         } else if (Tunnel.class.equals(ifaceState.getType())) {
54             bindServiceOnTunnel(tx, allServices, ifaceState);
55         }
56     }
57
58     private void bindServiceOnTunnel(TypedReadWriteTransaction<Configuration> tx, List<BoundServices> allServices,
59             Interface ifState) {
60         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
61                 .ietf.interfaces.rev140508.interfaces.Interface iface = interfaceManagerCommonUtils
62                 .getInterfaceFromConfigDS(ifState.getName());
63         NodeConnectorId nodeConnectorId = FlowBasedServicesUtils.getNodeConnectorIdFromInterface(ifState);
64         long portNo = IfmUtil.getPortNumberFromNodeConnectorId(nodeConnectorId);
65         Uint64 dpId = IfmUtil.getDpnFromNodeConnectorId(nodeConnectorId);
66         List<MatchInfo> matches = FlowBasedServicesUtils.getMatchInfoForTunnelPortAtIngressTable(dpId, portNo);
67         BoundServices highestPriorityBoundService = FlowBasedServicesUtils.getHighestPriorityService(allServices);
68         FlowBasedServicesUtils.installInterfaceIngressFlow(dpId, iface, highestPriorityBoundService,
69                 tx, matches, ifState.getIfIndex(), NwConstants.VLAN_INTERFACE_INGRESS_TABLE);
70
71         for (BoundServices boundService : allServices) {
72             if (!boundService.equals(highestPriorityBoundService)) {
73                 FlowBasedServicesUtils.installLPortDispatcherFlow(dpId, boundService, ifState.getName(),
74                         tx, ifState.getIfIndex(), boundService.getServicePriority().toJava(),
75                         (short) (boundService.getServicePriority().toJava() + 1));
76             }
77         }
78     }
79
80     private void bindServiceOnVlan(TypedReadWriteTransaction<Configuration> tx, List<BoundServices> allServices,
81             Interface ifState) {
82         LOG.info("bind all ingress services for vlan port: {}", ifState.getName());
83         NodeConnectorId nodeConnectorId = FlowBasedServicesUtils.getNodeConnectorIdFromInterface(ifState);
84         Uint64 dpId = IfmUtil.getDpnFromNodeConnectorId(nodeConnectorId);
85         allServices.sort(Comparator.comparing(BoundServices::getServicePriority));
86         BoundServices highestPriority = allServices.remove(0);
87         short nextServiceIndex = (short) (allServices.size() > 0 ? allServices.get(0).getServicePriority().toJava()
88                 : highestPriority.getServicePriority().toJava() + 1);
89         FlowBasedServicesUtils.installLPortDispatcherFlow(dpId, highestPriority, ifState.getName(), tx,
90                 ifState.getIfIndex(), NwConstants.DEFAULT_SERVICE_INDEX, nextServiceIndex);
91         BoundServices prev = null;
92         for (BoundServices boundService : allServices) {
93             if (prev != null) {
94                 FlowBasedServicesUtils.installLPortDispatcherFlow(dpId, prev, ifState.getName(), tx,
95                         ifState.getIfIndex(), prev.getServicePriority().toJava(),
96                         boundService.getServicePriority().toJava());
97             }
98             prev = boundService;
99         }
100         if (prev != null) {
101             FlowBasedServicesUtils.installLPortDispatcherFlow(dpId, prev, ifState.getName(), tx,
102                     ifState.getIfIndex(), prev.getServicePriority().toJava(),
103                     (short) (prev.getServicePriority().toJava() + 1));
104         }
105     }
106
107     @Override
108     public void bindServicesOnInterfaceType(List<ListenableFuture<Void>> futures, Uint64 dpnId, String ifaceName) {
109         // FIXME: does this mean this is not finished?
110         LOG.info("bindServicesOnInterfaceType Ingress - WIP");
111     }
112 }