andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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 | |
| 17 | package org.onosproject.driver.netconf; |
| 18 | |
| 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; |
| 24 | import org.onosproject.net.behaviour.ControllerInfo; |
| 25 | import org.slf4j.Logger; |
| 26 | import org.slf4j.LoggerFactory; |
| 27 | |
| 28 | import java.io.InputStream; |
| 29 | import java.io.StringWriter; |
| 30 | import java.util.ArrayList; |
| 31 | import java.util.List; |
| 32 | |
| 33 | /** |
| 34 | * Parser for Netconf XML configurations and replys. |
| 35 | */ |
| 36 | final class XmlConfigParser { |
| 37 | public static final Logger log = LoggerFactory |
| 38 | .getLogger(XmlConfigParser.class); |
| 39 | |
| 40 | private XmlConfigParser() { |
| 41 | //not called, preventing any allocation |
| 42 | } |
| 43 | |
| 44 | |
| 45 | protected static HierarchicalConfiguration loadXml(InputStream xmlStream) { |
| 46 | XMLConfiguration cfg = new XMLConfiguration(); |
| 47 | try { |
| 48 | cfg.load(xmlStream); |
| 49 | return cfg; |
| 50 | } catch (ConfigurationException e) { |
| 51 | throw new IllegalArgumentException("Cannot load xml from Stream", e); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | protected static List<ControllerInfo> parseStreamControllers(HierarchicalConfiguration cfg) { |
| 56 | List<ControllerInfo> controllers = new ArrayList<>(); |
| 57 | List<HierarchicalConfiguration> fields = |
| 58 | cfg.configurationsAt("data.capable-switch." + |
| 59 | "logical-switches." + |
| 60 | "switch.controllers.controller"); |
| 61 | for (HierarchicalConfiguration sub : fields) { |
| 62 | controllers.add(new ControllerInfo( |
| 63 | IpAddress.valueOf(sub.getString("ip-address")), |
| 64 | Integer.parseInt(sub.getString("port")), |
| 65 | sub.getString("protocol"))); |
| 66 | } |
| 67 | return controllers; |
| 68 | } |
| 69 | |
| 70 | protected static String parseSwitchId(HierarchicalConfiguration cfg) { |
| 71 | HierarchicalConfiguration field = |
| 72 | cfg.configurationAt("data.capable-switch." + |
| 73 | "logical-switches." + |
| 74 | "switch"); |
| 75 | return field.getProperty("id").toString(); |
| 76 | } |
| 77 | |
| 78 | protected static String parseCapableSwitchId(HierarchicalConfiguration cfg) { |
| 79 | HierarchicalConfiguration field = |
| 80 | cfg.configurationAt("data.capable-switch"); |
| 81 | return field.getProperty("id").toString(); |
| 82 | } |
| 83 | |
| 84 | protected static String createControllersConfig(HierarchicalConfiguration cfg, |
| 85 | HierarchicalConfiguration actualCfg, |
| 86 | String target, String netconfOperation, |
| 87 | String controllerOperation, |
| 88 | List<ControllerInfo> controllers) { |
| 89 | //cfg.getKeys().forEachRemaining(key -> System.out.println(key)); |
| 90 | cfg.setProperty("edit-config.target", target); |
| 91 | cfg.setProperty("edit-config.default-operation", netconfOperation); |
| 92 | cfg.setProperty("edit-config.config.capable-switch.id", |
| 93 | parseCapableSwitchId(actualCfg)); |
| 94 | cfg.setProperty("edit-config.config.capable-switch." + |
| 95 | "logical-switches.switch.id", parseSwitchId(actualCfg)); |
| 96 | List<ConfigurationNode> newControllers = new ArrayList<>(); |
| 97 | for (ControllerInfo ci : controllers) { |
| 98 | XMLConfiguration controller = new XMLConfiguration(); |
| 99 | controller.setRoot(new HierarchicalConfiguration.Node("controller")); |
| 100 | String id = ci.type() + ":" + ci.ip() + ":" + ci.port(); |
| 101 | controller.setProperty("id", id); |
| 102 | controller.setProperty("ip-address", ci.ip()); |
| 103 | controller.setProperty("port", ci.port()); |
| 104 | controller.setProperty("protocol", ci.type()); |
| 105 | newControllers.add(controller.getRootNode()); |
| 106 | } |
| 107 | cfg.addNodes("edit-config.config.capable-switch.logical-switches." + |
| 108 | "switch.controllers", newControllers); |
| 109 | XMLConfiguration editcfg = (XMLConfiguration) cfg; |
| 110 | StringWriter stringWriter = new StringWriter(); |
| 111 | try { |
| 112 | editcfg.save(stringWriter); |
| 113 | } catch (ConfigurationException e) { |
Ray Milkey | 676249c | 2015-12-18 09:27:03 -0800 | [diff] [blame^] | 114 | log.error("createControllersConfig()", e); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 115 | } |
| 116 | String s = stringWriter.toString() |
| 117 | .replaceAll("<controller>", |
| 118 | "<controller nc:operation=\"" + controllerOperation + "\">"); |
| 119 | s = s.replace("<target>" + target + "</target>", |
| 120 | "<target><" + target + "/></target>"); |
| 121 | return s; |
| 122 | |
| 123 | } |
| 124 | |
| 125 | //TODO implement mor methods for parsing configuration when you need them |
| 126 | } |