Merge "Fixed hard-coded port 12345 in TestClient"
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / transactions / md / HwvtepMacEntriesRemoveCommand.java
1 /*
2  * Copyright (c) 2015, 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
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             } else {
88                 LOG.debug("Failed to delete UcastMacRemote entry {}", rum.getUuid());
89             }
90         }
91     }
92
93     private void removeMcastMacsLocal(ReadWriteTransaction transaction) {
94         Collection<McastMacsLocal> deletedLMMRows =
95                 TyperUtils.extractRowsRemoved(McastMacsLocal.class, getUpdates(), getDbSchema()).values();
96         for (McastMacsLocal lmm : deletedLMMRows) {
97             if(lmm.getMac() != null && lmm.getLogicalSwitchColumn() != null &&
98                             lmm.getLogicalSwitchColumn().getData() != null) {
99                 InstanceIdentifier<LocalMcastMacs> lumId = getOvsdbConnectionInstance().getInstanceIdentifier()
100                     .augmentation(HwvtepGlobalAugmentation.class)
101                     .child(LocalMcastMacs.class,
102                                     new LocalMcastMacsKey(getLogicalSwitchRef(lmm.getLogicalSwitchColumn().getData()),
103                                                     getMacAddress(lmm.getMac())));
104                 transaction.delete(LogicalDatastoreType.OPERATIONAL, lumId);
105             } else {
106                 LOG.debug("Failed to delete McastMacLocal entry {}", lmm.getUuid());
107             }
108         }
109     }
110
111     private void removeMcastMacsRemote(ReadWriteTransaction transaction) {
112         Collection<McastMacsRemote> deletedMMRRows =
113                 TyperUtils.extractRowsRemoved(McastMacsRemote.class, getUpdates(), getDbSchema()).values();
114         for (McastMacsRemote rmm : deletedMMRRows) {
115             if(rmm.getMac() != null && rmm.getLogicalSwitchColumn() != null &&
116                             rmm.getLogicalSwitchColumn().getData() != null) {
117                 InstanceIdentifier<RemoteMcastMacs> lumId = getOvsdbConnectionInstance().getInstanceIdentifier()
118                     .augmentation(HwvtepGlobalAugmentation.class)
119                     .child(RemoteMcastMacs.class,
120                                     new RemoteMcastMacsKey(getLogicalSwitchRef(rmm.getLogicalSwitchColumn().getData()),
121                                                     getMacAddress(rmm.getMac())));
122                 transaction.delete(LogicalDatastoreType.OPERATIONAL, 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> lSwitchIid =
133                     HwvtepSouthboundMapper.createInstanceIdentifier(getOvsdbConnectionInstance(), logicalSwitch);
134             return new HwvtepLogicalSwitchRef(lSwitchIid);
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 }