Add 'protocol-framework/' from commit '5d015c2c5f800d136406c15fcb64fd531d4ffc26'
[netconf.git] / netconf / tools / netconf-testtool / src / main / java / org / opendaylight / netconf / test / tool / rpc / SimulatedGetConfig.java
1 /*
2  * Copyright (c) 2014 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.test.tool.rpc;
10
11 import com.google.common.base.Optional;
12 import java.io.File;
13 import java.io.IOException;
14 import java.util.Collections;
15 import java.util.List;
16 import org.opendaylight.controller.config.util.xml.DocumentedException;
17 import org.opendaylight.controller.config.util.xml.XmlElement;
18 import org.opendaylight.controller.config.util.xml.XmlUtil;
19 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
20 import org.opendaylight.netconf.confignetconfconnector.operations.AbstractConfigNetconfOperation;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23 import org.w3c.dom.Document;
24 import org.w3c.dom.Element;
25 import org.xml.sax.SAXException;
26
27 public class SimulatedGetConfig extends AbstractConfigNetconfOperation {
28
29     private final DataList storage;
30     private static final Logger LOG = LoggerFactory
31             .getLogger(SimulatedGetConfig.class);
32
33     public SimulatedGetConfig(final String netconfSessionIdForReporting, final DataList storage,
34                               final Optional<File> initialConfigXMLFile) {
35         super(null, netconfSessionIdForReporting);
36
37         if (initialConfigXMLFile.isPresent()) {
38             LOG.info("File is present: {}", initialConfigXMLFile.get()
39                     .getName());
40             this.storage = loadInitialConfigXMLFile(initialConfigXMLFile.get());
41         } else {
42             this.storage = storage;
43         }
44     }
45
46     private DataList loadInitialConfigXMLFile(final File file) {
47         LOG.info("Loading initial config xml file: {}", file.getName());
48         DataList configData = new DataList();
49         List<XmlElement> xmlElementList = Collections.emptyList();
50         try {
51             Element element = XmlUtil.readXmlToElement(file);
52             XmlElement xmlElement = XmlElement.fromDomElement(element);
53             xmlElementList = xmlElement.getChildElements();
54             configData.setConfigList(xmlElementList);
55         } catch (IOException e) {
56             LOG.info("IO exception loading xml file: {} ", e.getMessage());
57
58         } catch (SAXException e) {
59             LOG.info("SAXException {}", e.getMessage());
60         }
61         return configData;
62     }
63
64     @Override
65     protected Element handleWithNoSubsequentOperations(final Document document, final XmlElement operationElement)
66             throws DocumentedException {
67         final Element element = XmlUtil.createElement(document, XmlNetconfConstants.DATA_KEY, Optional.absent());
68
69         for (final XmlElement e : storage.getConfigList()) {
70             final Element domElement = e.getDomElement();
71             element.appendChild(element.getOwnerDocument().importNode(domElement, true));
72         }
73
74         return element;
75     }
76
77     @Override
78     protected String getOperationName() {
79         return XmlNetconfConstants.GET_CONFIG;
80     }
81 }