bug 6579 removed boilerplate code
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / transact / PhysicalPortRemoveCommand.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.ArrayList;
14 import java.util.Collection;
15 import java.util.HashMap;
16 import java.util.List;
17 import java.util.Map;
18 import java.util.Map.Entry;
19
20 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
21 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
22 import org.opendaylight.ovsdb.lib.notation.Mutator;
23 import org.opendaylight.ovsdb.lib.notation.UUID;
24 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
25 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
26 import org.opendaylight.ovsdb.schema.hardwarevtep.PhysicalPort;
27 import org.opendaylight.ovsdb.schema.hardwarevtep.PhysicalSwitch;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalPortAugmentation;
29 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 import com.google.common.base.Optional;
36 import com.google.common.collect.Sets;
37
38 public class PhysicalPortRemoveCommand extends AbstractTransactCommand {
39     private static final Logger LOG = LoggerFactory.getLogger(PhysicalPortRemoveCommand.class);
40
41     public PhysicalPortRemoveCommand(HwvtepOperationalState state,
42             Collection<DataTreeModification<Node>> changes) {
43         super(state, changes);
44     }
45
46     @Override
47     public void execute(TransactionBuilder transaction) {
48         //TODO reuse from base class instead of extractRemovedPorts
49         Map<InstanceIdentifier<Node>, List<HwvtepPhysicalPortAugmentation>> removeds =
50                 extractRemovedPorts(getChanges(), HwvtepPhysicalPortAugmentation.class);
51         if (!removeds.isEmpty()) {
52             for (Entry<InstanceIdentifier<Node>, List<HwvtepPhysicalPortAugmentation>> removed:
53                 removeds.entrySet()) {
54                 removePhysicalPort(transaction,  removed.getKey(), removed.getValue());
55             }
56         }
57     }
58
59     private void removePhysicalPort(TransactionBuilder transaction,
60             InstanceIdentifier<Node> psNodeiid,
61             List<HwvtepPhysicalPortAugmentation> listPort) {
62         for (HwvtepPhysicalPortAugmentation port : listPort) {
63             LOG.debug("Removing a physical port named: {}", port.getHwvtepNodeName().getValue());
64             Optional<HwvtepPhysicalPortAugmentation> operationalPhysicalPortOptional =
65                     getOperationalState().getPhysicalPortAugmentation(psNodeiid, port.getHwvtepNodeName());
66             PhysicalPort physicalPort = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), PhysicalPort.class, null);
67             //get managing global node of physicalSwitchBelong
68             //InstanceIdentifier<?> globalNodeIid = physicalSwitchBelong.getManagedBy().getValue();
69             if (operationalPhysicalPortOptional.isPresent()) {
70                 UUID physicalPortUuid = new UUID(operationalPhysicalPortOptional.get().getPhysicalPortUuid().getValue());
71                 PhysicalSwitch physicalSwitch = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(),
72                         PhysicalSwitch.class, null);
73                 transaction.add(op.delete(physicalPort.getSchema())
74                         .where(physicalPort.getUuidColumn().getSchema().opEqual(physicalPortUuid)).build());
75                 transaction.add(op.comment("Physical Port: Deleting " + port.getHwvtepNodeName().getValue()));
76                 transaction.add(op.mutate(physicalSwitch.getSchema())
77                         .addMutation(physicalSwitch.getPortsColumn().getSchema(), Mutator.DELETE,
78                                 Sets.newHashSet(physicalPortUuid)));
79                 transaction.add(op.comment("Physical Switch: Mutating " + port.getHwvtepNodeName().getValue() + " " + physicalPortUuid));
80             } else {
81                 LOG.warn("Unable to delete logical switch {} because it was not found in the operational store, "
82                         + "and thus we cannot retrieve its UUID", port.getHwvtepNodeName().getValue());
83             }
84         }
85     }
86
87     protected Map<InstanceIdentifier<Node>, List<HwvtepPhysicalPortAugmentation>> extractRemovedPorts(
88             Collection<DataTreeModification<Node>> changes, Class<HwvtepPhysicalPortAugmentation> class1) {
89         Map<InstanceIdentifier<Node>, List<HwvtepPhysicalPortAugmentation>> result
90             = new HashMap<InstanceIdentifier<Node>, List<HwvtepPhysicalPortAugmentation>>();
91         if (changes != null && !changes.isEmpty()) {
92             for (DataTreeModification<Node> change : changes) {
93                 final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
94                 final DataObjectModification<Node> mod = change.getRootNode();
95                 //If the node which physical ports belong to is removed, all physical ports
96                 //should be removed too.
97                 Node removed = TransactUtils.getRemoved(mod);
98                 if (removed != null) {
99                     List<HwvtepPhysicalPortAugmentation> lswitchListRemoved = new ArrayList<HwvtepPhysicalPortAugmentation>();
100                     if (removed.getTerminationPoint() != null) {
101                         for (TerminationPoint tp : removed.getTerminationPoint()) {
102                             HwvtepPhysicalPortAugmentation hppAugmentation = tp.getAugmentation(HwvtepPhysicalPortAugmentation.class);
103                             if (hppAugmentation != null) {
104                                 lswitchListRemoved.add(hppAugmentation);
105                             }
106                         }
107                     }
108                     if (!lswitchListRemoved.isEmpty()) {
109                         result.put(key, lswitchListRemoved);
110                     }
111                 }
112                 //If the node which physical ports belong to is updated, and physical ports may
113                 //be created or updated or deleted, we need to get deleted ones.
114                 Node updated = TransactUtils.getUpdated(mod);
115                 Node before = mod.getDataBefore();
116                 if (updated != null && before != null) {
117                     List<HwvtepPhysicalPortAugmentation> portListUpdated = new ArrayList<HwvtepPhysicalPortAugmentation>();
118                     List<HwvtepPhysicalPortAugmentation> portListBefore = new ArrayList<HwvtepPhysicalPortAugmentation>();
119                     List<HwvtepPhysicalPortAugmentation> portListRemoved = new ArrayList<HwvtepPhysicalPortAugmentation>();
120                     if (updated.getTerminationPoint() != null) {
121                         for (TerminationPoint tp : updated.getTerminationPoint()) {
122                             HwvtepPhysicalPortAugmentation hppAugmentation = tp.getAugmentation(HwvtepPhysicalPortAugmentation.class);
123                             if (hppAugmentation != null) {
124                                 portListUpdated.add(hppAugmentation);
125                             }
126                         }
127                     }
128                     if (before.getTerminationPoint() != null) {
129                         for (TerminationPoint tp : before.getTerminationPoint()) {
130                             HwvtepPhysicalPortAugmentation hppAugmentation = tp.getAugmentation(HwvtepPhysicalPortAugmentation.class);
131                             if (hppAugmentation != null) {
132                                 portListBefore.add(hppAugmentation);
133                             }
134                         }
135                     }
136                     portListBefore.removeAll(portListUpdated);
137                     //then exclude updated physical ports
138                     for (HwvtepPhysicalPortAugmentation portBefore: portListBefore) {
139                         int i = 0;
140                         for(; i < portListUpdated.size(); i++) {
141                             if (portBefore.getHwvtepNodeName().equals(portListUpdated.get(i).getHwvtepNodeName())) {
142                                 break;
143                             }
144                         }
145                         if (i == portListUpdated.size()) {
146                             portListRemoved.add(portBefore);
147                         }
148                     }
149                     if (!portListRemoved.isEmpty()) {
150                         result.put(key, portListRemoved);
151                     }
152                 }
153             }
154         }
155         return result;
156     }
157 }