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