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