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;
14 import java.io.IOException;
15 import java.util.Collections;
16 import java.util.List;
18 import org.opendaylight.controller.config.util.xml.DocumentedException;
19 import org.opendaylight.controller.config.util.xml.XmlElement;
20 import org.opendaylight.controller.config.util.xml.XmlUtil;
21 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
22 import org.opendaylight.netconf.confignetconfconnector.operations.AbstractConfigNetconfOperation;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import org.w3c.dom.Document;
26 import org.w3c.dom.Element;
27 import org.xml.sax.SAXException;
29 public class SimulatedGetConfig extends AbstractConfigNetconfOperation {
31 private final DataList storage;
32 private static final Logger LOG = LoggerFactory
33 .getLogger(SimulatedGetConfig.class);
35 public SimulatedGetConfig(final String netconfSessionIdForReporting, final DataList storage, final Optional<File> initialConfigXMLFile) {
36 super(null, netconfSessionIdForReporting);
38 if(initialConfigXMLFile.isPresent()) {
39 LOG.info("File is present: {}", initialConfigXMLFile.get()
41 this.storage = loadInitialConfigXMLFile(initialConfigXMLFile.get());
44 this.storage = storage;
48 private final DataList loadInitialConfigXMLFile( final File file ) {
49 LOG.info("Loading initial config xml file: {}", file.getName());
50 DataList configData = new DataList();
51 List<XmlElement> xmlElementList = Collections.emptyList();
53 Element element = XmlUtil.readXmlToElement(file);
54 XmlElement xmlElement = XmlElement.fromDomElement(element);
55 xmlElementList = xmlElement.getChildElements();
56 configData.setConfigList(xmlElementList);
58 catch (IOException e) {
59 LOG.info("IO exception loading xml file: {} ", e.getMessage());
62 catch (SAXException e) {
63 LOG.info("SAXException {}", e.getMessage());
69 protected Element handleWithNoSubsequentOperations(final Document document, final XmlElement operationElement) throws DocumentedException {
70 final Element element = XmlUtil.createElement(document, XmlNetconfConstants.DATA_KEY, Optional.<String>absent());
72 for(final XmlElement e : storage.getConfigList()) {
73 final Element domElement = e.getDomElement();
74 element.appendChild(element.getOwnerDocument().importNode(domElement, true));
81 protected String getOperationName() {
82 return XmlNetconfConstants.GET_CONFIG;