2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.netconf.mdsal.connector.ops;
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;
24 public class Lock extends AbstractLastNetconfOperation{
26 private static final Logger LOG = LoggerFactory.getLogger(Lock.class);
28 private static final String OPERATION_NAME = "lock";
29 private static final String TARGET_KEY = "target";
31 public Lock(final String netconfSessionIdForReporting) {
32 super(netconfSessionIdForReporting);
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());
43 throw new NetconfDocumentedException("Unable to lock " + targetDatastore + " datastore", NetconfDocumentedException.ErrorType.application,
44 NetconfDocumentedException.ErrorTag.operation_not_supported, NetconfDocumentedException.ErrorSeverity.error);
47 static Datastore extractTargetParameter(final XmlElement operationElement) throws NetconfDocumentedException {
48 final XmlElement targetChildNode;
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);
57 return Datastore.valueOf(targetChildNode.getName());
61 protected String getOperationName() {
62 return OPERATION_NAME;