20c59afe8d6802542b375815c23095dcee0cb8ae
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / transact / PhysicalSwitchRemoveCommand.java
1 /*
2  * Copyright © 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 java.util.Collection;
13 import java.util.Collections;
14 import java.util.HashMap;
15 import java.util.Map;
16 import java.util.Map.Entry;
17 import java.util.Optional;
18 import org.opendaylight.mdsal.binding.api.DataObjectModification;
19 import org.opendaylight.mdsal.binding.api.DataTreeModification;
20 import org.opendaylight.ovsdb.lib.notation.Mutator;
21 import org.opendaylight.ovsdb.lib.notation.UUID;
22 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
23 import org.opendaylight.ovsdb.schema.hardwarevtep.Global;
24 import org.opendaylight.ovsdb.schema.hardwarevtep.PhysicalSwitch;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.PhysicalSwitchAugmentation;
26 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
27 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 public class PhysicalSwitchRemoveCommand extends AbstractTransactCommand {
32     private static final Logger LOG = LoggerFactory.getLogger(PhysicalSwitchRemoveCommand.class);
33
34     public PhysicalSwitchRemoveCommand(final HwvtepOperationalState state,
35             final Collection<DataTreeModification<Node>> changes) {
36         super(state, changes);
37     }
38
39     @Override
40     public void execute(final TransactionBuilder transaction) {
41         Map<InstanceIdentifier<Node>, PhysicalSwitchAugmentation> removeds =
42                 extractRemovedSwitches(getChanges(),PhysicalSwitchAugmentation.class);
43         if (!removeds.isEmpty()) {
44             for (Entry<InstanceIdentifier<Node>, PhysicalSwitchAugmentation> removed:
45                 removeds.entrySet()) {
46                 removePhysicalSwitch(transaction,  removed.getKey(), removed.getValue());
47             }
48         }
49     }
50
51     private void removePhysicalSwitch(final TransactionBuilder transaction,
52             final InstanceIdentifier<Node> iid, final PhysicalSwitchAugmentation physicalSwitchAugmentation) {
53         LOG.debug("Removing a physical switch named: {}", physicalSwitchAugmentation.getHwvtepNodeName().getValue());
54         Optional<PhysicalSwitchAugmentation> operationalPhysicalSwitchOptional =
55                 getOperationalState().getPhysicalSwitchAugmentation(iid);
56         PhysicalSwitch physicalSwitch = transaction.getTypedRowSchema(PhysicalSwitch.class);
57         if (operationalPhysicalSwitchOptional.isPresent()
58                 && operationalPhysicalSwitchOptional.get().getPhysicalSwitchUuid() != null) {
59             UUID physicalSwitchUuid = new UUID(operationalPhysicalSwitchOptional.get()
60                     .getPhysicalSwitchUuid().getValue());
61             Global global = transaction.getTypedRowSchema(Global.class);
62             transaction.add(op.delete(physicalSwitch.getSchema())
63                     .where(physicalSwitch.getUuidColumn().getSchema().opEqual(physicalSwitchUuid)).build());
64             transaction.add(op.comment("Physical Switch: Deleting "
65                     + physicalSwitchAugmentation.getHwvtepNodeName().getValue()));
66             transaction.add(op.mutate(global.getSchema())
67                     .addMutation(global.getSwitchesColumn().getSchema(), Mutator.DELETE,
68                             Collections.singleton(physicalSwitchUuid)));
69             transaction.add(op.comment("Global: Mutating " + physicalSwitchAugmentation.getHwvtepNodeName().getValue()
70                     + " " + physicalSwitchUuid));
71         } else {
72             LOG.warn("Unable to delete physical switch {} because it was not found in the operational store",
73                     physicalSwitchAugmentation.getHwvtepNodeName().getValue());
74         }
75     }
76
77     private static Map<InstanceIdentifier<Node>, PhysicalSwitchAugmentation> extractRemovedSwitches(
78             final Collection<DataTreeModification<Node>> changes, final Class<PhysicalSwitchAugmentation> class1) {
79         Map<InstanceIdentifier<Node>, PhysicalSwitchAugmentation> result = new HashMap<>();
80         if (changes != null && !changes.isEmpty()) {
81             for (DataTreeModification<Node> change : changes) {
82                 final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
83                 final DataObjectModification<Node> mod = change.getRootNode();
84                 Node removed = TransactUtils.getRemoved(mod);
85                 if (removed != null) {
86                     PhysicalSwitchAugmentation physicalSwitch =
87                             removed.augmentation(PhysicalSwitchAugmentation.class);
88                     if (physicalSwitch != null) {
89                         result.put(key, physicalSwitch);
90                     }
91                 }
92             }
93         }
94         return result;
95     }
96 }