Merge "Resolve Bug:707 - ConfigPusher should wait for netconf-impl to register JMX...
[controller.git] / opendaylight / netconf / netconf-impl / src / main / java / org / opendaylight / controller / netconf / impl / mapping / operations / DefaultCloseSession.java
1 /*
2  * Copyright (c) 2013 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.impl.mapping.operations;
10
11 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
12 import org.opendaylight.controller.netconf.util.mapping.AbstractSingletonNetconfOperation;
13 import org.opendaylight.controller.netconf.util.xml.XmlElement;
14 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
15 import org.w3c.dom.Document;
16 import org.w3c.dom.Element;
17
18 import java.util.Collections;
19
20 public class DefaultCloseSession extends AbstractSingletonNetconfOperation {
21     public static final String CLOSE_SESSION = "close-session";
22     private final AutoCloseable sessionResources;
23
24     public DefaultCloseSession(String netconfSessionIdForReporting, AutoCloseable sessionResources) {
25         super(netconfSessionIdForReporting);
26         this.sessionResources = sessionResources;
27     }
28
29     @Override
30     protected String getOperationName() {
31         return CLOSE_SESSION;
32     }
33
34     /**
35      * Close netconf operation router associated to this session, which in turn
36      * closes NetconfOperationServiceSnapshot with all NetconfOperationService
37      * instances
38      */
39     @Override
40     protected Element handleWithNoSubsequentOperations(Document document, XmlElement operationElement)
41             throws NetconfDocumentedException {
42         try {
43             sessionResources.close();
44         } catch (Exception e) {
45             throw new NetconfDocumentedException("Unable to properly close session "
46                     + getNetconfSessionIdForReporting(), NetconfDocumentedException.ErrorType.application,
47                     NetconfDocumentedException.ErrorTag.operation_failed,
48                     NetconfDocumentedException.ErrorSeverity.error, Collections.singletonMap(
49                     NetconfDocumentedException.ErrorSeverity.error.toString(), e.getMessage()));
50         }
51         return document.createElement(XmlNetconfConstants.OK);
52     }
53 }