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