Bug 5386: Delete QoS and Queues entries from ovsdb on UNI deletion
[unimgr.git] / impl / src / main / java / org / opendaylight / unimgr / command / EvcDeleteCommand.java
1 /*
2  * Copyright (c) 2016 CableLabs 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 package org.opendaylight.unimgr.command;
9
10 import java.util.List;
11 import java.util.Set;
12
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
15 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
16 import org.opendaylight.unimgr.utils.EvcUtils;
17 import org.opendaylight.unimgr.utils.MdsalUtils;
18 import org.opendaylight.unimgr.utils.OvsdbUtils;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.EvcAugmentation;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.evc.UniDest;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.evc.UniSource;
22 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
23 import org.opendaylight.yangtools.yang.binding.DataObject;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
25
26 import com.google.common.base.Optional;
27
28 public class EvcDeleteCommand extends AbstractDeleteCommand {
29
30     public EvcDeleteCommand(DataBroker dataBroker,
31             AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes) {
32         super.changes = changes;
33         super.dataBroker = dataBroker;
34     }
35
36     @Override
37     public void execute() {
38         Set<InstanceIdentifier<EvcAugmentation>> removedEvcs = OvsdbUtils.extractRemoved(changes,
39                                                                                          EvcAugmentation.class);
40         if (!removedEvcs.isEmpty()) {
41             for (InstanceIdentifier<EvcAugmentation> removedEvcIid: removedEvcs) {
42                 EvcAugmentation evcAugmentation = MdsalUtils.read(dataBroker,
43                                                                    LogicalDatastoreType.OPERATIONAL,
44                                                                    removedEvcIid);
45                 if (evcAugmentation != null) {
46                     List<UniSource> unisSource = evcAugmentation.getUniSource();
47                     List<UniDest> unisDest = evcAugmentation.getUniDest();
48                     if (unisSource != null && !unisSource.isEmpty()) {
49                         for (UniSource source: unisSource) {
50                             if (source != null) {
51                                 Optional<Node> optionalSourceUniNode =
52                                         MdsalUtils.readNode(dataBroker,
53                                                                         LogicalDatastoreType.OPERATIONAL,
54                                                                         source.getUni());
55                                 EvcUtils.deleteEvcData(dataBroker, optionalSourceUniNode);
56                             }
57                         }
58                     }
59                     if (unisDest != null && !unisDest.isEmpty()) {
60                         for (UniDest dest : unisDest) {
61                             if (dest != null) {
62                                 Optional<Node> optionalDestUniNode =
63                                         MdsalUtils.readNode(dataBroker,
64                                                                         LogicalDatastoreType.OPERATIONAL,
65                                                                         dest.getUni());
66                                 EvcUtils.deleteEvcData(dataBroker, optionalDestUniNode);
67                             }
68                         }
69                     }
70                 }
71                 MdsalUtils.deleteNode(dataBroker, removedEvcIid, LogicalDatastoreType.OPERATIONAL);
72             }
73         }
74     }
75 }