manually cherry pick this patch https://git.opendaylight.org/gerrit/#/c/34579/
[unimgr.git] / impl / src / main / java / org / opendaylight / unimgr / command / EvcRemoveCommand.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
9 package org.opendaylight.unimgr.command;
10
11 import java.util.List;
12
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
15 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
16 import org.opendaylight.unimgr.api.AbstractCommand;
17 import org.opendaylight.unimgr.utils.EvcUtils;
18 import org.opendaylight.unimgr.utils.MdsalUtils;
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.Link;
23 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 import com.google.common.base.Optional;
29
30 public class EvcRemoveCommand extends AbstractCommand<Link> {
31
32     private static final Logger LOG = LoggerFactory.getLogger(EvcRemoveCommand.class);
33
34     public EvcRemoveCommand(final DataBroker dataBroker, final DataTreeModification<Link> removedEvcLink) {
35         super(dataBroker, removedEvcLink);
36     }
37
38     @Override
39     public void execute() {
40         final Link evcLink = dataObject.getRootNode().getDataBefore();
41         final EvcAugmentation evcAugmentation = evcLink.getAugmentation(EvcAugmentation.class);
42         final InstanceIdentifier<?> removedEvcIid = dataObject.getRootPath().getRootIdentifier();
43         if (evcAugmentation != null) {
44             final List<UniSource> unisSource = evcAugmentation.getUniSource();
45             final List<UniDest> unisDest = evcAugmentation.getUniDest();
46             if (unisSource != null && !unisSource.isEmpty()) {
47                 for (final UniSource source: unisSource) {
48                     if (source != null) {
49                         final Optional<Node> optionalSourceUniNode =
50                                 MdsalUtils.readNode(dataBroker,
51                                                                 LogicalDatastoreType.OPERATIONAL,
52                                                                 source.getUni());
53                         EvcUtils.deleteEvcData(dataBroker, optionalSourceUniNode);
54                     }
55                 }
56             }
57             if (unisDest != null && !unisDest.isEmpty()) {
58                 for (final UniDest dest : unisDest) {
59                     if (dest != null) {
60                         final Optional<Node> optionalDestUniNode =
61                                 MdsalUtils.readNode(dataBroker,
62                                                                 LogicalDatastoreType.OPERATIONAL,
63                                                                 dest.getUni());
64                         EvcUtils.deleteEvcData(dataBroker, optionalDestUniNode);
65                     }
66                 }
67             }
68         }
69         else {
70             LOG.info("EvcAugmentation is null");
71         }
72         MdsalUtils.deleteNode(dataBroker, removedEvcIid, LogicalDatastoreType.OPERATIONAL);
73     }
74 }