blob: 55eecb16375a630b704c0e75a56431f4ef5d8d77 [file] [log] [blame]
Andrea Campanellad8d92db2016-01-14 16:24:41 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
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
Yuta HIGUCHI0184a7b2017-03-31 13:13:58 -070028import java.io.ByteArrayInputStream;
Andrea Campanellad8d92db2016-01-14 16:24:41 -080029import java.io.InputStream;
30import java.io.StringWriter;
Yuta HIGUCHI0184a7b2017-03-31 13:13:58 -070031import java.nio.charset.StandardCharsets;
Andrea Campanellad8d92db2016-01-14 16:24:41 -080032import java.util.ArrayList;
33import java.util.List;
Aaron Kruglikov17b4c852016-01-15 16:37:04 -080034
Andrea Campanellad8d92db2016-01-14 16:24:41 -080035
36/**
37 * Parser for Netconf XML configurations and replys.
38 */
39public final class XmlConfigParser {
40 public static final Logger log = LoggerFactory
41 .getLogger(XmlConfigParser.class);
42
43 private XmlConfigParser() {
44 //not called, preventing any allocation
45 }
46
47
48 public static HierarchicalConfiguration loadXml(InputStream xmlStream) {
49 XMLConfiguration cfg = new XMLConfiguration();
50 try {
51 cfg.load(xmlStream);
52 return cfg;
53 } catch (ConfigurationException e) {
54 throw new IllegalArgumentException("Cannot load xml from Stream", e);
55 }
56 }
57
Yuta HIGUCHI0184a7b2017-03-31 13:13:58 -070058 public static HierarchicalConfiguration loadXmlString(String xmlStr) {
59 return loadXml(new ByteArrayInputStream(xmlStr.getBytes(StandardCharsets.UTF_8)));
60 }
61
Andrea Campanellad8d92db2016-01-14 16:24:41 -080062 public static List<ControllerInfo> parseStreamControllers(HierarchicalConfiguration cfg) {
63 List<ControllerInfo> controllers = new ArrayList<>();
64 List<HierarchicalConfiguration> fields =
65 cfg.configurationsAt("data.capable-switch." +
66 "logical-switches." +
67 "switch.controllers.controller");
68 for (HierarchicalConfiguration sub : fields) {
69 controllers.add(new ControllerInfo(
70 IpAddress.valueOf(sub.getString("ip-address")),
71 Integer.parseInt(sub.getString("port")),
72 sub.getString("protocol")));
73 }
74 return controllers;
75 }
76
Aaron Kruglikov17b4c852016-01-15 16:37:04 -080077
78 protected static String parseSwitchId(HierarchicalConfiguration cfg) {
Andrea Campanellad8d92db2016-01-14 16:24:41 -080079 HierarchicalConfiguration field =
80 cfg.configurationAt("data.capable-switch." +
81 "logical-switches." +
82 "switch");
83 return field.getProperty("id").toString();
84 }
85
86 public static String parseCapableSwitchId(HierarchicalConfiguration cfg) {
87 HierarchicalConfiguration field =
88 cfg.configurationAt("data.capable-switch");
89 return field.getProperty("id").toString();
90 }
91
92 public static String createControllersConfig(HierarchicalConfiguration cfg,
93 HierarchicalConfiguration actualCfg,
94 String target, String netconfOperation,
95 String controllerOperation,
96 List<ControllerInfo> controllers) {
97 //cfg.getKeys().forEachRemaining(key -> System.out.println(key));
98 cfg.setProperty("edit-config.target", target);
99 cfg.setProperty("edit-config.default-operation", netconfOperation);
100 cfg.setProperty("edit-config.config.capable-switch.id",
101 parseCapableSwitchId(actualCfg));
102 cfg.setProperty("edit-config.config.capable-switch." +
103 "logical-switches.switch.id", parseSwitchId(actualCfg));
104 List<ConfigurationNode> newControllers = new ArrayList<>();
105 for (ControllerInfo ci : controllers) {
106 XMLConfiguration controller = new XMLConfiguration();
107 controller.setRoot(new HierarchicalConfiguration.Node("controller"));
108 String id = ci.type() + ":" + ci.ip() + ":" + ci.port();
109 controller.setProperty("id", id);
110 controller.setProperty("ip-address", ci.ip());
111 controller.setProperty("port", ci.port());
112 controller.setProperty("protocol", ci.type());
113 newControllers.add(controller.getRootNode());
114 }
115 cfg.addNodes("edit-config.config.capable-switch.logical-switches." +
116 "switch.controllers", newControllers);
117 XMLConfiguration editcfg = (XMLConfiguration) cfg;
118 StringWriter stringWriter = new StringWriter();
119 try {
120 editcfg.save(stringWriter);
121 } catch (ConfigurationException e) {
122 log.error("createControllersConfig()", e);
123 }
124 String s = stringWriter.toString()
125 .replaceAll("<controller>",
126 "<controller nc:operation=\"" + controllerOperation + "\">");
127 s = s.replace("<target>" + target + "</target>",
128 "<target><" + target + "/></target>");
129 return s;
130
131 }
132
Andrea Campanellad8d92db2016-01-14 16:24:41 -0800133 //TODO implement mor methods for parsing configuration when you need them
Andreas Papazois1dff77c2016-02-16 16:27:33 +0200134
135 /**
136 * Parses a config reply and returns the result.
137 * @param reply a tree-like source
138 * @return the configuration result
139 */
140 public static boolean configSuccess(HierarchicalConfiguration reply) {
141 if (reply != null) {
142 if (reply.containsKey("ok")) {
143 return true;
144 }
145 }
146 return false;
147 }
Andrea Campanellad8d92db2016-01-14 16:24:41 -0800148}