Increase UnitTest coverage/refactor for MDsal Netconf.
[controller.git] / opendaylight / netconf / mdsal-netconf-connector / src / main / java / org / opendaylight / controller / 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.controller.netconf.mdsal.connector.ops;
10
11 import com.google.common.base.Optional;
12 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
13 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
14 import org.opendaylight.controller.netconf.util.mapping.AbstractSingletonNetconfOperation;
15 import org.opendaylight.controller.netconf.util.xml.XmlElement;
16 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19 import org.w3c.dom.Document;
20 import org.w3c.dom.Element;
21
22 public class Lock extends AbstractSingletonNetconfOperation {
23
24     private static final Logger LOG = LoggerFactory.getLogger(Lock.class);
25
26     private static final String OPERATION_NAME = "lock";
27     private static final String TARGET_KEY = "target";
28
29     public Lock(final String netconfSessionIdForReporting) {
30         super(netconfSessionIdForReporting);
31     }
32
33     @Override
34     protected Element handleWithNoSubsequentOperations(final Document document, final XmlElement operationElement) throws NetconfDocumentedException {
35         final Datastore targetDatastore = extractTargetParameter(operationElement);
36         if (targetDatastore == Datastore.candidate) {
37             LOG.debug("Locking candidate datastore on session: {}", getNetconfSessionIdForReporting());
38             return XmlUtil.createElement(document, XmlNetconfConstants.OK, Optional.<String>absent());
39         }
40
41         throw new NetconfDocumentedException("Unable to lock " + targetDatastore + " datastore", NetconfDocumentedException.ErrorType.application,
42                 NetconfDocumentedException.ErrorTag.operation_not_supported, NetconfDocumentedException.ErrorSeverity.error);
43     }
44
45     static Datastore extractTargetParameter(final XmlElement operationElement) throws NetconfDocumentedException {
46         final XmlElement targetElement = operationElement.getOnlyChildElementWithSameNamespace(TARGET_KEY);
47         final XmlElement targetChildNode = targetElement.getOnlyChildElementWithSameNamespace();
48         return Datastore.valueOf(targetChildNode.getName());
49     }
50
51     @Override
52     protected String getOperationName() {
53         return OPERATION_NAME;
54     }
55
56 }