ff08feb10f7f635c714425ffa6d113f85b551a8f
[netconf.git] / netconf / netconf-impl / src / main / java / org / opendaylight / 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 package org.opendaylight.netconf.impl.mapping.operations;
9
10 import static java.util.Objects.requireNonNull;
11
12 import java.util.Map;
13 import org.opendaylight.netconf.api.DocumentedException;
14 import org.opendaylight.netconf.api.xml.XmlElement;
15 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
16 import org.opendaylight.netconf.impl.NetconfServerSession;
17 import org.opendaylight.netconf.util.mapping.AbstractSingletonNetconfOperation;
18 import org.opendaylight.yangtools.yang.common.ErrorSeverity;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21 import org.w3c.dom.Document;
22 import org.w3c.dom.Element;
23
24 public class DefaultCloseSession extends AbstractSingletonNetconfOperation implements DefaultNetconfOperation {
25     private static final Logger LOG = LoggerFactory.getLogger(DefaultCloseSession.class);
26
27     public static final String CLOSE_SESSION = "close-session";
28
29     private final AutoCloseable sessionResources;
30     private NetconfServerSession session;
31
32     public DefaultCloseSession(final String netconfSessionIdForReporting, final AutoCloseable sessionResources) {
33         super(netconfSessionIdForReporting);
34         this.sessionResources = sessionResources;
35     }
36
37     @Override
38     protected String getOperationName() {
39         return CLOSE_SESSION;
40     }
41
42     /**
43      * Close netconf operation router associated to this session, which in turn
44      * closes NetconfOperationServiceSnapshot with all NetconfOperationService
45      * instances.
46      */
47     @SuppressWarnings("checkstyle:IllegalCatch")
48     @Override
49     protected Element handleWithNoSubsequentOperations(final Document document, final XmlElement operationElement)
50             throws DocumentedException {
51         try {
52             sessionResources.close();
53             requireNonNull(session, "Session was not set").delayedClose();
54             LOG.info("Session {} closing", session.getSessionId());
55         } catch (final Exception e) {
56             throw new DocumentedException("Unable to properly close session "
57                     + getNetconfSessionIdForReporting(), e, DocumentedException.ErrorType.APPLICATION,
58                     DocumentedException.ErrorTag.OPERATION_FAILED,
59                     ErrorSeverity.ERROR, Map.of(ErrorSeverity.ERROR.toString(), e.getMessage()));
60         }
61         return document.createElement(XmlNetconfConstants.OK);
62     }
63
64     @Override
65     public void setNetconfSession(final NetconfServerSession netconfServerSession) {
66         this.session = netconfServerSession;
67     }
68 }