Andrea Campanella | d8d92db | 2016-01-14 16:24:41 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Andrea Campanella | d8d92db | 2016-01-14 16:24:41 -0800 | [diff] [blame] | 3 | * |
| 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 Campanella | 238d96e | 2016-01-20 11:52:02 -0800 | [diff] [blame] | 17 | package org.onosproject.drivers.utilities; |
Andrea Campanella | d8d92db | 2016-01-14 16:24:41 -0800 | [diff] [blame] | 18 | |
Andrea Campanella | d8d92db | 2016-01-14 16:24:41 -0800 | [diff] [blame] | 19 | import org.apache.commons.configuration.ConfigurationException; |
| 20 | import org.apache.commons.configuration.HierarchicalConfiguration; |
| 21 | import org.apache.commons.configuration.XMLConfiguration; |
| 22 | import org.apache.commons.configuration.tree.ConfigurationNode; |
| 23 | import org.onlab.packet.IpAddress; |
Andrea Campanella | d8d92db | 2016-01-14 16:24:41 -0800 | [diff] [blame] | 24 | import org.onosproject.net.behaviour.ControllerInfo; |
Andrea Campanella | d8d92db | 2016-01-14 16:24:41 -0800 | [diff] [blame] | 25 | import org.slf4j.Logger; |
| 26 | import org.slf4j.LoggerFactory; |
| 27 | |
Andrea Campanella | af1fa39 | 2018-06-26 11:16:51 +0200 | [diff] [blame] | 28 | import javax.xml.parsers.DocumentBuilderFactory; |
| 29 | import javax.xml.parsers.ParserConfigurationException; |
Yuta HIGUCHI | 0184a7b | 2017-03-31 13:13:58 -0700 | [diff] [blame] | 30 | import java.io.ByteArrayInputStream; |
Andrea Campanella | d8d92db | 2016-01-14 16:24:41 -0800 | [diff] [blame] | 31 | import java.io.InputStream; |
| 32 | import java.io.StringWriter; |
Yuta HIGUCHI | 0184a7b | 2017-03-31 13:13:58 -0700 | [diff] [blame] | 33 | import java.nio.charset.StandardCharsets; |
Andrea Campanella | d8d92db | 2016-01-14 16:24:41 -0800 | [diff] [blame] | 34 | import java.util.ArrayList; |
| 35 | import java.util.List; |
Aaron Kruglikov | 17b4c85 | 2016-01-15 16:37:04 -0800 | [diff] [blame] | 36 | |
Andrea Campanella | d8d92db | 2016-01-14 16:24:41 -0800 | [diff] [blame] | 37 | /** |
| 38 | * Parser for Netconf XML configurations and replys. |
| 39 | */ |
| 40 | public final class XmlConfigParser { |
| 41 | public static final Logger log = LoggerFactory |
| 42 | .getLogger(XmlConfigParser.class); |
| 43 | |
Andrea Campanella | af1fa39 | 2018-06-26 11:16:51 +0200 | [diff] [blame] | 44 | 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 Campanella | d8d92db | 2016-01-14 16:24:41 -0800 | [diff] [blame] | 48 | private XmlConfigParser() { |
| 49 | //not called, preventing any allocation |
| 50 | } |
| 51 | |
| 52 | |
| 53 | public static HierarchicalConfiguration loadXml(InputStream xmlStream) { |
Andrea Campanella | d8d92db | 2016-01-14 16:24:41 -0800 | [diff] [blame] | 54 | try { |
Andrea Campanella | af1fa39 | 2018-06-26 11:16:51 +0200 | [diff] [blame] | 55 | XMLConfiguration cfg = new XMLConfiguration(); |
| 56 | DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance(); |
| 57 | //Disabling DTDs in order to avoid XXE xml-based attacks. |
| 58 | disableFeature(dbfactory, DISALLOW_DTD_FEATURE); |
| 59 | disableFeature(dbfactory, DISALLOW_EXTERNAL_DTD); |
| 60 | dbfactory.setXIncludeAware(false); |
| 61 | dbfactory.setExpandEntityReferences(false); |
| 62 | cfg.setDocumentBuilder(dbfactory.newDocumentBuilder()); |
Andrea Campanella | d8d92db | 2016-01-14 16:24:41 -0800 | [diff] [blame] | 63 | cfg.load(xmlStream); |
| 64 | return cfg; |
Andrea Campanella | af1fa39 | 2018-06-26 11:16:51 +0200 | [diff] [blame] | 65 | } catch (ConfigurationException | ParserConfigurationException e) { |
Andrea Campanella | d8d92db | 2016-01-14 16:24:41 -0800 | [diff] [blame] | 66 | throw new IllegalArgumentException("Cannot load xml from Stream", e); |
| 67 | } |
| 68 | } |
| 69 | |
Yuta HIGUCHI | 0184a7b | 2017-03-31 13:13:58 -0700 | [diff] [blame] | 70 | public static HierarchicalConfiguration loadXmlString(String xmlStr) { |
| 71 | return loadXml(new ByteArrayInputStream(xmlStr.getBytes(StandardCharsets.UTF_8))); |
| 72 | } |
| 73 | |
Andrea Campanella | d8d92db | 2016-01-14 16:24:41 -0800 | [diff] [blame] | 74 | public static List<ControllerInfo> parseStreamControllers(HierarchicalConfiguration cfg) { |
| 75 | List<ControllerInfo> controllers = new ArrayList<>(); |
| 76 | List<HierarchicalConfiguration> fields = |
| 77 | cfg.configurationsAt("data.capable-switch." + |
Andrea Campanella | af1fa39 | 2018-06-26 11:16:51 +0200 | [diff] [blame] | 78 | "logical-switches." + |
| 79 | "switch.controllers.controller"); |
Andrea Campanella | d8d92db | 2016-01-14 16:24:41 -0800 | [diff] [blame] | 80 | for (HierarchicalConfiguration sub : fields) { |
| 81 | controllers.add(new ControllerInfo( |
| 82 | IpAddress.valueOf(sub.getString("ip-address")), |
| 83 | Integer.parseInt(sub.getString("port")), |
| 84 | sub.getString("protocol"))); |
| 85 | } |
| 86 | return controllers; |
| 87 | } |
| 88 | |
Aaron Kruglikov | 17b4c85 | 2016-01-15 16:37:04 -0800 | [diff] [blame] | 89 | |
| 90 | protected static String parseSwitchId(HierarchicalConfiguration cfg) { |
Andrea Campanella | d8d92db | 2016-01-14 16:24:41 -0800 | [diff] [blame] | 91 | HierarchicalConfiguration field = |
| 92 | cfg.configurationAt("data.capable-switch." + |
Andrea Campanella | af1fa39 | 2018-06-26 11:16:51 +0200 | [diff] [blame] | 93 | "logical-switches." + |
| 94 | "switch"); |
Andrea Campanella | d8d92db | 2016-01-14 16:24:41 -0800 | [diff] [blame] | 95 | return field.getProperty("id").toString(); |
| 96 | } |
| 97 | |
| 98 | public static String parseCapableSwitchId(HierarchicalConfiguration cfg) { |
| 99 | HierarchicalConfiguration field = |
| 100 | cfg.configurationAt("data.capable-switch"); |
| 101 | return field.getProperty("id").toString(); |
| 102 | } |
| 103 | |
| 104 | public static String createControllersConfig(HierarchicalConfiguration cfg, |
| 105 | HierarchicalConfiguration actualCfg, |
| 106 | String target, String netconfOperation, |
| 107 | String controllerOperation, |
| 108 | List<ControllerInfo> controllers) { |
| 109 | //cfg.getKeys().forEachRemaining(key -> System.out.println(key)); |
| 110 | cfg.setProperty("edit-config.target", target); |
| 111 | cfg.setProperty("edit-config.default-operation", netconfOperation); |
| 112 | cfg.setProperty("edit-config.config.capable-switch.id", |
Andrea Campanella | af1fa39 | 2018-06-26 11:16:51 +0200 | [diff] [blame] | 113 | parseCapableSwitchId(actualCfg)); |
Andrea Campanella | d8d92db | 2016-01-14 16:24:41 -0800 | [diff] [blame] | 114 | cfg.setProperty("edit-config.config.capable-switch." + |
Andrea Campanella | af1fa39 | 2018-06-26 11:16:51 +0200 | [diff] [blame] | 115 | "logical-switches.switch.id", parseSwitchId(actualCfg)); |
Andrea Campanella | d8d92db | 2016-01-14 16:24:41 -0800 | [diff] [blame] | 116 | List<ConfigurationNode> newControllers = new ArrayList<>(); |
| 117 | for (ControllerInfo ci : controllers) { |
| 118 | XMLConfiguration controller = new XMLConfiguration(); |
| 119 | controller.setRoot(new HierarchicalConfiguration.Node("controller")); |
| 120 | String id = ci.type() + ":" + ci.ip() + ":" + ci.port(); |
| 121 | controller.setProperty("id", id); |
| 122 | controller.setProperty("ip-address", ci.ip()); |
| 123 | controller.setProperty("port", ci.port()); |
| 124 | controller.setProperty("protocol", ci.type()); |
| 125 | newControllers.add(controller.getRootNode()); |
| 126 | } |
| 127 | cfg.addNodes("edit-config.config.capable-switch.logical-switches." + |
Andrea Campanella | af1fa39 | 2018-06-26 11:16:51 +0200 | [diff] [blame] | 128 | "switch.controllers", newControllers); |
Andrea Campanella | d8d92db | 2016-01-14 16:24:41 -0800 | [diff] [blame] | 129 | XMLConfiguration editcfg = (XMLConfiguration) cfg; |
| 130 | StringWriter stringWriter = new StringWriter(); |
| 131 | try { |
| 132 | editcfg.save(stringWriter); |
| 133 | } catch (ConfigurationException e) { |
| 134 | log.error("createControllersConfig()", e); |
| 135 | } |
| 136 | String s = stringWriter.toString() |
| 137 | .replaceAll("<controller>", |
Andrea Campanella | af1fa39 | 2018-06-26 11:16:51 +0200 | [diff] [blame] | 138 | "<controller nc:operation=\"" + controllerOperation + "\">"); |
Andrea Campanella | d8d92db | 2016-01-14 16:24:41 -0800 | [diff] [blame] | 139 | s = s.replace("<target>" + target + "</target>", |
Andrea Campanella | af1fa39 | 2018-06-26 11:16:51 +0200 | [diff] [blame] | 140 | "<target><" + target + "/></target>"); |
Andrea Campanella | d8d92db | 2016-01-14 16:24:41 -0800 | [diff] [blame] | 141 | return s; |
| 142 | |
| 143 | } |
| 144 | |
Andrea Campanella | d8d92db | 2016-01-14 16:24:41 -0800 | [diff] [blame] | 145 | //TODO implement mor methods for parsing configuration when you need them |
Andreas Papazois | 1dff77c | 2016-02-16 16:27:33 +0200 | [diff] [blame] | 146 | |
| 147 | /** |
| 148 | * Parses a config reply and returns the result. |
Andrea Campanella | af1fa39 | 2018-06-26 11:16:51 +0200 | [diff] [blame] | 149 | * |
Andreas Papazois | 1dff77c | 2016-02-16 16:27:33 +0200 | [diff] [blame] | 150 | * @param reply a tree-like source |
| 151 | * @return the configuration result |
| 152 | */ |
| 153 | public static boolean configSuccess(HierarchicalConfiguration reply) { |
| 154 | if (reply != null) { |
| 155 | if (reply.containsKey("ok")) { |
| 156 | return true; |
| 157 | } |
| 158 | } |
| 159 | return false; |
| 160 | } |
Andrea Campanella | af1fa39 | 2018-06-26 11:16:51 +0200 | [diff] [blame] | 161 | |
| 162 | private static void disableFeature(DocumentBuilderFactory dbfactory, String feature) { |
| 163 | try { |
| 164 | dbfactory.setFeature(feature, true); |
| 165 | } catch (ParserConfigurationException e) { |
| 166 | // This should catch a failed setFeature feature |
| 167 | log.info("ParserConfigurationException was thrown. The feature '" + |
| 168 | feature + "' is probably not supported by your XML processor."); |
| 169 | } |
| 170 | } |
Andrea Campanella | d8d92db | 2016-01-14 16:24:41 -0800 | [diff] [blame] | 171 | } |