Integrate netconf-mapping-api into netconf-server
[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.server.api.operations.AbstractSingletonNetconfOperation;
14 import org.opendaylight.yangtools.yang.common.ErrorSeverity;
15 import org.opendaylight.yangtools.yang.common.ErrorTag;
16 import org.opendaylight.yangtools.yang.common.ErrorType;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19 import org.w3c.dom.Document;
20 import org.w3c.dom.Element;
21
22 /* FIXME Duplicated code
23    netconf/netconf/config-netconf-connector/src/main/java/org/opendaylight/netconf/
24    confignetconfconnector/operations/Lock.java
25  */
26 public class Lock extends AbstractSingletonNetconfOperation {
27     private static final Logger LOG = LoggerFactory.getLogger(Lock.class);
28     private static final String OPERATION_NAME = "lock";
29     private static final String TARGET_KEY = "target";
30
31     public Lock(final String netconfSessionIdForReporting) {
32         super(netconfSessionIdForReporting);
33     }
34
35     @Override
36     protected Element handleWithNoSubsequentOperations(final Document document, final XmlElement operationElement)
37             throws DocumentedException {
38         final Datastore targetDatastore = extractTargetParameter(operationElement);
39         if (targetDatastore == Datastore.candidate) {
40             LOG.debug("Locking candidate datastore on session: {}", getNetconfSessionIdForReporting());
41             return document.createElement(XmlNetconfConstants.OK);
42         }
43
44         throw new DocumentedException("Unable to lock " + targetDatastore + " datastore",
45                 ErrorType.APPLICATION, ErrorTag.OPERATION_NOT_SUPPORTED, 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 }