2 * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
8 package org.opendaylight.netvirt.elan.l2gw.listeners;
10 import static org.opendaylight.mdsal.binding.util.Datastore.CONFIGURATION;
12 import java.util.List;
13 import javax.annotation.PreDestroy;
14 import javax.inject.Inject;
15 import javax.inject.Singleton;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.opendaylight.infrautils.utils.concurrent.Executors;
18 import org.opendaylight.infrautils.utils.concurrent.LoggingFutures;
19 import org.opendaylight.mdsal.binding.api.DataBroker;
20 import org.opendaylight.mdsal.binding.util.ManagedNewTransactionRunner;
21 import org.opendaylight.mdsal.binding.util.ManagedNewTransactionRunnerImpl;
22 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
23 import org.opendaylight.netvirt.elan.cache.ElanInstanceCache;
24 import org.opendaylight.netvirt.elan.l2gw.utils.ElanL2GatewayMulticastUtils;
25 import org.opendaylight.netvirt.elan.utils.ElanClusterUtils;
26 import org.opendaylight.netvirt.elan.utils.ElanUtils;
27 import org.opendaylight.netvirt.elanmanager.utils.ElanL2GwCacheUtils;
28 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayDevice;
29 import org.opendaylight.serviceutils.tools.listener.AbstractClusteredAsyncDataTreeChangeListener;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.buckets.Bucket;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.elan.dpn.interfaces.list.DpnInterfaces;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
37 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
38 import org.opendaylight.yangtools.yang.common.Uint64;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
43 public class ElanGroupListener extends AbstractClusteredAsyncDataTreeChangeListener<Group> {
45 private static final Logger LOG = LoggerFactory.getLogger(ElanGroupListener.class);
46 private final ManagedNewTransactionRunner txRunner;
47 private final ElanClusterUtils elanClusterUtils;
48 private final ElanUtils elanUtils;
49 private final ElanL2GatewayMulticastUtils elanL2GatewayMulticastUtils;
50 private final ElanInstanceCache elanInstanceCache;
53 public ElanGroupListener(DataBroker db, ElanClusterUtils elanClusterUtils, ElanUtils elanUtils,
54 ElanL2GatewayMulticastUtils elanL2GatewayMulticastUtils, ElanInstanceCache elanInstanceCache) {
55 super(db, LogicalDatastoreType.CONFIGURATION, InstanceIdentifier.create(Nodes.class).child(Node.class)
56 .augmentation(FlowCapableNode.class).child(Group.class),
57 Executors.newListeningSingleThreadExecutor("ElanGroupListener", LOG));
58 this.txRunner = new ManagedNewTransactionRunnerImpl(db);
59 this.elanClusterUtils = elanClusterUtils;
60 this.elanUtils = elanUtils;
61 this.elanL2GatewayMulticastUtils = elanL2GatewayMulticastUtils;
62 this.elanInstanceCache = elanInstanceCache;
63 LOG.trace("ElanGroupListener registered");
67 LOG.info("{} init", getClass().getSimpleName());
77 public void remove(InstanceIdentifier<Group> identifier, Group del) {
78 LOG.trace("received group removed {}", del.key().getGroupId());
83 ElanInstance getElanInstanceFromGroupId(Group update) {
84 for (ElanInstance elanInstance : elanInstanceCache.getAllPresent()) {
85 if (elanInstance.getElanTag() != null) {
86 long elanTag = elanInstance.getElanTag().longValue();
87 long elanBCGroupId = ElanUtils.getElanRemoteBroadCastGroupID(elanTag);
88 if (elanBCGroupId == update.getGroupId().getValue().longValue()) {
97 private static Uint64 getDpnId(String node) {
99 String[] temp = node.split(":");
100 if (temp.length == 2) {
101 return Uint64.valueOf(temp[1]);
107 public void update(InstanceIdentifier<Group> identifier, @Nullable Group original, Group update) {
108 LOG.trace("received group updated {}", update.key().getGroupId());
109 final Uint64 dpnId = getDpnId(identifier.firstKeyOf(Node.class).getId().getValue());
114 List<L2GatewayDevice> allDevices = ElanL2GwCacheUtils.getAllElanDevicesFromCache();
115 if (allDevices.isEmpty()) {
116 LOG.trace("no elan devices present in cache {}", update.key().getGroupId());
119 int expectedElanFootprint = 0;
120 final ElanInstance elanInstance = getElanInstanceFromGroupId(update);
121 if (elanInstance == null) {
122 LOG.trace("no elan instance is null {}", update.key().getGroupId());
126 final int devicesSize = ElanL2GwCacheUtils.getInvolvedL2GwDevices(elanInstance.getElanInstanceName()).size();
127 if (devicesSize == 0) {
128 LOG.trace("no elan devices in elan cache {} {}", elanInstance.getElanInstanceName(),
129 update.key().getGroupId());
132 boolean updateGroup = false;
133 List<DpnInterfaces> dpns = elanUtils.getElanDPNByName(elanInstance.getElanInstanceName());
134 if (dpns.size() > 0) {
135 expectedElanFootprint += dpns.size();
139 expectedElanFootprint += devicesSize;
140 if (update.getBuckets() != null && update.getBuckets().getBucket() != null) {
141 if (update.getBuckets().getBucket().size() != expectedElanFootprint) {
144 LOG.trace("no of buckets matched perfectly {} {}", elanInstance.getElanInstanceName(),
145 update.key().getGroupId());
149 List<Bucket> bucketList = elanL2GatewayMulticastUtils.getRemoteBCGroupBuckets(elanInstance, null, dpnId, 0,
150 elanInstance.getElanTag().toJava());
151 expectedElanFootprint--;//remove local bcgroup bucket
152 if (bucketList.size() != expectedElanFootprint) {
153 //no point in retrying if not able to meet expected foot print
156 LOG.trace("no of buckets mismatched {} {}", elanInstance.getElanInstanceName(),
157 update.key().getGroupId());
158 elanClusterUtils.runOnlyInOwnerNode(elanInstance.getElanInstanceName(), "updating broadcast group", () -> {
159 LoggingFutures.addErrorLogging(txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION,
160 confTx -> elanL2GatewayMulticastUtils.setupElanBroadcastGroups(elanInstance, dpnId, confTx)),
161 LOG, "Error setting up ELAN BGs");
165 LOG.trace("no buckets in the update {} {}", elanInstance.getElanInstanceName(),
166 update.key().getGroupId());
171 public void add(InstanceIdentifier<Group> identifier, Group added) {
172 LOG.trace("received group add {}", added.key().getGroupId());
173 update(identifier, null/*original*/, added);