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