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