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