Fixup Augmentable and Identifiable methods changing
[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 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;
34
35 @Singleton
36 public class ElanGroupListener extends AsyncClusteredDataTreeChangeListenerBase<Group, ElanGroupListener> {
37
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;
44
45     @Inject
46     public ElanGroupListener(DataBroker db, ElanClusterUtils elanClusterUtils, ElanUtils elanUtils,
47             ElanL2GatewayMulticastUtils elanL2GatewayMulticastUtils, ElanInstanceCache elanInstanceCache) {
48         super(Group.class, ElanGroupListener.class);
49         broker = db;
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");
56     }
57
58     @Override
59     protected InstanceIdentifier<Group> getWildCardPath() {
60         return InstanceIdentifier.create(Nodes.class).child(Node.class)
61                 .augmentation(FlowCapableNode.class).child(Group.class);
62     }
63
64     @Override
65     protected void remove(InstanceIdentifier<Group> identifier, Group del) {
66         LOG.trace("received group removed {}", del.key().getGroupId());
67     }
68
69
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()) {
76                     return elanInstance;
77                 }
78             }
79         }
80         return null;
81     }
82
83     private BigInteger getDpnId(String node) {
84         //openflow:1]
85         String[] temp = node.split(":");
86         if (temp.length == 2) {
87             return new BigInteger(temp[1]);
88         }
89         return null;
90     }
91
92     @Override
93     protected void update(InstanceIdentifier<Group> identifier, Group original, Group update) {
94         LOG.trace("received group updated {}", update.key().getGroupId());
95         final BigInteger dpnId = getDpnId(identifier.firstKeyOf(Node.class).getId().getValue());
96         if (dpnId == null) {
97             return;
98         }
99
100         List<L2GatewayDevice> allDevices = ElanL2GwCacheUtils.getAllElanDevicesFromCache();
101         if (allDevices == null || allDevices.isEmpty()) {
102             LOG.trace("no elan devices present in cache {}", update.key().getGroupId());
103             return;
104         }
105         int expectedElanFootprint = 0;
106         final ElanInstance elanInstance = getElanInstanceFromGroupId(update);
107         if (elanInstance == null) {
108             LOG.trace("no elan instance is null {}", update.key().getGroupId());
109             return;
110         }
111
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.key().getGroupId());
117             return;
118         }
119         boolean updateGroup = false;
120         List<DpnInterfaces> dpns = elanUtils.getElanDPNByName(elanInstance.getElanInstanceName());
121         if (dpns.size() > 0) {
122             expectedElanFootprint += dpns.size();
123         } else {
124             updateGroup = true;
125         }
126         expectedElanFootprint += devices.size();
127         if (update.getBuckets() != null && update.getBuckets().getBucket() != null) {
128             if (update.getBuckets().getBucket().size() != expectedElanFootprint) {
129                 updateGroup = true;
130             } else {
131                 LOG.trace("no of buckets matched perfectly {} {}", elanInstance.getElanInstanceName(),
132                         update.key().getGroupId());
133             }
134         }
135         if (updateGroup) {
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
141                 return;
142             }
143             LOG.trace("no of buckets mismatched {} {}", elanInstance.getElanInstanceName(),
144                     update.key().getGroupId());
145             elanClusterUtils.runOnlyInOwnerNode(elanInstance.getElanInstanceName(), "updating broadcast group", () -> {
146                 elanL2GatewayMulticastUtils.setupElanBroadcastGroups(elanInstance, dpnId);
147                 return null;
148             });
149         } else {
150             LOG.trace("no buckets in the update {} {}", elanInstance.getElanInstanceName(),
151                     update.key().getGroupId());
152         }
153     }
154
155     @Override
156     protected void add(InstanceIdentifier<Group> identifier, Group added) {
157         LOG.trace("received group add {}", added.key().getGroupId());
158         update(identifier, null/*original*/, added);
159     }
160
161     @Override
162     protected ElanGroupListener getDataTreeChangeListener() {
163         return this;
164     }
165 }
166
167
168