2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.netconf.server.mapping.operations;
10 import static java.util.Objects.requireNonNull;
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;
27 public class DefaultCloseSession extends AbstractSingletonNetconfOperation implements DefaultNetconfOperation {
28 private static final Logger LOG = LoggerFactory.getLogger(DefaultCloseSession.class);
30 public static final String CLOSE_SESSION = "close-session";
32 private final AutoCloseable sessionResources;
33 private NetconfServerSession session;
35 public DefaultCloseSession(final SessionIdType sessionId, final AutoCloseable sessionResources) {
37 this.sessionResources = sessionResources;
41 protected String getOperationName() {
46 * Close netconf operation router associated to this session, which in turn
47 * closes NetconfOperationServiceSnapshot with all NetconfOperationService
50 @SuppressWarnings("checkstyle:IllegalCatch")
52 protected Element handleWithNoSubsequentOperations(final Document document, final XmlElement operationElement)
53 throws DocumentedException {
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()));
64 return document.createElement(XmlNetconfConstants.OK);
68 public void setNetconfSession(final NetconfServerSession netconfServerSession) {
69 session = netconfServerSession;