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
9 package org.opendaylight.netconf.test.tool.rpc;
11 import com.google.common.base.Optional;
13 import java.io.IOException;
14 import java.util.List;
15 import org.opendaylight.controller.config.util.xml.DocumentedException;
16 import org.opendaylight.controller.config.util.xml.XmlElement;
17 import org.opendaylight.controller.config.util.xml.XmlUtil;
18 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
19 import org.opendaylight.netconf.confignetconfconnector.operations.AbstractConfigNetconfOperation;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22 import org.w3c.dom.Document;
23 import org.w3c.dom.Element;
24 import org.xml.sax.SAXException;
26 public class SimulatedGetConfig extends AbstractConfigNetconfOperation {
28 private final DataList storage;
29 private static final Logger LOG = LoggerFactory
30 .getLogger(SimulatedGetConfig.class);
32 public SimulatedGetConfig(final String netconfSessionIdForReporting, final DataList storage,
33 final Optional<File> initialConfigXMLFile) {
34 super(null, netconfSessionIdForReporting);
36 if (initialConfigXMLFile.isPresent()) {
37 LOG.info("File is present: {}", initialConfigXMLFile.get()
39 this.storage = loadInitialConfigXMLFile(initialConfigXMLFile.get());
41 this.storage = storage;
45 private static DataList loadInitialConfigXMLFile(final File file) {
46 LOG.info("Loading initial config xml file: {}", file.getName());
47 DataList configData = new DataList();
49 Element element = XmlUtil.readXmlToElement(file);
50 XmlElement xmlElement = XmlElement.fromDomElement(element);
51 List<XmlElement> xmlElementList = xmlElement.getChildElements();
52 configData.setConfigList(xmlElementList);
53 } catch (IOException e) {
54 LOG.info("IO exception loading xml file: {} ", e.getMessage());
56 } catch (SAXException e) {
57 LOG.info("SAXException {}", e.getMessage());
63 protected Element handleWithNoSubsequentOperations(final Document document, final XmlElement operationElement)
64 throws DocumentedException {
65 final Element element = XmlUtil.createElement(document, XmlNetconfConstants.DATA_KEY, Optional.absent());
67 for (final XmlElement e : storage.getConfigList()) {
68 final Element domElement = e.getDomElement();
69 element.appendChild(element.getOwnerDocument().importNode(domElement, true));
76 protected String getOperationName() {
77 return XmlNetconfConstants.GET_CONFIG;