Merge "Eliminate XmlUtil.createElement(Document, String)"
[netconf.git] / netconf / mdsal-netconf-connector / src / main / java / org / opendaylight / netconf / mdsal / connector / ops / Lock.java
1 /*
2  * Copyright (c) 2015 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 package org.opendaylight.netconf.mdsal.connector.ops;
9
10 import org.opendaylight.netconf.api.DocumentedException;
11 import org.opendaylight.netconf.api.xml.XmlElement;
12 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
13 import org.opendaylight.netconf.util.mapping.AbstractSingletonNetconfOperation;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
16 import org.w3c.dom.Document;
17 import org.w3c.dom.Element;
18
19 /* FIXME Duplicated code
20    netconf/netconf/config-netconf-connector/src/main/java/org/opendaylight/netconf/
21    confignetconfconnector/operations/Lock.java
22  */
23 public class Lock extends AbstractSingletonNetconfOperation {
24
25     private static final Logger LOG = LoggerFactory.getLogger(Lock.class);
26
27     private static final String OPERATION_NAME = "lock";
28     private static final String TARGET_KEY = "target";
29
30     public Lock(final String netconfSessionIdForReporting) {
31         super(netconfSessionIdForReporting);
32     }
33
34     @Override
35     protected Element handleWithNoSubsequentOperations(final Document document, final XmlElement operationElement)
36             throws DocumentedException {
37         final Datastore targetDatastore = extractTargetParameter(operationElement);
38         if (targetDatastore == Datastore.candidate) {
39             LOG.debug("Locking candidate datastore on session: {}", getNetconfSessionIdForReporting());
40             return document.createElement(XmlNetconfConstants.OK);
41         }
42
43         throw new DocumentedException("Unable to lock " + targetDatastore + " datastore",
44                 DocumentedException.ErrorType.APPLICATION, DocumentedException.ErrorTag.OPERATION_NOT_SUPPORTED,
45                 DocumentedException.ErrorSeverity.ERROR);
46     }
47
48     static Datastore extractTargetParameter(final XmlElement operationElement) throws DocumentedException {
49         final XmlElement targetElement = operationElement.getOnlyChildElementWithSameNamespace(TARGET_KEY);
50         final XmlElement targetChildNode = targetElement.getOnlyChildElementWithSameNamespace();
51         return Datastore.valueOf(targetChildNode.getName());
52     }
53
54     @Override
55     protected String getOperationName() {
56         return OPERATION_NAME;
57     }
58
59 }