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