194411f2d6b2d47c313c89d954ce823fd59e5327
[transportpce.git] / tests / honeynode / 2.2.1 / 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
9 package org.opendaylight.netconf.impl.mapping.operations;
10
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import java.util.Collections;
14 import org.opendaylight.netconf.api.DocumentedException;
15 import org.opendaylight.netconf.api.xml.XmlElement;
16 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
17 import org.opendaylight.netconf.api.xml.XmlUtil;
18 import org.opendaylight.netconf.impl.NetconfServerSession;
19 import org.opendaylight.netconf.util.mapping.AbstractSingletonNetconfOperation;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22 import org.w3c.dom.Document;
23 import org.w3c.dom.Element;
24
25 public class DefaultCloseSession extends AbstractSingletonNetconfOperation implements DefaultNetconfOperation {
26     private static final Logger LOG = LoggerFactory.getLogger(DefaultCloseSession.class);
27
28     public static final String CLOSE_SESSION = "close-session";
29
30     private final AutoCloseable sessionResources;
31     private NetconfServerSession session;
32
33     public DefaultCloseSession(final String netconfSessionIdForReporting, final AutoCloseable sessionResources) {
34         super(netconfSessionIdForReporting);
35         this.sessionResources = sessionResources;
36     }
37
38     @Override
39     protected String getOperationName() {
40         return CLOSE_SESSION;
41     }
42
43     /**
44      * Close netconf operation router associated to this session, which in turn
45      * closes NetconfOperationServiceSnapshot with all NetconfOperationService
46      * instances.
47      */
48     @SuppressWarnings("checkstyle:IllegalCatch")
49     @Override
50     protected Element handleWithNoSubsequentOperations(final Document document, final XmlElement operationElement)
51             throws DocumentedException {
52         try {
53             sessionResources.close();
54             Preconditions.checkNotNull(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 "
58                     + getNetconfSessionIdForReporting(), e, DocumentedException.ErrorType.APPLICATION,
59                     DocumentedException.ErrorTag.OPERATION_FAILED,
60                     DocumentedException.ErrorSeverity.ERROR, Collections.singletonMap(
61                     DocumentedException.ErrorSeverity.ERROR.toString(), e.getMessage()));
62         }
63         return XmlUtil.createElement(document, XmlNetconfConstants.OK, Optional.absent());
64     }
65
66     @Override
67     public void setNetconfSession(final NetconfServerSession netconfServerSession) {
68         this.session = netconfServerSession;
69     }
70 }