BUG-2633 - Netconf northbound mapping.
[netconf.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.exception.MissingNameSpaceException;
15 import org.opendaylight.controller.netconf.util.exception.UnexpectedNamespaceException;
16 import org.opendaylight.controller.netconf.util.mapping.AbstractLastNetconfOperation;
17 import org.opendaylight.controller.netconf.util.xml.XmlElement;
18 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21 import org.w3c.dom.Document;
22 import org.w3c.dom.Element;
23
24 public class Lock extends AbstractLastNetconfOperation{
25
26     private static final Logger LOG = LoggerFactory.getLogger(Lock.class);
27
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) throws NetconfDocumentedException {
37         final Datastore targetDatastore = extractTargetParameter(operationElement);
38         if (targetDatastore == Datastore.candidate) {
39             LOG.debug("Locking candidate datastore on session: {}", getNetconfSessionIdForReporting());
40             return XmlUtil.createElement(document, XmlNetconfConstants.OK, Optional.<String>absent());
41         }
42
43         throw new NetconfDocumentedException("Unable to lock " + targetDatastore + " datastore", NetconfDocumentedException.ErrorType.application,
44                 NetconfDocumentedException.ErrorTag.operation_not_supported, NetconfDocumentedException.ErrorSeverity.error);
45     }
46
47     static Datastore extractTargetParameter(final XmlElement operationElement) throws NetconfDocumentedException {
48         final XmlElement targetChildNode;
49         try {
50             final XmlElement targetElement = operationElement.getOnlyChildElementWithSameNamespace(TARGET_KEY);
51             targetChildNode = targetElement.getOnlyChildElementWithSameNamespace();
52         } catch (final MissingNameSpaceException | UnexpectedNamespaceException e) {
53             LOG.trace("Can't get only child element with same namespace", e);
54             throw NetconfDocumentedException.wrap(e);
55         }
56
57         return Datastore.valueOf(targetChildNode.getName());
58     }
59
60     @Override
61     protected String getOperationName() {
62         return OPERATION_NAME;
63     }
64 }