Implement basic locking for candidate in config-netconf-connector
[controller.git] / opendaylight / netconf / config-netconf-connector / src / main / java / org / opendaylight / controller / netconf / confignetconfconnector / operations / UnLock.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.mapping.AbstractLastNetconfOperation;
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 /**
23  * Simple unlock implementation that pretends to unlock candidate datastore.
24  * Candidate datastore is allocated per session and is private so no real locking is needed (JMX is the only possible interference)
25  */
26 public class UnLock extends AbstractLastNetconfOperation {
27
28     private static final Logger LOG = LoggerFactory.getLogger(UnLock.class);
29
30     private static final String UNLOCK = "unlock";
31
32     public UnLock(final String netconfSessionIdForReporting) {
33         super(netconfSessionIdForReporting);
34     }
35
36     @Override
37     protected Element handleWithNoSubsequentOperations(final Document document, final XmlElement operationElement) throws NetconfDocumentedException {
38         final Datastore targetDatastore = Lock.extractTargetParameter(operationElement);
39         if(targetDatastore == Datastore.candidate) {
40             // Since candidate datastore instances are allocated per session and not accessible anywhere else, no need to lock
41             LOG.debug("Unlocking {} datastore on session: {}", targetDatastore, getNetconfSessionIdForReporting());
42             // TODO this should fail if we are not locked
43             return XmlUtil.createElement(document, XmlNetconfConstants.OK, Optional.<String>absent());
44         }
45
46         // Not supported running lock
47         throw new NetconfDocumentedException("Unable to unlock " + Datastore.running + " datastore", NetconfDocumentedException.ErrorType.application,
48                 NetconfDocumentedException.ErrorTag.operation_not_supported, NetconfDocumentedException.ErrorSeverity.error);
49     }
50
51     @Override
52     protected String getOperationName() {
53         return UNLOCK;
54     }
55 }