Update MRI projects for Aluminium
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / transact / UcastMacsRemoteRemoveCommand.java
1 /*
2  * Copyright (c) 2015, 2017 China Telecom Beijing Research Institute 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.ovsdb.hwvtepsouthbound.transact;
9
10 import static org.opendaylight.ovsdb.lib.operations.Operations.op;
11
12 import com.google.common.collect.Lists;
13 import java.util.Collection;
14 import java.util.List;
15 import java.util.Map;
16 import java.util.Map.Entry;
17 import java.util.Objects;
18 import org.opendaylight.mdsal.binding.api.DataTreeModification;
19 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepDeviceInfo;
20 import org.opendaylight.ovsdb.lib.notation.UUID;
21 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
22 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
23 import org.opendaylight.ovsdb.schema.hardwarevtep.UcastMacsRemote;
24 import org.opendaylight.ovsdb.utils.mdsal.utils.TransactionType;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteUcastMacs;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteUcastMacsKey;
29 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 public class UcastMacsRemoteRemoveCommand
35         extends AbstractTransactCommand<RemoteUcastMacs, RemoteUcastMacsKey, HwvtepGlobalAugmentation> {
36     private static final Logger LOG = LoggerFactory.getLogger(UcastMacsRemoteRemoveCommand.class);
37
38     public UcastMacsRemoteRemoveCommand(final HwvtepOperationalState state,
39             final Collection<DataTreeModification<Node>> changes) {
40         super(state, changes);
41     }
42
43     @Override
44     public void execute(final TransactionBuilder transaction) {
45         Map<InstanceIdentifier<Node>, List<RemoteUcastMacs>> removeds =
46                 extractRemoved(getChanges(),RemoteUcastMacs.class);
47         if (!removeds.isEmpty()) {
48             for (Entry<InstanceIdentifier<Node>, List<RemoteUcastMacs>> removed:
49                     removeds.entrySet()) {
50                 onConfigUpdate(transaction, removed.getKey(), removed.getValue());
51             }
52         }
53     }
54
55     public void onConfigUpdate(final TransactionBuilder transaction,
56                                final InstanceIdentifier<Node> nodeIid,
57                                final List<RemoteUcastMacs> macs) {
58         for (RemoteUcastMacs mac : macs) {
59             InstanceIdentifier<RemoteUcastMacs> macKey = nodeIid.augmentation(HwvtepGlobalAugmentation.class)
60                     .child(RemoteUcastMacs.class, mac.key());
61             getDeviceInfo().clearConfigData(RemoteUcastMacs.class, macKey);
62             onConfigUpdate(transaction, nodeIid, mac, macKey);
63         }
64     }
65
66     @Override
67     public void onConfigUpdate(final TransactionBuilder transaction,
68                                final InstanceIdentifier<Node> nodeIid,
69                                final RemoteUcastMacs remoteMcastMac,
70                                final InstanceIdentifier macKey,
71                                final Object... extraData) {
72         processDependencies(EmptyDependencyGetter.INSTANCE, transaction, nodeIid, macKey, remoteMcastMac);
73     }
74
75     @Override
76     public void doDeviceTransaction(final TransactionBuilder transaction,
77                                     final InstanceIdentifier<Node> instanceIdentifier,
78                                     final RemoteUcastMacs mac,
79                                     final InstanceIdentifier macKey,
80                                     final Object... extraData) {
81         removeUcastMacRemote(transaction, instanceIdentifier, Lists.newArrayList(mac));
82     }
83
84     private void removeUcastMacRemote(final TransactionBuilder transaction,
85                                       final InstanceIdentifier<Node> instanceIdentifier,
86                                       final List<RemoteUcastMacs> macList) {
87         for (RemoteUcastMacs mac: macList) {
88             final InstanceIdentifier<RemoteUcastMacs> macIid =
89                     instanceIdentifier.augmentation(HwvtepGlobalAugmentation.class)
90                             .child(RemoteUcastMacs.class, mac.key());
91             HwvtepDeviceInfo.DeviceData deviceData = getDeviceOpData(RemoteUcastMacs.class, macIid);
92             UcastMacsRemote ucastMacsRemote = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(),
93                     UcastMacsRemote.class, null);
94             LOG.debug("Remove received for remoteUcastMacs, key: {} txId: {}", macIid,
95                     getOperationalState().getTransactionId());
96             boolean deleted = false;
97             if (deviceData != null && deviceData.getUuid() != null) {
98                 if (deviceData.getData() != null && deviceData.getData() instanceof UcastMacsRemote
99                         && ((UcastMacsRemote)deviceData.getData()).getLogicalSwitchColumn() != null) {
100                     UUID logicalSwitchUid = ((UcastMacsRemote) deviceData.getData()).getLogicalSwitchColumn().getData();
101                     if (logicalSwitchUid != null) {
102                         deleted = true;
103                         updateCurrentTxDeleteData(RemoteUcastMacs.class, macIid, mac);
104                         transaction.add(op.delete(ucastMacsRemote.getSchema())
105                                 .where(ucastMacsRemote.getLogicalSwitchColumn().getSchema()
106                                         .opEqual(logicalSwitchUid))
107                                 .and(ucastMacsRemote.getMacColumn().getSchema().opEqual(mac.getMacEntryKey()
108                                         .getValue())).build());
109                         LOG.info("CONTROLLER - {} {}", TransactionType.DELETE, ucastMacsRemote);
110                     }
111                 }
112             }
113             if (!deleted && deviceData != null && deviceData.getUuid() != null) {
114                 updateCurrentTxDeleteData(RemoteUcastMacs.class, macIid, mac);
115                 UUID macEntryUUID = deviceData.getUuid();
116                 ucastMacsRemote.getUuidColumn().setData(macEntryUUID);
117                 transaction.add(op.delete(ucastMacsRemote.getSchema())
118                         .where(ucastMacsRemote.getUuidColumn().getSchema().opEqual(macEntryUUID)).build());
119                 LOG.info("CONTROLLER - {} {}", TransactionType.DELETE, ucastMacsRemote);
120             } else {
121                 LOG.trace("Remove failed to find in op datastore key:{} txId:{}", macIid, getOperationalState()
122                         .getTransactionId());
123             }
124             getDeviceInfo().clearConfigData(RemoteUcastMacs.class, macIid);
125         }
126     }
127
128     @Override
129     protected Map<RemoteUcastMacsKey, RemoteUcastMacs> getData(final HwvtepGlobalAugmentation augmentation) {
130         return augmentation.getRemoteUcastMacs();
131     }
132
133     @Override
134     protected boolean areEqual(final RemoteUcastMacs remoteUcastMacs1, final RemoteUcastMacs remoteUcastMacs2) {
135         return Objects.equals(remoteUcastMacs1.key(), remoteUcastMacs2.key());
136     }
137
138     @Override
139     public void onSuccess(final TransactionBuilder tx) {
140         for (MdsalUpdate mdsalUpdate : updates) {
141             RemoteUcastMacs mac = (RemoteUcastMacs) mdsalUpdate.getNewData();
142             InstanceIdentifier<RemoteUcastMacs> macIid = mdsalUpdate.getKey();
143             getDeviceInfo().removeRemoteUcast(
144                     (InstanceIdentifier<LogicalSwitches>) mac.getLogicalSwitchRef().getValue(), macIid);
145         }
146         getDeviceInfo().onOperDataAvailable();
147     }
148
149     @Override
150     protected boolean isDeleteCmd() {
151         return true;
152     }
153
154 }