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