Use IetfYangUtils to canonize addresses
[netvirt.git] / elanmanager / impl / src / main / java / org / opendaylight / netvirt / elan / l2gw / listeners / HwvtepLocalUcastMacListener.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.util.Collections;
11 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
12 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
13 import org.opendaylight.genius.datastoreutils.hwvtep.HwvtepClusteredDataTreeChangeListener;
14 import org.opendaylight.genius.utils.batching.ResourceBatchingManager;
15 import org.opendaylight.genius.utils.hwvtep.HwvtepNodeHACache;
16 import org.opendaylight.genius.utils.hwvtep.HwvtepUtils;
17 import org.opendaylight.netvirt.elan.cache.ElanInstanceCache;
18 import org.opendaylight.netvirt.elan.l2gw.utils.ElanL2GatewayUtils;
19 import org.opendaylight.netvirt.elanmanager.utils.ElanL2GwCacheUtils;
20 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayDevice;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.IetfYangUtil;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalUcastMacs;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
26 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
27 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 /**
32  * A listener for Ucast MAC entries that are added/removed to/from an External
33  * Device (e.g., TOR).
34  *
35  * <p>When a Ucast MAC addr appears in the hwvtep's operational DS, that MAC must
36  * be populated in DMAC tables in all Elan participating DPNs. ELAN is selected
37  * according to field 'tunnel_key' of the Logical Switch to which the new MAC
38  * belongs.
39  */
40 public class HwvtepLocalUcastMacListener extends
41         HwvtepClusteredDataTreeChangeListener<LocalUcastMacs, HwvtepLocalUcastMacListener> {
42
43     private static final Logger LOG = LoggerFactory.getLogger(HwvtepLocalUcastMacListener.class);
44
45     private final DataBroker broker;
46     private final ElanL2GatewayUtils elanL2GatewayUtils;
47     private final ElanInstanceCache elanInstanceCache;
48
49     public HwvtepLocalUcastMacListener(DataBroker broker, ElanL2GatewayUtils elanL2GatewayUtils,
50             ElanInstanceCache elanInstanceCache, HwvtepNodeHACache hwvtepNodeHACache) {
51         super(LocalUcastMacs.class, HwvtepLocalUcastMacListener.class, hwvtepNodeHACache);
52
53         this.broker = broker;
54         this.elanL2GatewayUtils = elanL2GatewayUtils;
55         this.elanInstanceCache = elanInstanceCache;
56         ResourceBatchingManager.getInstance().registerDefaultBatchHandlers(this.broker);
57     }
58
59     public void init() {
60         registerListener(LogicalDatastoreType.OPERATIONAL, broker);
61     }
62
63     @Override
64     protected void removed(InstanceIdentifier<LocalUcastMacs> identifier, LocalUcastMacs macRemoved) {
65         String hwvtepNodeId = identifier.firstKeyOf(Node.class).getNodeId().getValue();
66         MacAddress macAddress = IetfYangUtil.INSTANCE.canonizeMacAddress(macRemoved.getMacEntryKey());
67
68         LOG.trace("LocalUcastMacs {} removed from {}", macAddress.getValue(), hwvtepNodeId);
69
70         String elanName = getElanName(macRemoved);
71
72         L2GatewayDevice elanL2GwDevice = ElanL2GwCacheUtils.getL2GatewayDeviceFromCache(elanName, hwvtepNodeId);
73         if (elanL2GwDevice == null) {
74             LOG.warn("Could not find L2GatewayDevice for ELAN: {}, nodeID:{} from cache", elanName, hwvtepNodeId);
75             return;
76         }
77
78         // Remove MAC from cache
79         elanL2GwDevice.removeUcastLocalMac(macRemoved);
80         elanL2GatewayUtils.unInstallL2GwUcastMacFromL2gwDevices(elanName, elanL2GwDevice,
81                 Collections.singletonList(macAddress));
82         elanL2GatewayUtils.unInstallL2GwUcastMacFromElanDpns(elanInstanceCache.get(elanName).orNull(),
83                 elanL2GwDevice, Collections.singletonList(macAddress));
84     }
85
86     protected String getElanName(LocalUcastMacs mac) {
87         return mac.getLogicalSwitchRef().getValue()
88                 .firstKeyOf(LogicalSwitches.class).getHwvtepNodeName().getValue();
89     }
90
91     @Override
92     protected void updated(InstanceIdentifier<LocalUcastMacs> identifier, LocalUcastMacs original,
93             LocalUcastMacs update) {
94         // TODO (eperefr) what can change here?
95
96     }
97
98     @Override
99     public void added(InstanceIdentifier<LocalUcastMacs> identifier, LocalUcastMacs macAdded) {
100         String hwvtepNodeId = identifier.firstKeyOf(Node.class).getNodeId().getValue();
101         String macAddress = IetfYangUtil.INSTANCE.canonizeMacAddress(macAdded.getMacEntryKey()).getValue();
102
103         LOG.trace("LocalUcastMacs {} added to {}", macAddress, hwvtepNodeId);
104
105         String elanName = getElanName(macAdded);
106         ElanInstance elan = elanInstanceCache.get(elanName).orNull();
107         if (elan == null) {
108             LOG.warn("Could not find ELAN for mac {} being added", macAddress);
109             return;
110         }
111
112         L2GatewayDevice elanL2GwDevice = ElanL2GwCacheUtils.getL2GatewayDeviceFromCache(elanName, hwvtepNodeId);
113         if (elanL2GwDevice == null) {
114             LOG.warn("Could not find L2GatewayDevice for ELAN: {}, nodeID:{} from cache", elanName, hwvtepNodeId);
115             return;
116         }
117
118         // Cache MAC for furthur processing later
119         elanL2GwDevice.addUcastLocalMac(macAdded);
120
121         elanL2GatewayUtils.installL2GwUcastMacInElan(elan, elanL2GwDevice, macAddress, macAdded, null);
122     }
123
124     @Override
125     protected InstanceIdentifier<LocalUcastMacs> getWildCardPath() {
126         return HwvtepUtils.getWildCardPathForLocalUcastMacs();
127     }
128
129     @Override
130     protected HwvtepLocalUcastMacListener getDataTreeChangeListener() {
131         return this;
132     }
133 }