2 * Copyright (c) 2018 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
8 package org.opendaylight.netconf.mdsal.connector.ops;
10 import static java.nio.charset.StandardCharsets.UTF_8;
12 import com.google.common.base.Strings;
13 import java.io.IOException;
14 import java.io.InputStream;
15 import java.net.MalformedURLException;
17 import java.net.URLConnection;
18 import java.net.URLStreamHandler;
19 import java.util.Base64;
20 import java.util.Optional;
21 import org.opendaylight.netconf.api.DocumentedException;
22 import org.opendaylight.netconf.api.xml.XmlElement;
23 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
24 import org.opendaylight.netconf.api.xml.XmlUtil;
25 import org.opendaylight.netconf.util.mapping.AbstractSingletonNetconfOperation;
26 import org.opendaylight.yangtools.yang.common.ErrorSeverity;
27 import org.w3c.dom.Document;
28 import org.w3c.dom.Element;
29 import org.w3c.dom.NodeList;
30 import org.xml.sax.SAXException;
32 abstract class AbstractConfigOperation extends AbstractSingletonNetconfOperation {
33 static final String URL_KEY = "url";
34 static final String CONFIG_KEY = "config";
35 private static final int TIMEOUT_MS = 5000;
37 protected AbstractConfigOperation(final String netconfSessionIdForReporting) {
38 super(netconfSessionIdForReporting);
41 protected static NodeList getElementsByTagName(final XmlElement parent, final String key) throws
43 final Element domParent = parent.getDomElement();
44 final NodeList elementsByTagName;
46 if (Strings.isNullOrEmpty(domParent.getPrefix())) {
47 elementsByTagName = domParent.getElementsByTagName(key);
49 elementsByTagName = domParent.getElementsByTagNameNS(parent.getNamespace(), key);
52 return elementsByTagName;
55 static XmlElement getConfigElement(final XmlElement parent) throws DocumentedException {
56 final Optional<XmlElement> configElement = parent.getOnlyChildElementOptionally(CONFIG_KEY);
57 if (configElement.isPresent()) {
58 return configElement.get();
61 final Optional<XmlElement> urlElement = parent.getOnlyChildElementOptionally(URL_KEY);
62 if (urlElement.isEmpty()) {
63 throw new DocumentedException("Invalid RPC, neither <config> not <url> element is present",
64 DocumentedException.ErrorType.PROTOCOL,
65 DocumentedException.ErrorTag.MISSING_ELEMENT,
69 final Document document = getDocumentFromUrl(urlElement.get().getTextContent());
70 return XmlElement.fromDomElementWithExpected(document.getDocumentElement(), CONFIG_KEY,
71 XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
75 * Parses XML Document available at given URL.
77 * <p>JDK8 supports URL schemes that include http, https, file, and jar, but {@link URLStreamHandler}s for other
78 * protocols (e.g. ftp) may be available.
80 * @param url URL as defined in RFC 2396
81 * @see URL#URL(String, String, int, String)
83 private static Document getDocumentFromUrl(final String url) throws DocumentedException {
84 try (InputStream input = openConnection(new URL(url))) {
85 return XmlUtil.readXmlToDocument(input);
86 } catch (MalformedURLException e) {
87 throw new DocumentedException(url + " URL is invalid or unsupported", e,
88 DocumentedException.ErrorType.APPLICATION,
89 DocumentedException.ErrorTag.INVALID_VALUE,
91 } catch (IOException e) {
92 throw new DocumentedException("Could not open URL " + url, e,
93 DocumentedException.ErrorType.APPLICATION,
94 DocumentedException.ErrorTag.OPERATION_FAILED,
96 } catch (SAXException e) {
97 throw new DocumentedException("Could not parse XML at" + url, e,
98 DocumentedException.ErrorType.APPLICATION,
99 DocumentedException.ErrorTag.OPERATION_FAILED,
100 ErrorSeverity.ERROR);
104 private static InputStream openConnection(final URL url) throws IOException {
105 final URLConnection connection = url.openConnection();
106 connection.setConnectTimeout(TIMEOUT_MS);
107 connection.setReadTimeout(TIMEOUT_MS);
109 // Support Basic Authentication scheme, e.g. http://admin:admin@localhost:8000/config.conf
110 if (url.getUserInfo() != null) {
111 String basicAuth = "Basic " + Base64.getUrlEncoder().encodeToString(url.getUserInfo().getBytes(UTF_8));
112 connection.setRequestProperty("Authorization", basicAuth);
114 return connection.getInputStream();