f7b942125e532fb2da446e5cf46dd5a2bd0138a7
[netconf.git] / netconf / tools / netconf-testtool / src / main / java / org / opendaylight / netconf / test / tool / rpc / SimulatedGetConfig.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.netconf.test.tool.rpc;
10
11 import com.google.common.base.Optional;
12 import java.io.File;
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;
25
26 public class SimulatedGetConfig extends AbstractConfigNetconfOperation {
27
28     private final DataList storage;
29     private static final Logger LOG = LoggerFactory
30             .getLogger(SimulatedGetConfig.class);
31
32     public SimulatedGetConfig(final String netconfSessionIdForReporting, final DataList storage,
33                               final Optional<File> initialConfigXMLFile) {
34         super(null, netconfSessionIdForReporting);
35
36         if (initialConfigXMLFile.isPresent()) {
37             LOG.info("File is present: {}", initialConfigXMLFile.get()
38                     .getName());
39             this.storage = loadInitialConfigXMLFile(initialConfigXMLFile.get());
40         } else {
41             this.storage = storage;
42         }
43     }
44
45     private static DataList loadInitialConfigXMLFile(final File file) {
46         LOG.info("Loading initial config xml file: {}", file.getName());
47         DataList configData = new DataList();
48         try {
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());
55
56         } catch (SAXException e) {
57             LOG.info("SAXException {}", e.getMessage());
58         }
59         return configData;
60     }
61
62     @Override
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());
66
67         for (final XmlElement e : storage.getConfigList()) {
68             final Element domElement = e.getDomElement();
69             element.appendChild(element.getOwnerDocument().importNode(domElement, true));
70         }
71
72         return element;
73     }
74
75     @Override
76     protected String getOperationName() {
77         return XmlNetconfConstants.GET_CONFIG;
78     }
79 }