Migrate netconf-console to Karaf 4 way
[netconf.git] / netconf / netconf-impl / src / main / java / org / opendaylight / netconf / impl / 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.impl.mapping.operations;
9
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;
22
23 public class DefaultCloseSession extends AbstractSingletonNetconfOperation implements DefaultNetconfOperation {
24     private static final Logger LOG = LoggerFactory.getLogger(DefaultCloseSession.class);
25
26     public static final String CLOSE_SESSION = "close-session";
27
28     private final AutoCloseable sessionResources;
29     private NetconfServerSession session;
30
31     public DefaultCloseSession(final String netconfSessionIdForReporting, final AutoCloseable sessionResources) {
32         super(netconfSessionIdForReporting);
33         this.sessionResources = sessionResources;
34     }
35
36     @Override
37     protected String getOperationName() {
38         return CLOSE_SESSION;
39     }
40
41     /**
42      * Close netconf operation router associated to this session, which in turn
43      * closes NetconfOperationServiceSnapshot with all NetconfOperationService
44      * instances.
45      */
46     @SuppressWarnings("checkstyle:IllegalCatch")
47     @Override
48     protected Element handleWithNoSubsequentOperations(final Document document, final XmlElement operationElement)
49             throws DocumentedException {
50         try {
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()));
60         }
61         return XmlUtil.createElement(document, XmlNetconfConstants.OK);
62     }
63
64     @Override
65     public void setNetconfSession(final NetconfServerSession netconfServerSession) {
66         this.session = netconfServerSession;
67     }
68 }