f9372c0cf3caceb44d6c6807fcf1d377d783425e
[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.config.facade.xml.Datastore;
13 import org.opendaylight.controller.config.util.xml.DocumentedException;
14 import org.opendaylight.controller.config.util.xml.XmlElement;
15 import org.opendaylight.controller.config.util.xml.XmlUtil;
16 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
17 import org.opendaylight.controller.netconf.util.mapping.AbstractLastNetconfOperation;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20 import org.w3c.dom.Document;
21 import org.w3c.dom.Element;
22
23 /**
24  * Simple Lock implementation that pretends to lock candidate datastore.
25  * Candidate datastore is allocated per session and is private so no real locking is needed (JMX is the only possible interference)
26  */
27 public class Lock extends AbstractLastNetconfOperation {
28
29     private static final Logger LOG = LoggerFactory.getLogger(Lock.class);
30
31     private static final String LOCK = "lock";
32     private static final String TARGET_KEY = "target";
33
34     public Lock(final String netconfSessionIdForReporting) {
35         super(netconfSessionIdForReporting);
36     }
37
38     @Override
39     protected Element handleWithNoSubsequentOperations(final Document document, final XmlElement operationElement) throws DocumentedException {
40         final Datastore targetDatastore = extractTargetParameter(operationElement);
41         if(targetDatastore == Datastore.candidate) {
42             // Since candidate datastore instances are allocated per session and not accessible anywhere else, no need to lock
43             LOG.debug("Locking {} datastore on session: {}", targetDatastore, getNetconfSessionIdForReporting());
44             // TODO should this fail if we are already locked ?
45             return XmlUtil.createElement(document, XmlNetconfConstants.OK, Optional.<String>absent());
46         }
47
48         // Not supported running lock
49         throw new DocumentedException("Unable to lock " + Datastore.running + " datastore", DocumentedException.ErrorType.application,
50                 DocumentedException.ErrorTag.operation_not_supported, DocumentedException.ErrorSeverity.error);
51     }
52
53     static Datastore extractTargetParameter(final XmlElement operationElement) throws DocumentedException {
54         final XmlElement targetElement = operationElement.getOnlyChildElementWithSameNamespace(TARGET_KEY);
55         final XmlElement targetChildNode = targetElement.getOnlyChildElementWithSameNamespace();
56
57         return Datastore.valueOf(targetChildNode.getName());
58     }
59
60     @Override
61     protected String getOperationName() {
62         return LOCK;
63     }
64 }