Andrea Campanella | d8d92db | 2016-01-14 16:24:41 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
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 | |
| 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; |
Aaron Kruglikov | 17b4c85 | 2016-01-15 16:37:04 -0800 | [diff] [blame] | 25 | import org.onosproject.net.AnnotationKeys; |
Andrea Campanella | d8d92db | 2016-01-14 16:24:41 -0800 | [diff] [blame] | 26 | import org.onosproject.net.ChannelSpacing; |
Aaron Kruglikov | 17b4c85 | 2016-01-15 16:37:04 -0800 | [diff] [blame] | 27 | import org.onosproject.net.CltSignalType; |
| 28 | import org.onosproject.net.DefaultAnnotations; |
Andrea Campanella | d8d92db | 2016-01-14 16:24:41 -0800 | [diff] [blame] | 29 | import org.onosproject.net.GridType; |
| 30 | import org.onosproject.net.OchSignal; |
| 31 | import org.onosproject.net.OduSignalType; |
| 32 | import org.onosproject.net.PortNumber; |
Andrea Campanella | d8d92db | 2016-01-14 16:24:41 -0800 | [diff] [blame] | 33 | import org.onosproject.net.behaviour.ControllerInfo; |
Andrea Campanella | d8d92db | 2016-01-14 16:24:41 -0800 | [diff] [blame] | 34 | import org.onosproject.net.device.PortDescription; |
| 35 | import org.slf4j.Logger; |
| 36 | import org.slf4j.LoggerFactory; |
| 37 | |
HIGUCHI Yuta | 34a3f69 | 2016-01-09 21:08:57 -0800 | [diff] [blame] | 38 | import static org.onosproject.net.optical.device.OchPortHelper.ochPortDescription; |
HIGUCHI Yuta | 4c0ef6b | 2016-05-02 19:45:41 -0700 | [diff] [blame] | 39 | import static org.onosproject.net.optical.device.OduCltPortHelper.oduCltPortDescription; |
HIGUCHI Yuta | 34a3f69 | 2016-01-09 21:08:57 -0800 | [diff] [blame] | 40 | |
Andrea Campanella | d8d92db | 2016-01-14 16:24:41 -0800 | [diff] [blame] | 41 | import java.io.InputStream; |
| 42 | import java.io.StringWriter; |
| 43 | import java.util.ArrayList; |
| 44 | import java.util.List; |
Aaron Kruglikov | 17b4c85 | 2016-01-15 16:37:04 -0800 | [diff] [blame] | 45 | import java.util.concurrent.atomic.AtomicInteger; |
| 46 | |
Andrea Campanella | d8d92db | 2016-01-14 16:24:41 -0800 | [diff] [blame] | 47 | |
| 48 | /** |
| 49 | * Parser for Netconf XML configurations and replys. |
| 50 | */ |
| 51 | public final class XmlConfigParser { |
| 52 | public static final Logger log = LoggerFactory |
| 53 | .getLogger(XmlConfigParser.class); |
| 54 | |
| 55 | private XmlConfigParser() { |
| 56 | //not called, preventing any allocation |
| 57 | } |
| 58 | |
| 59 | |
| 60 | public static HierarchicalConfiguration loadXml(InputStream xmlStream) { |
| 61 | XMLConfiguration cfg = new XMLConfiguration(); |
| 62 | try { |
| 63 | cfg.load(xmlStream); |
| 64 | return cfg; |
| 65 | } catch (ConfigurationException e) { |
| 66 | throw new IllegalArgumentException("Cannot load xml from Stream", e); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | public static List<ControllerInfo> parseStreamControllers(HierarchicalConfiguration cfg) { |
| 71 | List<ControllerInfo> controllers = new ArrayList<>(); |
| 72 | List<HierarchicalConfiguration> fields = |
| 73 | cfg.configurationsAt("data.capable-switch." + |
| 74 | "logical-switches." + |
| 75 | "switch.controllers.controller"); |
| 76 | for (HierarchicalConfiguration sub : fields) { |
| 77 | controllers.add(new ControllerInfo( |
| 78 | IpAddress.valueOf(sub.getString("ip-address")), |
| 79 | Integer.parseInt(sub.getString("port")), |
| 80 | sub.getString("protocol"))); |
| 81 | } |
| 82 | return controllers; |
| 83 | } |
| 84 | |
Aaron Kruglikov | 17b4c85 | 2016-01-15 16:37:04 -0800 | [diff] [blame] | 85 | /** |
| 86 | * Parses a configuration and returns a set of ports for the fujitsu T100. |
| 87 | * @param cfg a hierarchical configuration |
| 88 | * @return a list of port descriptions |
| 89 | */ |
| 90 | public static List<PortDescription> parseFujitsuT100Ports(HierarchicalConfiguration cfg) { |
| 91 | AtomicInteger counter = new AtomicInteger(1); |
| 92 | List<PortDescription> portDescriptions = Lists.newArrayList(); |
| 93 | List<HierarchicalConfiguration> subtrees = |
| 94 | cfg.configurationsAt("data.interfaces.interface"); |
| 95 | for (HierarchicalConfiguration portConfig : subtrees) { |
| 96 | if (!portConfig.getString("name").contains("LCN") && |
| 97 | !portConfig.getString("name").contains("LMP") && |
| 98 | portConfig.getString("type").equals("ianaift:ethernetCsmacd")) { |
| 99 | portDescriptions.add(parseT100OduPort(portConfig, counter.getAndIncrement())); |
| 100 | } else if (portConfig.getString("type").equals("ianaift:otnOtu")) { |
| 101 | portDescriptions.add(parseT100OchPort(portConfig, counter.getAndIncrement())); |
| 102 | } |
| 103 | } |
| 104 | return portDescriptions; |
| 105 | } |
| 106 | |
HIGUCHI Yuta | 34a3f69 | 2016-01-09 21:08:57 -0800 | [diff] [blame] | 107 | private static PortDescription parseT100OchPort(HierarchicalConfiguration cfg, long count) { |
Aaron Kruglikov | 17b4c85 | 2016-01-15 16:37:04 -0800 | [diff] [blame] | 108 | PortNumber portNumber = PortNumber.portNumber(count); |
| 109 | HierarchicalConfiguration otuConfig = cfg.configurationAt("otu"); |
| 110 | boolean enabled = otuConfig.getString("administrative-state").equals("up"); |
| 111 | OduSignalType signalType = otuConfig.getString("rate").equals("OTU4") ? OduSignalType.ODU4 : null; |
| 112 | //Unsure how to retreive, outside knowledge it is tunable. |
| 113 | boolean isTunable = true; |
| 114 | OchSignal lambda = new OchSignal(GridType.DWDM, ChannelSpacing.CHL_50GHZ, 0, 4); |
| 115 | DefaultAnnotations annotations = DefaultAnnotations.builder(). |
| 116 | set(AnnotationKeys.PORT_NAME, cfg.getString("name")). |
| 117 | build(); |
HIGUCHI Yuta | 34a3f69 | 2016-01-09 21:08:57 -0800 | [diff] [blame] | 118 | return ochPortDescription(portNumber, enabled, signalType, isTunable, lambda, annotations); |
Aaron Kruglikov | 17b4c85 | 2016-01-15 16:37:04 -0800 | [diff] [blame] | 119 | } |
| 120 | |
HIGUCHI Yuta | 4c0ef6b | 2016-05-02 19:45:41 -0700 | [diff] [blame] | 121 | private static PortDescription parseT100OduPort(HierarchicalConfiguration cfg, long count) { |
Aaron Kruglikov | 17b4c85 | 2016-01-15 16:37:04 -0800 | [diff] [blame] | 122 | PortNumber portNumber = PortNumber.portNumber(count); |
| 123 | HierarchicalConfiguration ethernetConfig = cfg.configurationAt("ethernet"); |
| 124 | boolean enabled = ethernetConfig.getString("administrative-state").equals("up"); |
| 125 | //Rate is in kbps |
| 126 | CltSignalType signalType = ethernetConfig.getString("rate").equals("100000000") ? |
| 127 | CltSignalType.CLT_100GBE : null; |
| 128 | DefaultAnnotations annotations = DefaultAnnotations.builder(). |
| 129 | set(AnnotationKeys.PORT_NAME, cfg.getString("name")). |
| 130 | build(); |
HIGUCHI Yuta | 4c0ef6b | 2016-05-02 19:45:41 -0700 | [diff] [blame] | 131 | return oduCltPortDescription(portNumber, enabled, signalType, annotations); |
Aaron Kruglikov | 17b4c85 | 2016-01-15 16:37:04 -0800 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | protected static String parseSwitchId(HierarchicalConfiguration cfg) { |
Andrea Campanella | d8d92db | 2016-01-14 16:24:41 -0800 | [diff] [blame] | 135 | HierarchicalConfiguration field = |
| 136 | cfg.configurationAt("data.capable-switch." + |
| 137 | "logical-switches." + |
| 138 | "switch"); |
| 139 | return field.getProperty("id").toString(); |
| 140 | } |
| 141 | |
| 142 | public static String parseCapableSwitchId(HierarchicalConfiguration cfg) { |
| 143 | HierarchicalConfiguration field = |
| 144 | cfg.configurationAt("data.capable-switch"); |
| 145 | return field.getProperty("id").toString(); |
| 146 | } |
| 147 | |
| 148 | public static String createControllersConfig(HierarchicalConfiguration cfg, |
| 149 | HierarchicalConfiguration actualCfg, |
| 150 | String target, String netconfOperation, |
| 151 | String controllerOperation, |
| 152 | List<ControllerInfo> controllers) { |
| 153 | //cfg.getKeys().forEachRemaining(key -> System.out.println(key)); |
| 154 | cfg.setProperty("edit-config.target", target); |
| 155 | cfg.setProperty("edit-config.default-operation", netconfOperation); |
| 156 | cfg.setProperty("edit-config.config.capable-switch.id", |
| 157 | parseCapableSwitchId(actualCfg)); |
| 158 | cfg.setProperty("edit-config.config.capable-switch." + |
| 159 | "logical-switches.switch.id", parseSwitchId(actualCfg)); |
| 160 | List<ConfigurationNode> newControllers = new ArrayList<>(); |
| 161 | for (ControllerInfo ci : controllers) { |
| 162 | XMLConfiguration controller = new XMLConfiguration(); |
| 163 | controller.setRoot(new HierarchicalConfiguration.Node("controller")); |
| 164 | String id = ci.type() + ":" + ci.ip() + ":" + ci.port(); |
| 165 | controller.setProperty("id", id); |
| 166 | controller.setProperty("ip-address", ci.ip()); |
| 167 | controller.setProperty("port", ci.port()); |
| 168 | controller.setProperty("protocol", ci.type()); |
| 169 | newControllers.add(controller.getRootNode()); |
| 170 | } |
| 171 | cfg.addNodes("edit-config.config.capable-switch.logical-switches." + |
| 172 | "switch.controllers", newControllers); |
| 173 | XMLConfiguration editcfg = (XMLConfiguration) cfg; |
| 174 | StringWriter stringWriter = new StringWriter(); |
| 175 | try { |
| 176 | editcfg.save(stringWriter); |
| 177 | } catch (ConfigurationException e) { |
| 178 | log.error("createControllersConfig()", e); |
| 179 | } |
| 180 | String s = stringWriter.toString() |
| 181 | .replaceAll("<controller>", |
| 182 | "<controller nc:operation=\"" + controllerOperation + "\">"); |
| 183 | s = s.replace("<target>" + target + "</target>", |
| 184 | "<target><" + target + "/></target>"); |
| 185 | return s; |
| 186 | |
| 187 | } |
| 188 | |
Andrea Campanella | d8d92db | 2016-01-14 16:24:41 -0800 | [diff] [blame] | 189 | //TODO implement mor methods for parsing configuration when you need them |
Andreas Papazois | 1dff77c | 2016-02-16 16:27:33 +0200 | [diff] [blame] | 190 | |
| 191 | /** |
| 192 | * Parses a config reply and returns the result. |
| 193 | * @param reply a tree-like source |
| 194 | * @return the configuration result |
| 195 | */ |
| 196 | public static boolean configSuccess(HierarchicalConfiguration reply) { |
| 197 | if (reply != null) { |
| 198 | if (reply.containsKey("ok")) { |
| 199 | return true; |
| 200 | } |
| 201 | } |
| 202 | return false; |
| 203 | } |
Andrea Campanella | d8d92db | 2016-01-14 16:24:41 -0800 | [diff] [blame] | 204 | } |