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 java.math.BigInteger;
11 import java.util.List;
12 import java.util.concurrent.ConcurrentMap;
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.AsyncClusteredDataTreeChangeListenerBase;
18 import org.opendaylight.netvirt.elan.cache.ElanInstanceCache;
19 import org.opendaylight.netvirt.elan.l2gw.utils.ElanL2GatewayMulticastUtils;
20 import org.opendaylight.netvirt.elan.utils.ElanClusterUtils;
21 import org.opendaylight.netvirt.elan.utils.ElanUtils;
22 import org.opendaylight.netvirt.elanmanager.utils.ElanL2GwCacheUtils;
23 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayDevice;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.buckets.Bucket;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.elan.dpn.interfaces.list.DpnInterfaces;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 public class ElanGroupListener extends AsyncClusteredDataTreeChangeListenerBase<Group, ElanGroupListener> {
38 private static final Logger LOG = LoggerFactory.getLogger(ElanGroupListener.class);
39 private final DataBroker broker;
40 private final ElanClusterUtils elanClusterUtils;
41 private final ElanUtils elanUtils;
42 private final ElanL2GatewayMulticastUtils elanL2GatewayMulticastUtils;
43 private final ElanInstanceCache elanInstanceCache;
46 public ElanGroupListener(DataBroker db, ElanClusterUtils elanClusterUtils, ElanUtils elanUtils,
47 ElanL2GatewayMulticastUtils elanL2GatewayMulticastUtils, ElanInstanceCache elanInstanceCache) {
48 super(Group.class, ElanGroupListener.class);
50 this.elanClusterUtils = elanClusterUtils;
51 this.elanUtils = elanUtils;
52 this.elanL2GatewayMulticastUtils = elanL2GatewayMulticastUtils;
53 this.elanInstanceCache = elanInstanceCache;
54 registerListener(LogicalDatastoreType.CONFIGURATION, broker);
55 LOG.trace("ElanGroupListener registered");
59 protected InstanceIdentifier<Group> getWildCardPath() {
60 return InstanceIdentifier.create(Nodes.class).child(Node.class)
61 .augmentation(FlowCapableNode.class).child(Group.class);
65 protected void remove(InstanceIdentifier<Group> identifier, Group del) {
66 LOG.trace("received group removed {}", del.getKey().getGroupId());
70 ElanInstance getElanInstanceFromGroupId(Group update) {
71 for (ElanInstance elanInstance : elanInstanceCache.getAllPresent()) {
72 if (elanInstance.getElanTag() != null) {
73 long elanTag = elanInstance.getElanTag();
74 long elanBCGroupId = ElanUtils.getElanRemoteBroadCastGroupID(elanTag);
75 if (elanBCGroupId == update.getGroupId().getValue()) {
83 private BigInteger getDpnId(String node) {
85 String[] temp = node.split(":");
86 if (temp.length == 2) {
87 return new BigInteger(temp[1]);
93 protected void update(InstanceIdentifier<Group> identifier, Group original, Group update) {
94 LOG.trace("received group updated {}", update.getKey().getGroupId());
95 final BigInteger dpnId = getDpnId(identifier.firstKeyOf(Node.class).getId().getValue());
100 List<L2GatewayDevice> allDevices = ElanL2GwCacheUtils.getAllElanDevicesFromCache();
101 if (allDevices == null || allDevices.isEmpty()) {
102 LOG.trace("no elan devices present in cache {}", update.getKey().getGroupId());
105 int expectedElanFootprint = 0;
106 final ElanInstance elanInstance = getElanInstanceFromGroupId(update);
107 if (elanInstance == null) {
108 LOG.trace("no elan instance is null {}", update.getKey().getGroupId());
112 ConcurrentMap<String, L2GatewayDevice> devices =
113 ElanL2GwCacheUtils.getInvolvedL2GwDevices(elanInstance.getElanInstanceName());
114 if (devices == null || devices.isEmpty()) {
115 LOG.trace("no elan devices in elan cache {} {}", elanInstance.getElanInstanceName(),
116 update.getKey().getGroupId());
119 boolean updateGroup = false;
120 List<DpnInterfaces> dpns = elanUtils.getElanDPNByName(elanInstance.getElanInstanceName());
121 if (dpns.size() > 0) {
122 expectedElanFootprint += dpns.size();
126 expectedElanFootprint += devices.size();
127 if (update.getBuckets() != null && update.getBuckets().getBucket() != null) {
128 if (update.getBuckets().getBucket().size() != expectedElanFootprint) {
131 LOG.trace("no of buckets matched perfectly {} {}", elanInstance.getElanInstanceName(),
132 update.getKey().getGroupId());
136 List<Bucket> bucketList = elanL2GatewayMulticastUtils.getRemoteBCGroupBuckets(elanInstance, null, dpnId, 0,
137 elanInstance.getElanTag());
138 expectedElanFootprint--;//remove local bcgroup bucket
139 if (bucketList.size() != expectedElanFootprint) {
140 //no point in retrying if not able to meet expected foot print
143 LOG.trace("no of buckets mismatched {} {}", elanInstance.getElanInstanceName(),
144 update.getKey().getGroupId());
145 elanClusterUtils.runOnlyInOwnerNode(elanInstance.getElanInstanceName(), "updating broadcast group", () -> {
146 elanL2GatewayMulticastUtils.setupElanBroadcastGroups(elanInstance, dpnId);
150 LOG.trace("no buckets in the update {} {}", elanInstance.getElanInstanceName(),
151 update.getKey().getGroupId());
156 protected void add(InstanceIdentifier<Group> identifier, Group added) {
157 LOG.trace("received group add {}", added.getKey().getGroupId());
158 update(identifier, null/*original*/, added);
162 protected ElanGroupListener getDataTreeChangeListener() {