9cca810ec98133ff54e818807ac282bbd761df49
[netconf.git] / netconf / config-netconf-connector / src / main / java / org / opendaylight / netconf / confignetconfconnector / operations / Lock.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. 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.netconf.confignetconfconnector.operations;
10
11 import com.google.common.base.Optional;
12 import org.opendaylight.controller.config.facade.xml.Datastore;
13 import org.opendaylight.controller.config.util.xml.DocumentedException;
14 import org.opendaylight.controller.config.util.xml.XmlElement;
15 import org.opendaylight.controller.config.util.xml.XmlUtil;
16 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
17 import org.opendaylight.netconf.util.mapping.AbstractLastNetconfOperation;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20 import org.w3c.dom.Document;
21 import org.w3c.dom.Element;
22
23 /**
24  * Simple Lock implementation that pretends to lock candidate datastore.
25  * Candidate datastore is allocated per session and is private so no real locking is needed
26  * (JMX is the only possible interference)
27  */
28 public class Lock extends AbstractLastNetconfOperation {
29
30     private static final Logger LOG = LoggerFactory.getLogger(Lock.class);
31
32     private static final String LOCK = "lock";
33     private static final String TARGET_KEY = "target";
34
35     public Lock(final String netconfSessionIdForReporting) {
36         super(netconfSessionIdForReporting);
37     }
38
39     @Override
40     protected Element handleWithNoSubsequentOperations(final Document document,
41                                                        final XmlElement operationElement) throws DocumentedException {
42         final Datastore targetDatastore = extractTargetParameter(operationElement);
43         if (targetDatastore == Datastore.candidate) {
44             // Since candidate datastore instances are allocated per session and not accessible anywhere else,
45             // no need to lock
46             LOG.debug("Locking {} datastore on session: {}", targetDatastore, getNetconfSessionIdForReporting());
47             // TODO should this fail if we are already locked ?
48             return XmlUtil.createElement(document, XmlNetconfConstants.OK, Optional.absent());
49         }
50
51         // Not supported running lock
52         throw new DocumentedException("Unable to lock " + Datastore.running + " datastore",
53                 DocumentedException.ErrorType.APPLICATION,
54                 DocumentedException.ErrorTag.OPERATION_NOT_SUPPORTED, DocumentedException.ErrorSeverity.ERROR);
55     }
56
57     static Datastore extractTargetParameter(final XmlElement operationElement) throws DocumentedException {
58         final XmlElement targetElement = operationElement.getOnlyChildElementWithSameNamespace(TARGET_KEY);
59         final XmlElement targetChildNode = targetElement.getOnlyChildElementWithSameNamespace();
60
61         return Datastore.valueOf(targetChildNode.getName());
62     }
63
64     @Override
65     protected String getOperationName() {
66         return LOCK;
67     }
68 }