2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.netconf.test.tool.rpc;
10 import com.google.common.base.Optional;
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;
24 public class SimulatedGetConfig extends AbstractLastNetconfOperation {
26 private final DataList storage;
27 private static final Logger LOG = LoggerFactory
28 .getLogger(SimulatedGetConfig.class);
30 public SimulatedGetConfig(final String netconfSessionIdForReporting, final DataList storage,
31 final Optional<File> initialConfigXMLFile) {
32 super(netconfSessionIdForReporting);
34 if (initialConfigXMLFile.isPresent()) {
35 LOG.info("File is present: {}", initialConfigXMLFile.get()
37 this.storage = loadInitialConfigXMLFile(initialConfigXMLFile.get());
39 this.storage = storage;
43 private static DataList loadInitialConfigXMLFile(final File file) {
44 LOG.info("Loading initial config xml file: {}", file.getName());
45 DataList configData = new DataList();
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());
54 } catch (SAXException e) {
55 LOG.info("SAXException {}", e.getMessage());
61 protected Element handleWithNoSubsequentOperations(final Document document, final XmlElement operationElement) {
62 final Element element = XmlUtil.createElement(document, XmlNetconfConstants.DATA_KEY);
64 for (final XmlElement e : storage.getConfigList()) {
65 final Element domElement = e.getDomElement();
66 element.appendChild(element.getOwnerDocument().importNode(domElement, true));
73 protected String getOperationName() {
74 return XmlNetconfConstants.GET_CONFIG;