Enhance the use of cache in HwvtepDeviceInfo
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / transactions / md / HwvtepMacEntriesRemoveCommand.java
1 /*
2  * Copyright (c) 2015, 2017 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
9 package org.opendaylight.ovsdb.hwvtepsouthbound.transactions.md;
10
11 import java.util.Collection;
12 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
13 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepConnectionInstance;
14 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepSouthboundConstants;
15 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepSouthboundMapper;
16 import org.opendaylight.ovsdb.lib.message.TableUpdates;
17 import org.opendaylight.ovsdb.lib.notation.UUID;
18 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
19 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
20 import org.opendaylight.ovsdb.schema.hardwarevtep.LogicalSwitch;
21 import org.opendaylight.ovsdb.schema.hardwarevtep.McastMacsLocal;
22 import org.opendaylight.ovsdb.schema.hardwarevtep.McastMacsRemote;
23 import org.opendaylight.ovsdb.schema.hardwarevtep.UcastMacsLocal;
24 import org.opendaylight.ovsdb.schema.hardwarevtep.UcastMacsRemote;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepLogicalSwitchRef;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalMcastMacs;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalMcastMacsKey;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalUcastMacs;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalUcastMacsKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteMcastMacs;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteMcastMacsKey;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteUcastMacs;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteUcastMacsKey;
37 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 public class HwvtepMacEntriesRemoveCommand extends AbstractTransactionCommand {
42
43     private static final Logger LOG = LoggerFactory.getLogger(HwvtepMacEntriesRemoveCommand.class);
44
45     public HwvtepMacEntriesRemoveCommand(HwvtepConnectionInstance key, TableUpdates updates, DatabaseSchema dbSchema) {
46         super(key, updates, dbSchema);
47     }
48
49
50     @Override
51     public void execute(ReadWriteTransaction transaction) {
52         removeUcastMacsLocal(transaction);
53         removeUcastMacsRemote(transaction);
54         removeMcastMacsLocal(transaction);
55         removeMcastMacsRemote(transaction);
56     }
57
58     private void removeUcastMacsLocal(ReadWriteTransaction transaction) {
59         Collection<UcastMacsLocal> deletedLUMRows =
60                 TyperUtils.extractRowsRemoved(UcastMacsLocal.class, getUpdates(), getDbSchema()).values();
61         for (UcastMacsLocal lum : deletedLUMRows) {
62             if (lum.getMac() != null && lum.getLogicalSwitchColumn() != null
63                     && lum.getLogicalSwitchColumn().getData() != null) {
64                 InstanceIdentifier<LocalUcastMacs> lumId = getOvsdbConnectionInstance().getInstanceIdentifier()
65                     .augmentation(HwvtepGlobalAugmentation.class).child(LocalUcastMacs.class,
66                                     new LocalUcastMacsKey(getLogicalSwitchRef(lum.getLogicalSwitchColumn().getData()),
67                                                     getMacAddress(lum.getMac())));
68                 addToDeleteTx(transaction, LocalUcastMacs.class, lumId, lum.getUuid());
69             } else {
70                 LOG.debug("Failed to delete UcastMacLocal entry {}", lum.getUuid());
71             }
72         }
73     }
74
75     private void removeUcastMacsRemote(ReadWriteTransaction transaction) {
76         Collection<UcastMacsRemote> deletedUMRRows =
77                 TyperUtils.extractRowsRemoved(UcastMacsRemote.class, getUpdates(), getDbSchema()).values();
78         for (UcastMacsRemote rum : deletedUMRRows) {
79             if (rum.getMac() != null && rum.getLogicalSwitchColumn() != null
80                     && rum.getLogicalSwitchColumn().getData() != null) {
81                 InstanceIdentifier<RemoteUcastMacs> rumId = getOvsdbConnectionInstance().getInstanceIdentifier()
82                     .augmentation(HwvtepGlobalAugmentation.class).child(RemoteUcastMacs.class,
83                                     new RemoteUcastMacsKey(getLogicalSwitchRef(rum.getLogicalSwitchColumn().getData()),
84                                                     getMacAddress(rum.getMac())));
85                 addToDeleteTx(transaction, RemoteUcastMacs.class, rumId, rum.getUuid());
86             } else {
87                 LOG.debug("Failed to delete UcastMacRemote entry {}", rum.getUuid());
88             }
89         }
90     }
91
92     private void removeMcastMacsLocal(ReadWriteTransaction transaction) {
93         Collection<McastMacsLocal> deletedLMMRows =
94                 TyperUtils.extractRowsRemoved(McastMacsLocal.class, getUpdates(), getDbSchema()).values();
95         for (McastMacsLocal lmm : deletedLMMRows) {
96             if (lmm.getMac() != null && lmm.getLogicalSwitchColumn() != null
97                     && lmm.getLogicalSwitchColumn().getData() != null) {
98                 InstanceIdentifier<LocalMcastMacs> lumId = getOvsdbConnectionInstance().getInstanceIdentifier()
99                     .augmentation(HwvtepGlobalAugmentation.class)
100                     .child(LocalMcastMacs.class,
101                                     new LocalMcastMacsKey(getLogicalSwitchRef(lmm.getLogicalSwitchColumn().getData()),
102                                                     getMacAddress(lmm.getMac())));
103                 addToDeleteTx(transaction, LocalMcastMacs.class, lumId, lmm.getUuid());
104             } else {
105                 LOG.debug("Failed to delete McastMacLocal entry {}", lmm.getUuid());
106             }
107         }
108     }
109
110     private void removeMcastMacsRemote(ReadWriteTransaction transaction) {
111         Collection<McastMacsRemote> deletedMMRRows =
112                 TyperUtils.extractRowsRemoved(McastMacsRemote.class, getUpdates(), getDbSchema()).values();
113         for (McastMacsRemote rmm : deletedMMRRows) {
114             if (rmm.getMac() != null && rmm.getLogicalSwitchColumn() != null
115                     && rmm.getLogicalSwitchColumn().getData() != null) {
116                 InstanceIdentifier<RemoteMcastMacs> lumId = getOvsdbConnectionInstance().getInstanceIdentifier()
117                     .augmentation(HwvtepGlobalAugmentation.class)
118                     .child(RemoteMcastMacs.class,
119                                     new RemoteMcastMacsKey(getLogicalSwitchRef(rmm.getLogicalSwitchColumn().getData()),
120                                                     getMacAddress(rmm.getMac())));
121                 addToDeleteTx(transaction, RemoteMcastMacs.class, lumId, rmm.getUuid());
122                 getOvsdbConnectionInstance().getDeviceInfo().clearDeviceOperData(RemoteMcastMacs.class, lumId);
123             } else {
124                 LOG.debug("Failed to delete McastMacRemote entry {}", rmm.getUuid());
125             }
126         }
127     }
128
129     private HwvtepLogicalSwitchRef getLogicalSwitchRef(UUID switchUUID) {
130         LogicalSwitch logicalSwitch = getOvsdbConnectionInstance().getDeviceInfo().getLogicalSwitch(switchUUID);
131         if (logicalSwitch != null) {
132             InstanceIdentifier<LogicalSwitches> switchIid =
133                     HwvtepSouthboundMapper.createInstanceIdentifier(getOvsdbConnectionInstance(), logicalSwitch);
134             return new HwvtepLogicalSwitchRef(switchIid);
135         }
136         LOG.debug("Failed to get LogicalSwitch {}", switchUUID);
137         LOG.trace("Available LogicalSwitches: {}",
138                         getOvsdbConnectionInstance().getDeviceInfo().getLogicalSwitches().values());
139         return null;
140     }
141
142     private MacAddress getMacAddress(String mac) {
143         if (mac.equals(HwvtepSouthboundConstants.UNKNOWN_DST_STRING)) {
144             return HwvtepSouthboundConstants.UNKNOWN_DST_MAC;
145         } else {
146             return new MacAddress(mac);
147         }
148     }
149 }