Make methods static
[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.mdsal.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.ovsdb.utils.mdsal.utils.TransactionType;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepLogicalSwitchRef;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalMcastMacs;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalMcastMacsKey;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalUcastMacs;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalUcastMacsKey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteMcastMacs;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteMcastMacsKey;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteUcastMacs;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteUcastMacsKey;
38 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 public class HwvtepMacEntriesRemoveCommand extends AbstractTransactionCommand {
44
45     private static final Logger LOG = LoggerFactory.getLogger(HwvtepMacEntriesRemoveCommand.class);
46
47     public HwvtepMacEntriesRemoveCommand(HwvtepConnectionInstance key, TableUpdates updates, DatabaseSchema dbSchema) {
48         super(key, updates, dbSchema);
49     }
50
51
52     @Override
53     public void execute(ReadWriteTransaction transaction) {
54         removeUcastMacsLocal(transaction);
55         removeUcastMacsRemote(transaction);
56         removeMcastMacsLocal(transaction);
57         removeMcastMacsRemote(transaction);
58     }
59
60     private void removeUcastMacsLocal(ReadWriteTransaction transaction) {
61         Collection<UcastMacsLocal> deletedLUMRows =
62                 TyperUtils.extractRowsRemoved(UcastMacsLocal.class, getUpdates(), getDbSchema()).values();
63         for (UcastMacsLocal lum : deletedLUMRows) {
64             if (lum.getMac() != null && lum.getLogicalSwitchColumn() != null
65                     && lum.getLogicalSwitchColumn().getData() != null) {
66                 LOG.info("DEVICE - {} LocalUcastMacs for Node {} - {}", TransactionType.DELETE,
67                     getOvsdbConnectionInstance().getInstanceIdentifier().firstKeyOf(Node.class)
68                         .getNodeId().getValue(), lum.getMac());
69                 InstanceIdentifier<LocalUcastMacs> lumId = getOvsdbConnectionInstance().getInstanceIdentifier()
70                     .augmentation(HwvtepGlobalAugmentation.class).child(LocalUcastMacs.class,
71                                     new LocalUcastMacsKey(getLogicalSwitchRef(lum.getLogicalSwitchColumn().getData()),
72                                                     getMacAddress(lum.getMac())));
73                 addToDeleteTx(transaction, LocalUcastMacs.class, lumId, lum.getUuid());
74             } else {
75                 LOG.debug("Failed to delete UcastMacLocal entry {}", lum.getUuid());
76             }
77         }
78     }
79
80     private void removeUcastMacsRemote(ReadWriteTransaction transaction) {
81         Collection<UcastMacsRemote> deletedUMRRows =
82                 TyperUtils.extractRowsRemoved(UcastMacsRemote.class, getUpdates(), getDbSchema()).values();
83         for (UcastMacsRemote rum : deletedUMRRows) {
84             if (rum.getMac() != null && rum.getLogicalSwitchColumn() != null
85                     && rum.getLogicalSwitchColumn().getData() != null) {
86                 InstanceIdentifier<RemoteUcastMacs> rumId = getOvsdbConnectionInstance().getInstanceIdentifier()
87                     .augmentation(HwvtepGlobalAugmentation.class).child(RemoteUcastMacs.class,
88                                     new RemoteUcastMacsKey(getLogicalSwitchRef(rum.getLogicalSwitchColumn().getData()),
89                                                     getMacAddress(rum.getMac())));
90                 addToDeleteTx(transaction, RemoteUcastMacs.class, rumId, rum.getUuid());
91             } else {
92                 LOG.debug("Failed to delete UcastMacRemote entry {}", rum.getUuid());
93             }
94         }
95     }
96
97     private void removeMcastMacsLocal(ReadWriteTransaction transaction) {
98         Collection<McastMacsLocal> deletedLMMRows =
99                 TyperUtils.extractRowsRemoved(McastMacsLocal.class, getUpdates(), getDbSchema()).values();
100         for (McastMacsLocal lmm : deletedLMMRows) {
101             if (lmm.getMac() != null && lmm.getLogicalSwitchColumn() != null
102                     && lmm.getLogicalSwitchColumn().getData() != null) {
103                 InstanceIdentifier<LocalMcastMacs> lumId = getOvsdbConnectionInstance().getInstanceIdentifier()
104                     .augmentation(HwvtepGlobalAugmentation.class)
105                     .child(LocalMcastMacs.class,
106                                     new LocalMcastMacsKey(getLogicalSwitchRef(lmm.getLogicalSwitchColumn().getData()),
107                                                     getMacAddress(lmm.getMac())));
108                 addToDeleteTx(transaction, LocalMcastMacs.class, lumId, lmm.getUuid());
109             } else {
110                 LOG.debug("Failed to delete McastMacLocal entry {}", lmm.getUuid());
111             }
112         }
113     }
114
115     private void removeMcastMacsRemote(ReadWriteTransaction transaction) {
116         Collection<McastMacsRemote> deletedMMRRows =
117                 TyperUtils.extractRowsRemoved(McastMacsRemote.class, getUpdates(), getDbSchema()).values();
118         for (McastMacsRemote rmm : deletedMMRRows) {
119             if (rmm.getMac() != null && rmm.getLogicalSwitchColumn() != null
120                     && rmm.getLogicalSwitchColumn().getData() != null) {
121                 InstanceIdentifier<RemoteMcastMacs> lumId = getOvsdbConnectionInstance().getInstanceIdentifier()
122                     .augmentation(HwvtepGlobalAugmentation.class)
123                     .child(RemoteMcastMacs.class,
124                                     new RemoteMcastMacsKey(getLogicalSwitchRef(rmm.getLogicalSwitchColumn().getData()),
125                                                     getMacAddress(rmm.getMac())));
126                 addToDeleteTx(transaction, RemoteMcastMacs.class, lumId, rmm.getUuid());
127                 getOvsdbConnectionInstance().getDeviceInfo().clearDeviceOperData(RemoteMcastMacs.class, lumId);
128             } else {
129                 LOG.debug("Failed to delete McastMacRemote entry {}", rmm.getUuid());
130             }
131         }
132     }
133
134     private HwvtepLogicalSwitchRef getLogicalSwitchRef(UUID switchUUID) {
135         LogicalSwitch logicalSwitch = getOvsdbConnectionInstance().getDeviceInfo().getLogicalSwitch(switchUUID);
136         if (logicalSwitch != null) {
137             InstanceIdentifier<LogicalSwitches> switchIid =
138                     HwvtepSouthboundMapper.createInstanceIdentifier(getOvsdbConnectionInstance(), logicalSwitch);
139             return new HwvtepLogicalSwitchRef(switchIid);
140         }
141         LOG.debug("Failed to get LogicalSwitch {}", switchUUID);
142         LOG.trace("Available LogicalSwitches: {}",
143                         getOvsdbConnectionInstance().getDeviceInfo().getLogicalSwitches().values());
144         return null;
145     }
146
147     private static MacAddress getMacAddress(String mac) {
148         if (mac.equals(HwvtepSouthboundConstants.UNKNOWN_DST_STRING)) {
149             return HwvtepSouthboundConstants.UNKNOWN_DST_MAC;
150         } else {
151             return new MacAddress(mac);
152         }
153     }
154 }