Change logical-switch-ref to iid in hwvtep.yang
[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         Map<InstanceIdentifier<Node>, List<HwvtepPhysicalPortAugmentation>> removeds =
49                 extractRemoved(getChanges(),HwvtepPhysicalPortAugmentation.class);
50         if (!removeds.isEmpty()) {
51             for (Entry<InstanceIdentifier<Node>, List<HwvtepPhysicalPortAugmentation>> removed:
52                 removeds.entrySet()) {
53                 removePhysicalPort(transaction,  removed.getKey(), removed.getValue());
54             }
55         }
56     }
57
58     private void removePhysicalPort(TransactionBuilder transaction,
59             InstanceIdentifier<Node> psNodeiid,
60             List<HwvtepPhysicalPortAugmentation> listPort) {
61         for (HwvtepPhysicalPortAugmentation port : listPort) {
62             LOG.debug("Removing a physical port named: {}", port.getHwvtepNodeName().getValue());
63             Optional<HwvtepPhysicalPortAugmentation> operationalPhysicalPortOptional =
64                     getOperationalState().getPhysicalPortAugmentation(psNodeiid, port.getHwvtepNodeName());
65             PhysicalPort physicalPort = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), PhysicalPort.class, null);
66             //get managing global node of physicalSwitchBelong
67             //InstanceIdentifier<?> globalNodeIid = physicalSwitchBelong.getManagedBy().getValue();
68             if (operationalPhysicalPortOptional.isPresent()) {
69                 UUID physicalPortUuid = new UUID(operationalPhysicalPortOptional.get().getPhysicalPortUuid().getValue());
70                 PhysicalSwitch physicalSwitch = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(),
71                         PhysicalSwitch.class, null);
72                 transaction.add(op.delete(physicalPort.getSchema())
73                         .where(physicalPort.getUuidColumn().getSchema().opEqual(physicalPortUuid)).build());
74                 transaction.add(op.comment("Physical Port: Deleting " + port.getHwvtepNodeName().getValue()));
75                 transaction.add(op.mutate(physicalSwitch.getSchema())
76                         .addMutation(physicalSwitch.getPortsColumn().getSchema(), Mutator.DELETE,
77                                 Sets.newHashSet(physicalPortUuid)));
78                 transaction.add(op.comment("Physical Switch: Mutating " + port.getHwvtepNodeName().getValue() + " " + physicalPortUuid));
79             } else {
80                 LOG.warn("Unable to delete logical switch {} because it was not found in the operational store, "
81                         + "and thus we cannot retrieve its UUID", port.getHwvtepNodeName().getValue());
82             }
83         }
84     }
85
86     private Map<InstanceIdentifier<Node>, List<HwvtepPhysicalPortAugmentation>> extractRemoved(
87             Collection<DataTreeModification<Node>> changes, Class<HwvtepPhysicalPortAugmentation> class1) {
88         Map<InstanceIdentifier<Node>, List<HwvtepPhysicalPortAugmentation>> result
89             = new HashMap<InstanceIdentifier<Node>, List<HwvtepPhysicalPortAugmentation>>();
90         if (changes != null && !changes.isEmpty()) {
91             for (DataTreeModification<Node> change : changes) {
92                 final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
93                 final DataObjectModification<Node> mod = change.getRootNode();
94                 //If the node which physical ports belong to is removed, all physical ports
95                 //should be removed too.
96                 Node removed = TransactUtils.getRemoved(mod);
97                 if (removed != null) {
98                     List<HwvtepPhysicalPortAugmentation> lswitchListRemoved = new ArrayList<HwvtepPhysicalPortAugmentation>();
99                     if (removed.getTerminationPoint() != null) {
100                         for (TerminationPoint tp : removed.getTerminationPoint()) {
101                             HwvtepPhysicalPortAugmentation hppAugmentation = tp.getAugmentation(HwvtepPhysicalPortAugmentation.class);
102                             if (hppAugmentation != null) {
103                                 lswitchListRemoved.add(hppAugmentation);
104                             }
105                         }
106                     }
107                     if (!lswitchListRemoved.isEmpty()) {
108                         result.put(key, lswitchListRemoved);
109                     }
110                 }
111                 //If the node which physical ports belong to is updated, and physical ports may
112                 //be created or updated or deleted, we need to get deleted ones.
113                 Node updated = TransactUtils.getUpdated(mod);
114                 Node before = mod.getDataBefore();
115                 if (updated != null && before != null) {
116                     List<HwvtepPhysicalPortAugmentation> portListUpdated = new ArrayList<HwvtepPhysicalPortAugmentation>();
117                     List<HwvtepPhysicalPortAugmentation> portListBefore = new ArrayList<HwvtepPhysicalPortAugmentation>();
118                     List<HwvtepPhysicalPortAugmentation> portListRemoved = new ArrayList<HwvtepPhysicalPortAugmentation>();
119                     if (updated.getTerminationPoint() != null) {
120                         for (TerminationPoint tp : updated.getTerminationPoint()) {
121                             HwvtepPhysicalPortAugmentation hppAugmentation = tp.getAugmentation(HwvtepPhysicalPortAugmentation.class);
122                             if (hppAugmentation != null) {
123                                 portListUpdated.add(hppAugmentation);
124                             }
125                         }
126                     }
127                     if (before.getTerminationPoint() != null) {
128                         for (TerminationPoint tp : before.getTerminationPoint()) {
129                             HwvtepPhysicalPortAugmentation hppAugmentation = tp.getAugmentation(HwvtepPhysicalPortAugmentation.class);
130                             if (hppAugmentation != null) {
131                                 portListBefore.add(hppAugmentation);
132                             }
133                         }
134                     }
135                     portListBefore.removeAll(portListUpdated);
136                     //then exclude updated physical ports
137                     for (HwvtepPhysicalPortAugmentation portBefore: portListBefore) {
138                         int i = 0;
139                         for(; i < portListUpdated.size(); i++) {
140                             if (portBefore.getHwvtepNodeName().equals(portListUpdated.get(i).getHwvtepNodeName())) {
141                                 break;
142                             }
143                         }
144                         if (i == portListUpdated.size()) {
145                             portListRemoved.add(portBefore);
146                         }
147                     }
148                     if (!portListRemoved.isEmpty()) {
149                         result.put(key, portListRemoved);
150                     }
151                 }
152             }
153         }
154         return result;
155     }
156 }