Use java.util.Optional
[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 package org.opendaylight.netconf.test.tool.rpc;
9
10 import java.io.File;
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;
23
24 public class SimulatedGetConfig extends AbstractLastNetconfOperation {
25
26     private final DataList storage;
27     private static final Logger LOG = LoggerFactory.getLogger(SimulatedGetConfig.class);
28
29     public SimulatedGetConfig(final String netconfSessionIdForReporting, final DataList storage,
30                               final Optional<File> initialConfigXMLFile) {
31         super(netconfSessionIdForReporting);
32
33         if (initialConfigXMLFile.isPresent()) {
34             LOG.info("File is present: {}", initialConfigXMLFile.get()
35                     .getName());
36             this.storage = loadInitialConfigXMLFile(initialConfigXMLFile.get());
37         } else {
38             this.storage = storage;
39         }
40     }
41
42     private static DataList loadInitialConfigXMLFile(final File file) {
43         LOG.info("Loading initial config xml file: {}", file.getName());
44         DataList configData = new DataList();
45         try {
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());
52
53         } catch (SAXException e) {
54             LOG.info("SAXException {}", e.getMessage());
55         }
56         return configData;
57     }
58
59     @Override
60     protected Element handleWithNoSubsequentOperations(final Document document, final XmlElement operationElement) {
61         final Element element = document.createElement(XmlNetconfConstants.DATA_KEY);
62
63         for (final XmlElement e : storage.getConfigList()) {
64             final Element domElement = e.getDomElement();
65             element.appendChild(element.getOwnerDocument().importNode(domElement, true));
66         }
67
68         return element;
69     }
70
71     @Override
72     protected String getOperationName() {
73         return XmlNetconfConstants.GET_CONFIG;
74     }
75 }