Fix netconf-connector-config groupId
[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
13 import java.io.File;
14 import java.io.IOException;
15 import java.util.Collections;
16 import java.util.List;
17
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;
28
29 public class SimulatedGetConfig extends AbstractConfigNetconfOperation {
30
31     private final DataList storage;
32     private static final Logger LOG = LoggerFactory
33             .getLogger(SimulatedGetConfig.class);
34
35     public SimulatedGetConfig(final String netconfSessionIdForReporting, final DataList storage, final Optional<File> initialConfigXMLFile) {
36         super(null, netconfSessionIdForReporting);
37
38         if(initialConfigXMLFile.isPresent()) {
39             LOG.info("File is present: {}", initialConfigXMLFile.get()
40                     .getName());
41             this.storage = loadInitialConfigXMLFile(initialConfigXMLFile.get());
42         }
43         else {
44             this.storage = storage;
45         }
46     }
47
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();
52         try {
53             Element element = XmlUtil.readXmlToElement(file);
54             XmlElement xmlElement = XmlElement.fromDomElement(element);
55             xmlElementList = xmlElement.getChildElements();
56             configData.setConfigList(xmlElementList);
57         }
58         catch (IOException e) {
59             LOG.info("IO exception loading xml file: {} ", e.getMessage());
60
61         }
62         catch (SAXException e) {
63             LOG.info("SAXException {}", e.getMessage());
64         }
65         return configData;
66     }
67
68     @Override
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());
71
72         for(final XmlElement e : storage.getConfigList()) {
73             final Element domElement = e.getDomElement();
74             element.appendChild(element.getOwnerDocument().importNode(domElement, true));
75         }
76
77         return element;
78     }
79
80     @Override
81     protected String getOperationName() {
82         return XmlNetconfConstants.GET_CONFIG;
83     }
84 }