421122f32d26237daa9ebbd378da1c75497ade1d
[netconf.git] / protocol / netconf-server / src / main / java / org / opendaylight / netconf / server / 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.server.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.server.NetconfServerSession;
17 import org.opendaylight.netconf.server.api.operations.AbstractSingletonNetconfOperation;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.SessionIdType;
19 import org.opendaylight.yangtools.yang.common.ErrorSeverity;
20 import org.opendaylight.yangtools.yang.common.ErrorTag;
21 import org.opendaylight.yangtools.yang.common.ErrorType;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24 import org.w3c.dom.Document;
25 import org.w3c.dom.Element;
26
27 public class DefaultCloseSession extends AbstractSingletonNetconfOperation implements DefaultNetconfOperation {
28     private static final Logger LOG = LoggerFactory.getLogger(DefaultCloseSession.class);
29
30     public static final String CLOSE_SESSION = "close-session";
31
32     private final AutoCloseable sessionResources;
33     private NetconfServerSession session;
34
35     public DefaultCloseSession(final SessionIdType sessionId, final AutoCloseable sessionResources) {
36         super(sessionId);
37         this.sessionResources = sessionResources;
38     }
39
40     @Override
41     protected String getOperationName() {
42         return CLOSE_SESSION;
43     }
44
45     /**
46      * Close netconf operation router associated to this session, which in turn
47      * closes NetconfOperationServiceSnapshot with all NetconfOperationService
48      * instances.
49      */
50     @SuppressWarnings("checkstyle:IllegalCatch")
51     @Override
52     protected Element handleWithNoSubsequentOperations(final Document document, final XmlElement operationElement)
53             throws DocumentedException {
54         try {
55             sessionResources.close();
56             requireNonNull(session, "Session was not set").delayedClose();
57             LOG.info("Session {} closing", session.sessionId().getValue());
58         } catch (final Exception e) {
59             throw new DocumentedException("Unable to properly close session " + sessionId().getValue(), e,
60                     ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED, ErrorSeverity.ERROR,
61                     // FIXME: i.e. <error>exception.toString()</error>? That looks wrong on a few levels.
62                     Map.of(ErrorSeverity.ERROR.elementBody(), e.getMessage()));
63         }
64         return document.createElement(XmlNetconfConstants.OK);
65     }
66
67     @Override
68     public void setNetconfSession(final NetconfServerSession netconfServerSession) {
69         session = netconfServerSession;
70     }
71 }