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 com.google.common.base.Preconditions;
11 import java.util.Collections;
12 import org.opendaylight.netconf.api.DocumentedException;
13 import org.opendaylight.netconf.api.xml.XmlElement;
14 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
15 import org.opendaylight.netconf.api.xml.XmlUtil;
16 import org.opendaylight.netconf.impl.NetconfServerSession;
17 import org.opendaylight.netconf.util.mapping.AbstractSingletonNetconfOperation;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20 import org.w3c.dom.Document;
21 import org.w3c.dom.Element;
23 public class DefaultCloseSession extends AbstractSingletonNetconfOperation implements DefaultNetconfOperation {
24 private static final Logger LOG = LoggerFactory.getLogger(DefaultCloseSession.class);
26 public static final String CLOSE_SESSION = "close-session";
28 private final AutoCloseable sessionResources;
29 private NetconfServerSession session;
31 public DefaultCloseSession(final String netconfSessionIdForReporting, final AutoCloseable sessionResources) {
32 super(netconfSessionIdForReporting);
33 this.sessionResources = sessionResources;
37 protected String getOperationName() {
42 * Close netconf operation router associated to this session, which in turn
43 * closes NetconfOperationServiceSnapshot with all NetconfOperationService
46 @SuppressWarnings("checkstyle:IllegalCatch")
48 protected Element handleWithNoSubsequentOperations(final Document document, final XmlElement operationElement)
49 throws DocumentedException {
51 sessionResources.close();
52 Preconditions.checkNotNull(session, "Session was not set").delayedClose();
53 LOG.info("Session {} closing", session.getSessionId());
54 } catch (final Exception e) {
55 throw new DocumentedException("Unable to properly close session "
56 + getNetconfSessionIdForReporting(), e, DocumentedException.ErrorType.APPLICATION,
57 DocumentedException.ErrorTag.OPERATION_FAILED,
58 DocumentedException.ErrorSeverity.ERROR, Collections.singletonMap(
59 DocumentedException.ErrorSeverity.ERROR.toString(), e.getMessage()));
61 return XmlUtil.createElement(document, XmlNetconfConstants.OK);
65 public void setNetconfSession(final NetconfServerSession netconfServerSession) {
66 this.session = netconfServerSession;