Merge "Eliminate XmlUtil.createElement(Document, String)"
[netconf.git] / netconf / mdsal-netconf-connector / src / main / java / org / opendaylight / netconf / mdsal / connector / ops / Unlock.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/confignetconfconnector/
21    operations/UnLock.java
22 */
23 public class Unlock extends AbstractSingletonNetconfOperation {
24
25     private static final Logger LOG = LoggerFactory.getLogger(Unlock.class);
26
27     private static final String OPERATION_NAME = "unlock";
28
29     public Unlock(final String netconfSessionIdForReporting) {
30         super(netconfSessionIdForReporting);
31     }
32
33     @Override
34     protected Element handleWithNoSubsequentOperations(final Document document, final XmlElement operationElement)
35             throws DocumentedException {
36         final Datastore targetDatastore = Lock.extractTargetParameter(operationElement);
37         if (targetDatastore == Datastore.candidate) {
38             LOG.debug("Unlocking candidate datastore on session: {}", getNetconfSessionIdForReporting());
39             return document.createElement(XmlNetconfConstants.OK);
40         }
41
42         throw new DocumentedException("Unable to unlock " + targetDatastore + " datastore",
43                 DocumentedException.ErrorType.APPLICATION, DocumentedException.ErrorTag.OPERATION_NOT_SUPPORTED,
44                 DocumentedException.ErrorSeverity.ERROR);
45     }
46
47     @Override
48     protected String getOperationName() {
49         return OPERATION_NAME;
50     }
51
52 }