Remove redundant code constructs
[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
9 package org.opendaylight.netconf.mdsal.connector.ops;
10
11 import com.google.common.base.Optional;
12 import org.opendaylight.netconf.api.DocumentedException;
13 import org.opendaylight.netconf.api.xml.XmlElement;
14 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
15 import org.opendaylight.netconf.api.xml.XmlUtil;
16 import org.opendaylight.netconf.util.mapping.AbstractSingletonNetconfOperation;
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
28     private static final Logger LOG = LoggerFactory.getLogger(Lock.class);
29
30     private static final String OPERATION_NAME = "lock";
31     private static final String TARGET_KEY = "target";
32
33     public Lock(final String netconfSessionIdForReporting) {
34         super(netconfSessionIdForReporting);
35     }
36
37     @Override
38     protected Element handleWithNoSubsequentOperations(final Document document, final XmlElement operationElement)
39             throws DocumentedException {
40         final Datastore targetDatastore = extractTargetParameter(operationElement);
41         if (targetDatastore == Datastore.candidate) {
42             LOG.debug("Locking candidate datastore on session: {}", getNetconfSessionIdForReporting());
43             return XmlUtil.createElement(document, XmlNetconfConstants.OK, Optional.absent());
44         }
45
46         throw new DocumentedException("Unable to lock " + targetDatastore + " datastore",
47                 DocumentedException.ErrorType.APPLICATION, DocumentedException.ErrorTag.OPERATION_NOT_SUPPORTED,
48                 DocumentedException.ErrorSeverity.ERROR);
49     }
50
51     static Datastore extractTargetParameter(final XmlElement operationElement) throws DocumentedException {
52         final XmlElement targetElement = operationElement.getOnlyChildElementWithSameNamespace(TARGET_KEY);
53         final XmlElement targetChildNode = targetElement.getOnlyChildElementWithSameNamespace();
54         return Datastore.valueOf(targetChildNode.getName());
55     }
56
57     @Override
58     protected String getOperationName() {
59         return OPERATION_NAME;
60     }
61
62 }