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