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.Collections;
15 import java.util.List;
16 import org.opendaylight.controller.config.util.xml.DocumentedException;
17 import org.opendaylight.controller.config.util.xml.XmlElement;
18 import org.opendaylight.controller.config.util.xml.XmlUtil;
19 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
20 import org.opendaylight.netconf.confignetconfconnector.operations.AbstractConfigNetconfOperation;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23 import org.w3c.dom.Document;
24 import org.w3c.dom.Element;
25 import org.xml.sax.SAXException;
27 public class SimulatedGetConfig extends AbstractConfigNetconfOperation {
29 private final DataList storage;
30 private static final Logger LOG = LoggerFactory
31 .getLogger(SimulatedGetConfig.class);
33 public SimulatedGetConfig(final String netconfSessionIdForReporting, final DataList storage, 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());
42 this.storage = storage;
46 private final DataList loadInitialConfigXMLFile( final File file ) {
47 LOG.info("Loading initial config xml file: {}", file.getName());
48 DataList configData = new DataList();
49 List<XmlElement> xmlElementList = Collections.emptyList();
51 Element element = XmlUtil.readXmlToElement(file);
52 XmlElement xmlElement = XmlElement.fromDomElement(element);
53 xmlElementList = xmlElement.getChildElements();
54 configData.setConfigList(xmlElementList);
56 catch (IOException e) {
57 LOG.info("IO exception loading xml file: {} ", e.getMessage());
60 catch (SAXException e) {
61 LOG.info("SAXException {}", e.getMessage());
67 protected Element handleWithNoSubsequentOperations(final Document document, final XmlElement operationElement) throws DocumentedException {
68 final Element element = XmlUtil.createElement(document, XmlNetconfConstants.DATA_KEY, Optional.<String>absent());
70 for(final XmlElement e : storage.getConfigList()) {
71 final Element domElement = e.getDomElement();
72 element.appendChild(element.getOwnerDocument().importNode(domElement, true));
79 protected String getOperationName() {
80 return XmlNetconfConstants.GET_CONFIG;