15973c439d407facb9645ecb58b2a8373eac5be9
[netvirt.git] / elanmanager / impl / src / main / java / org / opendaylight / netvirt / elan / internal / ElanExtnTepListener.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.netvirt.elan.internal;
9
10 import com.google.common.collect.Lists;
11 import com.google.common.util.concurrent.SettableFuture;
12 import javax.annotation.PostConstruct;
13 import javax.inject.Inject;
14 import javax.inject.Singleton;
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
18 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
19 import org.opendaylight.netvirt.elan.cache.ElanInstanceCache;
20 import org.opendaylight.netvirt.elan.l2gw.utils.ElanL2GatewayMulticastUtils;
21 import org.opendaylight.netvirt.elan.utils.ElanConstants;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanInstances;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.elan.instance.ExternalTeps;
25 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 @Singleton
30 public class ElanExtnTepListener extends AsyncDataTreeChangeListenerBase<ExternalTeps, ElanExtnTepListener> {
31
32     private static final Logger LOG = LoggerFactory.getLogger(ElanExtnTepListener.class);
33
34     private final DataBroker broker;
35     private final ElanL2GatewayMulticastUtils elanL2GatewayMulticastUtils;
36     private final JobCoordinator jobCoordinator;
37     private final ElanInstanceCache elanInstanceCache;
38
39     @Inject
40     public ElanExtnTepListener(DataBroker dataBroker, ElanL2GatewayMulticastUtils elanL2GatewayMulticastUtils,
41             JobCoordinator jobCoordinator, ElanInstanceCache elanInstanceCache) {
42         super(ExternalTeps.class, ElanExtnTepListener.class);
43         this.broker = dataBroker;
44         this.elanL2GatewayMulticastUtils = elanL2GatewayMulticastUtils;
45         this.jobCoordinator = jobCoordinator;
46         this.elanInstanceCache = elanInstanceCache;
47     }
48
49     @Override
50     @PostConstruct
51     public void init() {
52         registerListener(LogicalDatastoreType.OPERATIONAL, broker);
53     }
54
55     @Override
56     public InstanceIdentifier<ExternalTeps> getWildCardPath() {
57         return InstanceIdentifier.create(ElanInstances.class).child(ElanInstance.class).child(ExternalTeps.class);
58     }
59
60     @Override
61     protected void add(InstanceIdentifier<ExternalTeps> instanceIdentifier, ExternalTeps tep) {
62         LOG.trace("ExternalTeps add received {}", instanceIdentifier);
63         updateElanRemoteBroadCastGroup(instanceIdentifier);
64     }
65
66     @Override
67     protected void update(InstanceIdentifier<ExternalTeps> instanceIdentifier, ExternalTeps tep, ExternalTeps t1) {
68     }
69
70     @Override
71     protected void remove(InstanceIdentifier<ExternalTeps> instanceIdentifier, ExternalTeps tep) {
72         LOG.trace("ExternalTeps remove received {}", instanceIdentifier);
73         updateElanRemoteBroadCastGroup(instanceIdentifier);
74     }
75
76     @SuppressWarnings("checkstyle:IllegalCatch")
77     private void updateElanRemoteBroadCastGroup(final InstanceIdentifier<ExternalTeps> iid) {
78         String elanName = iid.firstKeyOf(ElanInstance.class).getElanInstanceName();
79         ElanInstance elanInfo = elanInstanceCache.get(elanName).orNull();
80         if (elanInfo == null) {
81             return;
82         }
83
84         jobCoordinator.enqueueJob(elanName, () -> {
85             SettableFuture<Void> ft = SettableFuture.create();
86             try {
87                 //TODO make the following method return ft
88                 elanL2GatewayMulticastUtils.updateRemoteBroadcastGroupForAllElanDpns(elanInfo);
89                 ft.set(null);
90             } catch (Exception e) {
91                 //since the above method does a sync write , if it fails there was no retry
92                 //by setting the above mdsal exception in ft, and returning the ft makes sures that job is retried
93                 ft.setException(e);
94             }
95             return Lists.newArrayList(ft);
96         }, ElanConstants.JOB_MAX_RETRIES);
97     }
98
99     @Override
100     protected ElanExtnTepListener getDataTreeChangeListener() {
101         return this;
102     }
103 }