Use plain @Nullable
[genius.git] / interfacemanager / interfacemanager-impl / src / main / java / org / opendaylight / genius / interfacemanager / servicebindings / flowbased / state / helpers / AbstractFlowBasedServicesStateBindHelper.java
1 /*
2  * Copyright (c) 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 static org.opendaylight.genius.infra.Datastore.CONFIGURATION;
11
12 import com.google.common.util.concurrent.ListenableFuture;
13 import java.math.BigInteger;
14 import java.util.List;
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.genius.infra.Datastore.Configuration;
17 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
18 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
19 import org.opendaylight.genius.infra.TypedReadWriteTransaction;
20 import org.opendaylight.genius.interfacemanager.servicebindings.flowbased.state.factory.FlowBasedServicesStateAddable;
21 import org.opendaylight.genius.interfacemanager.servicebindings.flowbased.utilities.FlowBasedServicesUtils;
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.state.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.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 /**
32  * AbstractFlowBasedServicesConfigBindHelper to enable flow based ingress/egress service binding for interfaces.
33  */
34 public abstract class AbstractFlowBasedServicesStateBindHelper implements FlowBasedServicesStateAddable {
35
36     private static final Logger LOG = LoggerFactory.getLogger(AbstractFlowBasedServicesStateBindHelper.class);
37
38     private final DataBroker dataBroker;
39     private final ManagedNewTransactionRunner txRunner;
40
41     /**
42      * Create instance.
43      * @param dataBroker instance of interfaceMgrProvider
44      */
45     protected AbstractFlowBasedServicesStateBindHelper(final DataBroker dataBroker) {
46         this.dataBroker = dataBroker;
47         this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
48     }
49
50     protected DataBroker getDataBroker() {
51         return dataBroker;
52     }
53
54     protected ManagedNewTransactionRunner getTxRunner() {
55         return txRunner;
56     }
57
58
59     @Override
60     public final void bindServices(List<ListenableFuture<Void>> futures, Interface ifaceState,
61                                    List<BoundServices> allServices, Class<? extends ServiceModeBase> serviceMode) {
62         futures.add(txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION, tx -> {
63             LOG.debug("binding services on interface {}", ifaceState.getName());
64             ServicesInfo servicesInfo = FlowBasedServicesUtils.getServicesInfoForInterface(tx, ifaceState.getName(),
65                     serviceMode);
66             if (servicesInfo == null) {
67                 LOG.trace("service info is null for interface {}", ifaceState.getName());
68                 return;
69             }
70             if (allServices == null || allServices.isEmpty()) {
71                 LOG.trace("bound services is empty for interface {}", ifaceState.getName());
72                 return;
73             }
74
75             if (L2vlan.class.equals(ifaceState.getType()) || Tunnel.class.equals(ifaceState.getType())) {
76                 bindServicesOnInterface(tx, allServices, ifaceState);
77             }
78         }));
79     }
80
81     protected abstract void bindServicesOnInterface(TypedReadWriteTransaction<Configuration> tx,
82                                                     List<BoundServices> allServices, Interface ifState);
83
84     @Override
85     public abstract void bindServicesOnInterfaceType(List<ListenableFuture<Void>> futures, BigInteger dpnId,
86                                                      String ifaceName);
87 }
88
89