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