Merge "Model dom-broker statistics"
[controller.git] / opendaylight / netconf / config-netconf-connector / src / main / java / org / opendaylight / controller / netconf / confignetconfconnector / operations / Lock.java
1 /*
2  * Copyright (c) 2014 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.confignetconfconnector.operations;
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 /**
25  * Simple Lock implementation that pretends to lock candidate datastore.
26  * Candidate datastore is allocated per session and is private so no real locking is needed (JMX is the only possible interference)
27  */
28 public class Lock extends AbstractLastNetconfOperation {
29
30     private static final Logger LOG = LoggerFactory.getLogger(Lock.class);
31
32     private static final String LOCK = "lock";
33     private static final String TARGET_KEY = "target";
34
35     public Lock(final String netconfSessionIdForReporting) {
36         super(netconfSessionIdForReporting);
37     }
38
39     @Override
40     protected Element handleWithNoSubsequentOperations(final Document document, final XmlElement operationElement) throws NetconfDocumentedException {
41         final Datastore targetDatastore = extractTargetParameter(operationElement);
42         if(targetDatastore == Datastore.candidate) {
43             // Since candidate datastore instances are allocated per session and not accessible anywhere else, no need to lock
44             LOG.debug("Locking {} datastore on session: {}", targetDatastore, getNetconfSessionIdForReporting());
45             // TODO should this fail if we are already locked ?
46             return XmlUtil.createElement(document, XmlNetconfConstants.OK, Optional.<String>absent());
47         }
48
49         // Not supported running lock
50         throw new NetconfDocumentedException("Unable to lock " + Datastore.running + " datastore", NetconfDocumentedException.ErrorType.application,
51                 NetconfDocumentedException.ErrorTag.operation_not_supported, NetconfDocumentedException.ErrorSeverity.error);
52     }
53
54     static Datastore extractTargetParameter(final XmlElement operationElement) throws NetconfDocumentedException {
55         final XmlElement targetChildNode;
56         try {
57             final XmlElement targetElement = operationElement.getOnlyChildElementWithSameNamespace(TARGET_KEY);
58             targetChildNode = targetElement.getOnlyChildElementWithSameNamespace();
59         } catch (final MissingNameSpaceException | UnexpectedNamespaceException e) {
60             LOG.trace("Can't get only child element with same namespace", e);
61             throw NetconfDocumentedException.wrap(e);
62         }
63
64         return Datastore.valueOf(targetChildNode.getName());
65     }
66
67     @Override
68     protected String getOperationName() {
69         return LOCK;
70     }
71 }