Integrate createTopologyListPath()
[netconf.git] / netconf / sal-netconf-connector / src / main / java / org / opendaylight / netconf / sal / connect / netconf / sal / LockChangeListener.java
1 /*
2  * Copyright (c) 2019 Lumina Networks, Inc. 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.netconf.sal.connect.netconf.sal;
9
10 import java.util.Collection;
11 import org.opendaylight.mdsal.binding.api.DataObjectModification;
12 import org.opendaylight.mdsal.binding.api.DataTreeChangeListener;
13 import org.opendaylight.mdsal.binding.api.DataTreeModification;
14 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
15 import org.opendaylight.netconf.dom.api.NetconfDataTreeService;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.optional.rev190614.netconf.node.fields.optional.topology.node.DatastoreLock;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 final class LockChangeListener implements DataTreeChangeListener<DatastoreLock> {
21
22     private static final Logger LOG = LoggerFactory.getLogger(LockChangeListener.class);
23
24     private final NetconfDeviceDataBroker netconfDeviceDataBroker;
25     private final AbstractNetconfDataTreeService netconfDataTreeService;
26
27     LockChangeListener(final DOMDataBroker netconfDeviceDataBrokder,
28                        final NetconfDataTreeService netconfDataTreeService) {
29         this.netconfDeviceDataBroker = (NetconfDeviceDataBroker)netconfDeviceDataBrokder;
30         this.netconfDataTreeService = (AbstractNetconfDataTreeService) netconfDataTreeService;
31     }
32
33     @Override
34     public void onDataTreeChanged(final Collection<DataTreeModification<DatastoreLock>> changes) {
35         for (final DataTreeModification<DatastoreLock> change : changes) {
36             final DataObjectModification<DatastoreLock> rootNode = change.getRootNode();
37             switch (rootNode.getModificationType()) {
38                 case SUBTREE_MODIFIED:
39                 case WRITE:
40                     if (!rootNode.getDataAfter().getDatastoreLockAllowed()) {
41                         LOG.warn("With blocking the lock/unlock operations, the user is coming to "
42                                  + "operate in a manner which is not supported. Concurrent access to "
43                                  + "the data store may interfere with data consistency.");
44                     }
45                     netconfDeviceDataBroker.setLockAllowed(rootNode.getDataAfter().getDatastoreLockAllowed());
46                     netconfDataTreeService.setLockAllowed(rootNode.getDataAfter().getDatastoreLockAllowed());
47                     break;
48                 case DELETE:
49                     netconfDeviceDataBroker.setLockAllowed(true);
50                     netconfDataTreeService.setLockAllowed(true);
51                     break;
52                 default:
53                     LOG.debug("Unsupported modification type: {}.", rootNode.getModificationType());
54             }
55         }
56     }
57 }