8c7465b02961197e4a1bc5c836d10d00c0441be5
[controller.git] / opendaylight / netconf / netconf-impl / src / main / java / org / opendaylight / controller / 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.controller.netconf.impl.mapping.operations;
10
11 import com.google.common.base.Optional;
12 import java.util.Collections;
13 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
14 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
15 import org.opendaylight.controller.netconf.util.mapping.AbstractSingletonNetconfOperation;
16 import org.opendaylight.controller.netconf.util.xml.XmlElement;
17 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
18 import org.w3c.dom.Document;
19 import org.w3c.dom.Element;
20
21 public class DefaultCloseSession extends AbstractSingletonNetconfOperation {
22     public static final String CLOSE_SESSION = "close-session";
23     private final AutoCloseable sessionResources;
24
25     public DefaultCloseSession(String netconfSessionIdForReporting, AutoCloseable sessionResources) {
26         super(netconfSessionIdForReporting);
27         this.sessionResources = sessionResources;
28     }
29
30     @Override
31     protected String getOperationName() {
32         return CLOSE_SESSION;
33     }
34
35     /**
36      * Close netconf operation router associated to this session, which in turn
37      * closes NetconfOperationServiceSnapshot with all NetconfOperationService
38      * instances
39      */
40     @Override
41     protected Element handleWithNoSubsequentOperations(Document document, XmlElement operationElement)
42             throws NetconfDocumentedException {
43         try {
44             sessionResources.close();
45         } catch (Exception e) {
46             throw new NetconfDocumentedException("Unable to properly close session "
47                     + getNetconfSessionIdForReporting(), NetconfDocumentedException.ErrorType.application,
48                     NetconfDocumentedException.ErrorTag.operation_failed,
49                     NetconfDocumentedException.ErrorSeverity.error, Collections.singletonMap(
50                         NetconfDocumentedException.ErrorSeverity.error.toString(), e.getMessage()));
51         }
52         return XmlUtil.createElement(document, XmlNetconfConstants.OK, Optional.<String>absent());
53     }
54 }