blob: 5b2374fe707dcb399b53b07b17016ccb04f45480 [file] [log] [blame]
Andrea Campanellad8d92db2016-01-14 16:24:41 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Andrea Campanellad8d92db2016-01-14 16:24:41 -08003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Andrea Campanella238d96e2016-01-20 11:52:02 -080017package org.onosproject.drivers.utilities;
Andrea Campanellad8d92db2016-01-14 16:24:41 -080018
Andrea Campanellad8d92db2016-01-14 16:24:41 -080019import org.apache.commons.configuration.ConfigurationException;
20import org.apache.commons.configuration.HierarchicalConfiguration;
21import org.apache.commons.configuration.XMLConfiguration;
22import org.apache.commons.configuration.tree.ConfigurationNode;
23import org.onlab.packet.IpAddress;
Andrea Campanellad8d92db2016-01-14 16:24:41 -080024import org.onosproject.net.behaviour.ControllerInfo;
Andrea Campanellad8d92db2016-01-14 16:24:41 -080025import org.slf4j.Logger;
26import org.slf4j.LoggerFactory;
27
Andrea Campanellaaf1fa392018-06-26 11:16:51 +020028import javax.xml.parsers.DocumentBuilderFactory;
29import javax.xml.parsers.ParserConfigurationException;
Yuta HIGUCHI0184a7b2017-03-31 13:13:58 -070030import java.io.ByteArrayInputStream;
Andrea Campanellad8d92db2016-01-14 16:24:41 -080031import java.io.InputStream;
32import java.io.StringWriter;
Yuta HIGUCHI0184a7b2017-03-31 13:13:58 -070033import java.nio.charset.StandardCharsets;
Andrea Campanellad8d92db2016-01-14 16:24:41 -080034import java.util.ArrayList;
35import java.util.List;
Aaron Kruglikov17b4c852016-01-15 16:37:04 -080036
Andrea Campanellad8d92db2016-01-14 16:24:41 -080037/**
38 * Parser for Netconf XML configurations and replys.
39 */
40public final class XmlConfigParser {
41 public static final Logger log = LoggerFactory
42 .getLogger(XmlConfigParser.class);
43
Andrea Campanellaaf1fa392018-06-26 11:16:51 +020044 private static final String DISALLOW_DTD_FEATURE = "http://apache.org/xml/features/disallow-doctype-decl";
45 private static final String DISALLOW_EXTERNAL_DTD =
46 "http://apache.org/xml/features/nonvalidating/load-external-dtd";
47
Andrea Campanellad8d92db2016-01-14 16:24:41 -080048 private XmlConfigParser() {
49 //not called, preventing any allocation
50 }
51
Michael Enrico584eebd2021-07-22 08:04:13 +000052 private static HierarchicalConfiguration loadXmlCommonPart(XMLConfiguration cfg, InputStream xmlStream)
53 throws ParserConfigurationException, ConfigurationException {
54
55 DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance();
56 //Disabling DTDs in order to avoid XXE xml-based attacks.
57 disableFeature(dbfactory, DISALLOW_DTD_FEATURE);
58 disableFeature(dbfactory, DISALLOW_EXTERNAL_DTD);
59 dbfactory.setXIncludeAware(false);
60 dbfactory.setExpandEntityReferences(false);
61 cfg.setDocumentBuilder(dbfactory.newDocumentBuilder());
62 cfg.load(xmlStream);
63 return cfg;
64 }
Andrea Campanellad8d92db2016-01-14 16:24:41 -080065
66 public static HierarchicalConfiguration loadXml(InputStream xmlStream) {
Andrea Campanellad8d92db2016-01-14 16:24:41 -080067 try {
Andrea Campanellaaf1fa392018-06-26 11:16:51 +020068 XMLConfiguration cfg = new XMLConfiguration();
Michael Enrico584eebd2021-07-22 08:04:13 +000069 return loadXmlCommonPart(cfg, xmlStream);
70 } catch (ConfigurationException | ParserConfigurationException e) {
71 throw new IllegalArgumentException("Cannot load xml from Stream", e);
72 }
73 }
74
75 public static HierarchicalConfiguration loadXml(InputStream xmlStream, boolean withDelim) {
76 try {
77 XMLConfiguration cfg = new XMLConfiguration();
78 //Optionally disable default comma-based parsing on config values to allow JSON strings to be used therein
79 if (!withDelim) {
80 cfg.setDelimiterParsingDisabled(true);
81 }
82 return loadXmlCommonPart(cfg, xmlStream);
Andrea Campanellaaf1fa392018-06-26 11:16:51 +020083 } catch (ConfigurationException | ParserConfigurationException e) {
Andrea Campanellad8d92db2016-01-14 16:24:41 -080084 throw new IllegalArgumentException("Cannot load xml from Stream", e);
85 }
86 }
87
Yuta HIGUCHI0184a7b2017-03-31 13:13:58 -070088 public static HierarchicalConfiguration loadXmlString(String xmlStr) {
89 return loadXml(new ByteArrayInputStream(xmlStr.getBytes(StandardCharsets.UTF_8)));
90 }
91
Michael Enrico584eebd2021-07-22 08:04:13 +000092 public static HierarchicalConfiguration loadXmlString(String xmlStr, boolean withDelim) {
93 return loadXml(new ByteArrayInputStream(xmlStr.getBytes(StandardCharsets.UTF_8)), withDelim);
94 }
95
Andrea Campanellad8d92db2016-01-14 16:24:41 -080096 public static List<ControllerInfo> parseStreamControllers(HierarchicalConfiguration cfg) {
97 List<ControllerInfo> controllers = new ArrayList<>();
98 List<HierarchicalConfiguration> fields =
99 cfg.configurationsAt("data.capable-switch." +
Andrea Campanellaaf1fa392018-06-26 11:16:51 +0200100 "logical-switches." +
101 "switch.controllers.controller");
Andrea Campanellad8d92db2016-01-14 16:24:41 -0800102 for (HierarchicalConfiguration sub : fields) {
103 controllers.add(new ControllerInfo(
104 IpAddress.valueOf(sub.getString("ip-address")),
105 Integer.parseInt(sub.getString("port")),
106 sub.getString("protocol")));
107 }
108 return controllers;
109 }
110
Aaron Kruglikov17b4c852016-01-15 16:37:04 -0800111
112 protected static String parseSwitchId(HierarchicalConfiguration cfg) {
Andrea Campanellad8d92db2016-01-14 16:24:41 -0800113 HierarchicalConfiguration field =
114 cfg.configurationAt("data.capable-switch." +
Andrea Campanellaaf1fa392018-06-26 11:16:51 +0200115 "logical-switches." +
116 "switch");
Andrea Campanellad8d92db2016-01-14 16:24:41 -0800117 return field.getProperty("id").toString();
118 }
119
120 public static String parseCapableSwitchId(HierarchicalConfiguration cfg) {
121 HierarchicalConfiguration field =
122 cfg.configurationAt("data.capable-switch");
123 return field.getProperty("id").toString();
124 }
125
126 public static String createControllersConfig(HierarchicalConfiguration cfg,
127 HierarchicalConfiguration actualCfg,
128 String target, String netconfOperation,
129 String controllerOperation,
130 List<ControllerInfo> controllers) {
131 //cfg.getKeys().forEachRemaining(key -> System.out.println(key));
132 cfg.setProperty("edit-config.target", target);
133 cfg.setProperty("edit-config.default-operation", netconfOperation);
134 cfg.setProperty("edit-config.config.capable-switch.id",
Andrea Campanellaaf1fa392018-06-26 11:16:51 +0200135 parseCapableSwitchId(actualCfg));
Andrea Campanellad8d92db2016-01-14 16:24:41 -0800136 cfg.setProperty("edit-config.config.capable-switch." +
Andrea Campanellaaf1fa392018-06-26 11:16:51 +0200137 "logical-switches.switch.id", parseSwitchId(actualCfg));
Andrea Campanellad8d92db2016-01-14 16:24:41 -0800138 List<ConfigurationNode> newControllers = new ArrayList<>();
139 for (ControllerInfo ci : controllers) {
140 XMLConfiguration controller = new XMLConfiguration();
141 controller.setRoot(new HierarchicalConfiguration.Node("controller"));
142 String id = ci.type() + ":" + ci.ip() + ":" + ci.port();
143 controller.setProperty("id", id);
144 controller.setProperty("ip-address", ci.ip());
145 controller.setProperty("port", ci.port());
146 controller.setProperty("protocol", ci.type());
147 newControllers.add(controller.getRootNode());
148 }
149 cfg.addNodes("edit-config.config.capable-switch.logical-switches." +
Andrea Campanellaaf1fa392018-06-26 11:16:51 +0200150 "switch.controllers", newControllers);
Andrea Campanellad8d92db2016-01-14 16:24:41 -0800151 XMLConfiguration editcfg = (XMLConfiguration) cfg;
152 StringWriter stringWriter = new StringWriter();
153 try {
154 editcfg.save(stringWriter);
155 } catch (ConfigurationException e) {
156 log.error("createControllersConfig()", e);
157 }
158 String s = stringWriter.toString()
159 .replaceAll("<controller>",
Andrea Campanellaaf1fa392018-06-26 11:16:51 +0200160 "<controller nc:operation=\"" + controllerOperation + "\">");
Andrea Campanellad8d92db2016-01-14 16:24:41 -0800161 s = s.replace("<target>" + target + "</target>",
Andrea Campanellaaf1fa392018-06-26 11:16:51 +0200162 "<target><" + target + "/></target>");
Andrea Campanellad8d92db2016-01-14 16:24:41 -0800163 return s;
164
165 }
166
Andrea Campanellad8d92db2016-01-14 16:24:41 -0800167 //TODO implement mor methods for parsing configuration when you need them
Andreas Papazois1dff77c2016-02-16 16:27:33 +0200168
169 /**
170 * Parses a config reply and returns the result.
Andrea Campanellaaf1fa392018-06-26 11:16:51 +0200171 *
Andreas Papazois1dff77c2016-02-16 16:27:33 +0200172 * @param reply a tree-like source
173 * @return the configuration result
174 */
175 public static boolean configSuccess(HierarchicalConfiguration reply) {
176 if (reply != null) {
177 if (reply.containsKey("ok")) {
178 return true;
179 }
180 }
181 return false;
182 }
Andrea Campanellaaf1fa392018-06-26 11:16:51 +0200183
184 private static void disableFeature(DocumentBuilderFactory dbfactory, String feature) {
185 try {
186 dbfactory.setFeature(feature, true);
187 } catch (ParserConfigurationException e) {
188 // This should catch a failed setFeature feature
189 log.info("ParserConfigurationException was thrown. The feature '" +
190 feature + "' is probably not supported by your XML processor.");
191 }
192 }
Andrea Campanellad8d92db2016-01-14 16:24:41 -0800193}