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