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;
11 import java.io.IOException;
12 import java.util.List;
13 import java.util.Optional;
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.getLogger(SimulatedGetConfig.class);
29 public SimulatedGetConfig(final String netconfSessionIdForReporting, final DataList storage,
30 final Optional<File> initialConfigXMLFile) {
31 super(netconfSessionIdForReporting);
33 if (initialConfigXMLFile.isPresent()) {
34 LOG.info("File is present: {}", initialConfigXMLFile.get()
36 this.storage = loadInitialConfigXMLFile(initialConfigXMLFile.get());
38 this.storage = storage;
42 private static DataList loadInitialConfigXMLFile(final File file) {
43 LOG.info("Loading initial config xml file: {}", file.getName());
44 DataList configData = new DataList();
46 Element element = XmlUtil.readXmlToElement(file);
47 XmlElement xmlElement = XmlElement.fromDomElement(element);
48 List<XmlElement> xmlElementList = xmlElement.getChildElements();
49 configData.setConfigList(xmlElementList);
50 } catch (IOException e) {
51 LOG.info("IO exception loading xml file: {} ", e.getMessage());
53 } catch (SAXException e) {
54 LOG.info("SAXException {}", e.getMessage());
60 protected Element handleWithNoSubsequentOperations(final Document document, final XmlElement operationElement) {
61 final Element element = document.createElement(XmlNetconfConstants.DATA_KEY);
63 for (final XmlElement e : storage.getConfigList()) {
64 final Element domElement = e.getDomElement();
65 element.appendChild(element.getOwnerDocument().importNode(domElement, true));
72 protected String getOperationName() {
73 return XmlNetconfConstants.GET_CONFIG;