Andrea Campanella | d8d92db | 2016-01-14 16:24:41 -0800 | [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; |
| 18 | |
| 19 | import com.google.common.collect.Lists; |
| 20 | import org.apache.commons.configuration.ConfigurationException; |
| 21 | import org.apache.commons.configuration.HierarchicalConfiguration; |
| 22 | import org.apache.commons.configuration.XMLConfiguration; |
| 23 | import org.apache.commons.configuration.tree.ConfigurationNode; |
| 24 | import org.onlab.packet.IpAddress; |
| 25 | import org.onosproject.net.ChannelSpacing; |
| 26 | import org.onosproject.net.GridType; |
| 27 | import org.onosproject.net.OchSignal; |
| 28 | import org.onosproject.net.OduSignalType; |
| 29 | import org.onosproject.net.PortNumber; |
| 30 | import org.onosproject.net.SparseAnnotations; |
| 31 | import org.onosproject.net.behaviour.ControllerInfo; |
| 32 | import org.onosproject.net.device.OchPortDescription; |
| 33 | import org.onosproject.net.device.PortDescription; |
| 34 | import org.slf4j.Logger; |
| 35 | import org.slf4j.LoggerFactory; |
| 36 | |
| 37 | import java.io.InputStream; |
| 38 | import java.io.StringWriter; |
| 39 | import java.util.ArrayList; |
| 40 | import java.util.List; |
| 41 | |
| 42 | /** |
| 43 | * Parser for Netconf XML configurations and replys. |
| 44 | */ |
| 45 | public final class XmlConfigParser { |
| 46 | public static final Logger log = LoggerFactory |
| 47 | .getLogger(XmlConfigParser.class); |
| 48 | |
| 49 | private XmlConfigParser() { |
| 50 | //not called, preventing any allocation |
| 51 | } |
| 52 | |
| 53 | |
| 54 | public static HierarchicalConfiguration loadXml(InputStream xmlStream) { |
| 55 | XMLConfiguration cfg = new XMLConfiguration(); |
| 56 | try { |
| 57 | cfg.load(xmlStream); |
| 58 | return cfg; |
| 59 | } catch (ConfigurationException e) { |
| 60 | throw new IllegalArgumentException("Cannot load xml from Stream", e); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | public static List<ControllerInfo> parseStreamControllers(HierarchicalConfiguration cfg) { |
| 65 | List<ControllerInfo> controllers = new ArrayList<>(); |
| 66 | List<HierarchicalConfiguration> fields = |
| 67 | cfg.configurationsAt("data.capable-switch." + |
| 68 | "logical-switches." + |
| 69 | "switch.controllers.controller"); |
| 70 | for (HierarchicalConfiguration sub : fields) { |
| 71 | controllers.add(new ControllerInfo( |
| 72 | IpAddress.valueOf(sub.getString("ip-address")), |
| 73 | Integer.parseInt(sub.getString("port")), |
| 74 | sub.getString("protocol"))); |
| 75 | } |
| 76 | return controllers; |
| 77 | } |
| 78 | |
| 79 | public static String parseSwitchId(HierarchicalConfiguration cfg) { |
| 80 | HierarchicalConfiguration field = |
| 81 | cfg.configurationAt("data.capable-switch." + |
| 82 | "logical-switches." + |
| 83 | "switch"); |
| 84 | return field.getProperty("id").toString(); |
| 85 | } |
| 86 | |
| 87 | public static String parseCapableSwitchId(HierarchicalConfiguration cfg) { |
| 88 | HierarchicalConfiguration field = |
| 89 | cfg.configurationAt("data.capable-switch"); |
| 90 | return field.getProperty("id").toString(); |
| 91 | } |
| 92 | |
| 93 | public static String createControllersConfig(HierarchicalConfiguration cfg, |
| 94 | HierarchicalConfiguration actualCfg, |
| 95 | String target, String netconfOperation, |
| 96 | String controllerOperation, |
| 97 | List<ControllerInfo> controllers) { |
| 98 | //cfg.getKeys().forEachRemaining(key -> System.out.println(key)); |
| 99 | cfg.setProperty("edit-config.target", target); |
| 100 | cfg.setProperty("edit-config.default-operation", netconfOperation); |
| 101 | cfg.setProperty("edit-config.config.capable-switch.id", |
| 102 | parseCapableSwitchId(actualCfg)); |
| 103 | cfg.setProperty("edit-config.config.capable-switch." + |
| 104 | "logical-switches.switch.id", parseSwitchId(actualCfg)); |
| 105 | List<ConfigurationNode> newControllers = new ArrayList<>(); |
| 106 | for (ControllerInfo ci : controllers) { |
| 107 | XMLConfiguration controller = new XMLConfiguration(); |
| 108 | controller.setRoot(new HierarchicalConfiguration.Node("controller")); |
| 109 | String id = ci.type() + ":" + ci.ip() + ":" + ci.port(); |
| 110 | controller.setProperty("id", id); |
| 111 | controller.setProperty("ip-address", ci.ip()); |
| 112 | controller.setProperty("port", ci.port()); |
| 113 | controller.setProperty("protocol", ci.type()); |
| 114 | newControllers.add(controller.getRootNode()); |
| 115 | } |
| 116 | cfg.addNodes("edit-config.config.capable-switch.logical-switches." + |
| 117 | "switch.controllers", newControllers); |
| 118 | XMLConfiguration editcfg = (XMLConfiguration) cfg; |
| 119 | StringWriter stringWriter = new StringWriter(); |
| 120 | try { |
| 121 | editcfg.save(stringWriter); |
| 122 | } catch (ConfigurationException e) { |
| 123 | log.error("createControllersConfig()", e); |
| 124 | } |
| 125 | String s = stringWriter.toString() |
| 126 | .replaceAll("<controller>", |
| 127 | "<controller nc:operation=\"" + controllerOperation + "\">"); |
| 128 | s = s.replace("<target>" + target + "</target>", |
| 129 | "<target><" + target + "/></target>"); |
| 130 | return s; |
| 131 | |
| 132 | } |
| 133 | |
| 134 | public static List<HierarchicalConfiguration> parseWaveServerCienaPorts(HierarchicalConfiguration cfg) { |
| 135 | return cfg.configurationsAt("ws-ports.port-interface"); |
| 136 | } |
| 137 | |
| 138 | public static PortDescription parseWaveServerCienaOCHPorts(long portNumber, long oduPortSpeed, |
| 139 | HierarchicalConfiguration config, |
| 140 | SparseAnnotations annotations) { |
| 141 | final List<String> tunableType = Lists.newArrayList("Performance-Optimized", "Accelerated"); |
| 142 | final String transmitterPath = "ptp-config.transmitter-state"; |
| 143 | final String tunablePath = "ptp-config.adv-config.tx-tuning-mode"; |
| 144 | final String gridTypePath = "ptp-config.adv-config.wl-spacing"; |
| 145 | final String frequencyPath = "ptp-config.adv-config.frequency"; |
| 146 | |
| 147 | boolean isEnabled = config.getString(transmitterPath).equals("enabled"); |
| 148 | boolean isTunable = tunableType.contains(config.getString(tunablePath)); |
| 149 | |
| 150 | //FIXME change when all optical types have two way information methods, see jira tickets |
| 151 | final int speed100GbpsinMbps = 100000; |
| 152 | OduSignalType oduSignalType = oduPortSpeed == speed100GbpsinMbps ? OduSignalType.ODU4 : null; |
| 153 | GridType gridType = config.getString(gridTypePath).equals("FlexGrid") ? GridType.FLEX : null; |
| 154 | ChannelSpacing chSpacing = gridType == GridType.FLEX ? ChannelSpacing.CHL_6P25GHZ : null; |
| 155 | |
| 156 | //Working in Ghz //(Nominal central frequency - 193.1)/channelSpacing = spacingMultiplier |
| 157 | final int baseFrequency = 193100; |
| 158 | int spacingMult = (int) (toGbps((Integer.parseInt(config.getString(frequencyPath)) - |
| 159 | baseFrequency)) / toGbpsFromHz(chSpacing.frequency().asHz())); //FIXME is there a better way ? |
| 160 | |
| 161 | return new OchPortDescription(PortNumber.portNumber(portNumber), isEnabled, oduSignalType, isTunable, |
| 162 | new OchSignal(gridType, chSpacing, spacingMult, 1), annotations); |
| 163 | } |
| 164 | |
| 165 | //FIXME remove when all optical types have two way information methods, see jira tickets |
| 166 | private static long toGbps(long speed) { |
| 167 | return speed * 1000; |
| 168 | } |
| 169 | |
| 170 | private static long toGbpsFromHz(long speed) { |
| 171 | return speed / 1000; |
| 172 | } |
| 173 | //TODO implement mor methods for parsing configuration when you need them |
| 174 | } |