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.impl.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.impl.NetconfServerSession;
17 import org.opendaylight.netconf.util.mapping.AbstractSingletonNetconfOperation;
18 import org.opendaylight.yangtools.yang.common.ErrorSeverity;
19 import org.opendaylight.yangtools.yang.common.ErrorType;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22 import org.w3c.dom.Document;
23 import org.w3c.dom.Element;
25 public class DefaultCloseSession extends AbstractSingletonNetconfOperation implements DefaultNetconfOperation {
26 private static final Logger LOG = LoggerFactory.getLogger(DefaultCloseSession.class);
28 public static final String CLOSE_SESSION = "close-session";
30 private final AutoCloseable sessionResources;
31 private NetconfServerSession session;
33 public DefaultCloseSession(final String netconfSessionIdForReporting, final AutoCloseable sessionResources) {
34 super(netconfSessionIdForReporting);
35 this.sessionResources = sessionResources;
39 protected String getOperationName() {
44 * Close netconf operation router associated to this session, which in turn
45 * closes NetconfOperationServiceSnapshot with all NetconfOperationService
48 @SuppressWarnings("checkstyle:IllegalCatch")
50 protected Element handleWithNoSubsequentOperations(final Document document, final XmlElement operationElement)
51 throws DocumentedException {
53 sessionResources.close();
54 requireNonNull(session, "Session was not set").delayedClose();
55 LOG.info("Session {} closing", session.getSessionId());
56 } catch (final Exception e) {
57 throw new DocumentedException("Unable to properly close session " + getNetconfSessionIdForReporting(), e,
58 ErrorType.APPLICATION, DocumentedException.ErrorTag.OPERATION_FAILED, ErrorSeverity.ERROR,
59 Map.of(ErrorSeverity.ERROR.toString(), e.getMessage()));
61 return document.createElement(XmlNetconfConstants.OK);
65 public void setNetconfSession(final NetconfServerSession netconfServerSession) {
66 this.session = netconfServerSession;