f2ef480a37bb4b1855ab04d73716ff0aeb8707de
[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.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.genius.infra.ManagedNewTransactionRunner;
18 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
19 import org.opendaylight.infrautils.utils.concurrent.Executors;
20 import org.opendaylight.infrautils.utils.concurrent.LoggingFutures;
21 import org.opendaylight.mdsal.binding.api.DataBroker;
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;
41
42 @Singleton
43 public class ElanGroupListener extends AbstractClusteredAsyncDataTreeChangeListener<Group> {
44
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;
51
52     @Inject
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");
64     }
65
66     public void init() {
67         LOG.info("{} init", getClass().getSimpleName());
68     }
69
70     @Override
71     @PreDestroy
72     public void close() {
73         super.close();
74     }
75
76     @Override
77     public void remove(InstanceIdentifier<Group> identifier, Group del) {
78         LOG.trace("received group removed {}", del.key().getGroupId());
79     }
80
81
82     @Nullable
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()) {
89                     return elanInstance;
90                 }
91             }
92         }
93         return null;
94     }
95
96     @Nullable
97     private static Uint64 getDpnId(String node) {
98         //openflow:1]
99         String[] temp = node.split(":");
100         if (temp.length == 2) {
101             return Uint64.valueOf(temp[1]);
102         }
103         return null;
104     }
105
106     @Override
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());
110         if (dpnId == null) {
111             return;
112         }
113
114         List<L2GatewayDevice> allDevices = ElanL2GwCacheUtils.getAllElanDevicesFromCache();
115         if (allDevices.isEmpty()) {
116             LOG.trace("no elan devices present in cache {}", update.key().getGroupId());
117             return;
118         }
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());
123             return;
124         }
125
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());
130             return;
131         }
132         boolean updateGroup = false;
133         List<DpnInterfaces> dpns = elanUtils.getElanDPNByName(elanInstance.getElanInstanceName());
134         if (dpns.size() > 0) {
135             expectedElanFootprint += dpns.size();
136         } else {
137             updateGroup = true;
138         }
139         expectedElanFootprint += devicesSize;
140         if (update.getBuckets() != null && update.getBuckets().getBucket() != null) {
141             if (update.getBuckets().getBucket().size() != expectedElanFootprint) {
142                 updateGroup = true;
143             } else {
144                 LOG.trace("no of buckets matched perfectly {} {}", elanInstance.getElanInstanceName(),
145                         update.key().getGroupId());
146             }
147         }
148         if (updateGroup) {
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
154                 return;
155             }
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");
162                 return null;
163             });
164         } else {
165             LOG.trace("no buckets in the update {} {}", elanInstance.getElanInstanceName(),
166                     update.key().getGroupId());
167         }
168     }
169
170     @Override
171     public void add(InstanceIdentifier<Group> identifier, Group added) {
172         LOG.trace("received group add {}", added.key().getGroupId());
173         update(identifier, null/*original*/, added);
174     }
175 }
176
177
178