9d06ba27efaea441b24f841e943f4fddf9877f1e
[unimgr.git] / impl / src / main / java / org / opendaylight / unimgr / command / UniDeleteCommand.java
1 /*
2  * Copyright (c) 2015 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.Set;
11
12 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
13 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
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.controller.md.sal.common.api.data.TransactionCommitFailedException;
17 import org.opendaylight.unimgr.impl.UnimgrMapper;
18 import org.opendaylight.unimgr.impl.UnimgrUtils;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeRef;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.UniAugmentation;
21 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
22 import org.opendaylight.yangtools.yang.binding.DataObject;
23 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 import com.google.common.base.Optional;
28 import com.google.common.util.concurrent.CheckedFuture;
29
30 public class UniDeleteCommand extends AbstractDeleteCommand {
31
32     private static final Logger LOG = LoggerFactory.getLogger(UniDeleteCommand.class);
33
34     public UniDeleteCommand(DataBroker dataBroker,
35             AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes) {
36         super.changes = changes;
37         super.dataBroker = dataBroker;
38     }
39
40     @Override
41     public void execute() {
42         Set<InstanceIdentifier<UniAugmentation>> removedUnis = UnimgrUtils.extractRemoved(changes, UniAugmentation.class);
43         Set<InstanceIdentifier<Node>> removedNodes = UnimgrUtils.extractRemoved(changes, Node.class);
44         if (!removedUnis.isEmpty()) {
45             for (InstanceIdentifier<UniAugmentation> removedUniIid: removedUnis) {
46                 UniAugmentation uniAug = UnimgrUtils.read(dataBroker, LogicalDatastoreType.CONFIGURATION, removedUniIid);
47                 if(uniAug != null){
48                     LOG.trace("Uni Augmentation present.");
49                     OvsdbNodeRef ovsdbNodeRef = uniAug.getOvsdbNodeRef();
50                     InstanceIdentifier<Node> ovsdbIid = ovsdbNodeRef.getValue().firstIdentifierOf(Node.class);
51                     Optional<Node> optNode = UnimgrUtils.readNode(dataBroker, LogicalDatastoreType.CONFIGURATION, ovsdbIid);
52                     if (optNode.isPresent()) {
53                         Node ovsdbNode = optNode.get();
54                         InstanceIdentifier<Node> iidBridgeNode = UnimgrMapper.getOvsdbBridgeNodeIid(ovsdbNode);
55                         deleteNode(iidBridgeNode);
56                         LOG.info("Received a request to remove a UNI BridgeNode ", iidBridgeNode);
57                     }
58                     deleteNode(ovsdbIid);
59                     LOG.info("Received a request to remove an UNI ", removedUniIid);
60                 }
61             }
62         }
63         else {LOG.info("Removed UNIs is empty.");}
64
65         if(!removedNodes.isEmpty()) {
66             for(InstanceIdentifier<Node> iidNode : removedNodes) {
67                 Optional<Node> optNode = UnimgrUtils.readNode(dataBroker, LogicalDatastoreType.CONFIGURATION, iidNode);
68                 if (optNode.isPresent()) {
69                     Node ovsdbNode = optNode.get();
70                     InstanceIdentifier<Node> iidBridgeNode = UnimgrMapper.getOvsdbBridgeNodeIid(ovsdbNode);
71                     deleteNode(iidBridgeNode);
72                     LOG.info("Received a request to remove a BridgeNode ", iidBridgeNode);
73                 }
74                 deleteNode(iidNode);
75                 LOG.info("Received a request to remove a Node ", iidNode);
76            }
77         }
78         else {LOG.info("Removed Nodes is empty.");}
79     }
80
81     private void deleteNode(InstanceIdentifier<Node> iidNode) {
82         WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
83         transaction.delete(LogicalDatastoreType.OPERATIONAL, iidNode);
84         transaction.delete(LogicalDatastoreType.CONFIGURATION, iidNode);
85         CheckedFuture<Void, TransactionCommitFailedException> future = transaction.submit();
86         try {
87             future.checkedGet();
88         } catch (TransactionCommitFailedException e) {
89             LOG.warn("Failed to delete iidNode {} {}", e.getMessage(), iidNode);
90         }
91     }
92 }