c7a78ddc121b3d1e79c5a261855faff63a4312be
[netvirt.git] / elanmanager / impl / src / main / java / org / opendaylight / netvirt / elan / l2gw / listeners / ElanGroupListener.java
1 /*
2  * Copyright (c) 2016 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.l2gw.listeners;
9
10 import static org.opendaylight.genius.infra.Datastore.CONFIGURATION;
11
12 import java.math.BigInteger;
13 import java.util.List;
14 import java.util.concurrent.ConcurrentMap;
15 import javax.annotation.Nullable;
16 import javax.inject.Inject;
17 import javax.inject.Singleton;
18 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
19 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
20 import org.opendaylight.genius.datastoreutils.AsyncClusteredDataTreeChangeListenerBase;
21 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
22 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
23 import org.opendaylight.infrautils.utils.concurrent.ListenableFutures;
24 import org.opendaylight.netvirt.elan.cache.ElanInstanceCache;
25 import org.opendaylight.netvirt.elan.l2gw.utils.ElanL2GatewayMulticastUtils;
26 import org.opendaylight.netvirt.elan.utils.ElanClusterUtils;
27 import org.opendaylight.netvirt.elan.utils.ElanUtils;
28 import org.opendaylight.netvirt.elanmanager.utils.ElanL2GwCacheUtils;
29 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayDevice;
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.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 @Singleton
42 public class ElanGroupListener extends AsyncClusteredDataTreeChangeListenerBase<Group, ElanGroupListener> {
43
44     private static final Logger LOG = LoggerFactory.getLogger(ElanGroupListener.class);
45     private final ManagedNewTransactionRunner txRunner;
46     private final ElanClusterUtils elanClusterUtils;
47     private final ElanUtils elanUtils;
48     private final ElanL2GatewayMulticastUtils elanL2GatewayMulticastUtils;
49     private final ElanInstanceCache elanInstanceCache;
50
51     @Inject
52     public ElanGroupListener(DataBroker db, ElanClusterUtils elanClusterUtils, ElanUtils elanUtils,
53             ElanL2GatewayMulticastUtils elanL2GatewayMulticastUtils, ElanInstanceCache elanInstanceCache) {
54         super(Group.class, ElanGroupListener.class);
55         this.txRunner = new ManagedNewTransactionRunnerImpl(db);
56         this.elanClusterUtils = elanClusterUtils;
57         this.elanUtils = elanUtils;
58         this.elanL2GatewayMulticastUtils = elanL2GatewayMulticastUtils;
59         this.elanInstanceCache = elanInstanceCache;
60         registerListener(LogicalDatastoreType.CONFIGURATION, db);
61         LOG.trace("ElanGroupListener registered");
62     }
63
64     @Override
65     protected InstanceIdentifier<Group> getWildCardPath() {
66         return InstanceIdentifier.create(Nodes.class).child(Node.class)
67                 .augmentation(FlowCapableNode.class).child(Group.class);
68     }
69
70     @Override
71     protected void remove(InstanceIdentifier<Group> identifier, Group del) {
72         LOG.trace("received group removed {}", del.key().getGroupId());
73     }
74
75
76     @Nullable
77     ElanInstance getElanInstanceFromGroupId(Group update) {
78         for (ElanInstance elanInstance : elanInstanceCache.getAllPresent()) {
79             if (elanInstance.getElanTag() != null) {
80                 long elanTag = elanInstance.getElanTag();
81                 long elanBCGroupId = ElanUtils.getElanRemoteBroadCastGroupID(elanTag);
82                 if (elanBCGroupId == update.getGroupId().getValue()) {
83                     return elanInstance;
84                 }
85             }
86         }
87         return null;
88     }
89
90     @Nullable
91     private BigInteger getDpnId(String node) {
92         //openflow:1]
93         String[] temp = node.split(":");
94         if (temp.length == 2) {
95             return new BigInteger(temp[1]);
96         }
97         return null;
98     }
99
100     @Override
101     protected void update(InstanceIdentifier<Group> identifier, @Nullable Group original, Group update) {
102         LOG.trace("received group updated {}", update.key().getGroupId());
103         final BigInteger dpnId = getDpnId(identifier.firstKeyOf(Node.class).getId().getValue());
104         if (dpnId == null) {
105             return;
106         }
107
108         List<L2GatewayDevice> allDevices = ElanL2GwCacheUtils.getAllElanDevicesFromCache();
109         if (allDevices == null || allDevices.isEmpty()) {
110             LOG.trace("no elan devices present in cache {}", update.key().getGroupId());
111             return;
112         }
113         int expectedElanFootprint = 0;
114         final ElanInstance elanInstance = getElanInstanceFromGroupId(update);
115         if (elanInstance == null) {
116             LOG.trace("no elan instance is null {}", update.key().getGroupId());
117             return;
118         }
119
120         ConcurrentMap<String, L2GatewayDevice> devices =
121                 ElanL2GwCacheUtils.getInvolvedL2GwDevices(elanInstance.getElanInstanceName());
122         if (devices == null || devices.isEmpty()) {
123             LOG.trace("no elan devices in elan cache {} {}", elanInstance.getElanInstanceName(),
124                     update.key().getGroupId());
125             return;
126         }
127         boolean updateGroup = false;
128         List<DpnInterfaces> dpns = elanUtils.getElanDPNByName(elanInstance.getElanInstanceName());
129         if (dpns.size() > 0) {
130             expectedElanFootprint += dpns.size();
131         } else {
132             updateGroup = true;
133         }
134         expectedElanFootprint += devices.size();
135         if (update.getBuckets() != null && update.getBuckets().getBucket() != null) {
136             if (update.getBuckets().getBucket().size() != expectedElanFootprint) {
137                 updateGroup = true;
138             } else {
139                 LOG.trace("no of buckets matched perfectly {} {}", elanInstance.getElanInstanceName(),
140                         update.key().getGroupId());
141             }
142         }
143         if (updateGroup) {
144             List<Bucket> bucketList = elanL2GatewayMulticastUtils.getRemoteBCGroupBuckets(elanInstance, null, dpnId, 0,
145                     elanInstance.getElanTag());
146             expectedElanFootprint--;//remove local bcgroup bucket
147             if (bucketList.size() != expectedElanFootprint) {
148                 //no point in retrying if not able to meet expected foot print
149                 return;
150             }
151             LOG.trace("no of buckets mismatched {} {}", elanInstance.getElanInstanceName(),
152                     update.key().getGroupId());
153             elanClusterUtils.runOnlyInOwnerNode(elanInstance.getElanInstanceName(), "updating broadcast group", () -> {
154                 ListenableFutures.addErrorLogging(txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION,
155                     confTx -> elanL2GatewayMulticastUtils.setupElanBroadcastGroups(elanInstance, dpnId, confTx)),
156                     LOG, "Error setting up ELAN BGs");
157                 return null;
158             });
159         } else {
160             LOG.trace("no buckets in the update {} {}", elanInstance.getElanInstanceName(),
161                     update.key().getGroupId());
162         }
163     }
164
165     @Override
166     protected void add(InstanceIdentifier<Group> identifier, Group added) {
167         LOG.trace("received group add {}", added.key().getGroupId());
168         update(identifier, null/*original*/, added);
169     }
170
171     @Override
172     protected ElanGroupListener getDataTreeChangeListener() {
173         return this;
174     }
175 }
176
177
178