1873ae3aa0f18ee046d8253db297ae4691dae794
[netconf.git] / protocol / netconf-server / src / main / java / org / opendaylight / netconf / server / mapping / operations / DefaultStartExi.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.server.mapping.operations;
9
10 import org.opendaylight.netconf.api.DocumentedException;
11 import org.opendaylight.netconf.api.NamespaceURN;
12 import org.opendaylight.netconf.api.messages.NetconfMessage;
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.server.NetconfServerSession;
17 import org.opendaylight.netconf.server.api.operations.AbstractSingletonNetconfOperation;
18 import org.opendaylight.netconf.server.api.operations.NetconfOperationChainedExecution;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.SessionIdType;
20 import org.opendaylight.yangtools.yang.common.ErrorSeverity;
21 import org.opendaylight.yangtools.yang.common.ErrorTag;
22 import org.opendaylight.yangtools.yang.common.ErrorType;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import org.w3c.dom.Document;
26 import org.w3c.dom.Element;
27
28 public class DefaultStartExi extends AbstractSingletonNetconfOperation implements DefaultNetconfOperation {
29     public static final String START_EXI = "start-exi";
30
31     private static final Logger LOG = LoggerFactory.getLogger(DefaultStartExi.class);
32     private NetconfServerSession netconfSession;
33
34     public DefaultStartExi(final SessionIdType sessionId) {
35         super(sessionId);
36     }
37
38     @Override
39     public Document handle(final Document message,
40                            final NetconfOperationChainedExecution subsequentOperation) throws DocumentedException {
41         if (LOG.isDebugEnabled()) {
42             LOG.debug("Received start-exi message {} ", XmlUtil.toString(message));
43         }
44
45         try {
46             netconfSession.startExiCommunication(new NetconfMessage(message));
47         } catch (final IllegalArgumentException e) {
48             throw new DocumentedException("Failed to parse EXI parameters", e, ErrorType.PROTOCOL,
49                     ErrorTag.OPERATION_FAILED, ErrorSeverity.ERROR);
50         }
51
52         return super.handle(message, subsequentOperation);
53     }
54
55     @Override
56     protected Element handleWithNoSubsequentOperations(final Document document,
57                                                        final XmlElement operationElement) {
58         final Element getSchemaResult = document.createElementNS(NamespaceURN.BASE, XmlNetconfConstants.OK);
59         LOG.trace("{} operation successful", START_EXI);
60         return getSchemaResult;
61     }
62
63     @Override
64     protected String getOperationName() {
65         return START_EXI;
66     }
67
68     @Override
69     protected String getOperationNamespace() {
70         return NamespaceURN.EXI;
71     }
72
73     @Override
74     public void setNetconfSession(final NetconfServerSession netconfServerSession) {
75         netconfSession = netconfServerSession;
76     }
77 }