blob: f06ad814419afdcebcfd73fd770df85ae63b1b3b [file] [log] [blame]
Akihiro Yamanouchi5e5d4df2016-06-08 17:06:33 +09001/*
2 * Copyright 2016-present 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
17package org.onosproject.drivers.fujitsu;
18
19import com.google.common.collect.ImmutableList;
20import org.apache.commons.configuration.HierarchicalConfiguration;
21import org.onosproject.drivers.utilities.XmlConfigParser;
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +090022import org.apache.commons.configuration.ConfigurationException;
23import org.apache.commons.configuration.XMLConfiguration;
24import org.apache.commons.configuration.tree.ConfigurationNode;
Akihiro Yamanouchi5e5d4df2016-06-08 17:06:33 +090025import org.onlab.packet.IpAddress;
26import org.onosproject.mastership.MastershipService;
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +090027import org.onosproject.net.Annotations;
28import org.onosproject.net.DefaultAnnotations;
Akihiro Yamanouchi5e5d4df2016-06-08 17:06:33 +090029import org.onosproject.net.DeviceId;
30import org.onosproject.net.behaviour.ControllerConfig;
31import org.onosproject.net.behaviour.ControllerInfo;
32import org.onosproject.net.driver.AbstractHandlerBehaviour;
33import org.onosproject.net.driver.DriverHandler;
34import org.onosproject.netconf.NetconfController;
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +090035import org.onosproject.netconf.NetconfDevice;
36import org.onosproject.netconf.NetconfException;
Akihiro Yamanouchi5e5d4df2016-06-08 17:06:33 +090037import org.slf4j.Logger;
38
39import java.io.ByteArrayInputStream;
40import java.io.IOException;
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +090041import java.io.StringWriter;
Akihiro Yamanouchi5e5d4df2016-06-08 17:06:33 +090042import java.nio.charset.StandardCharsets;
43import java.util.ArrayList;
44import java.util.List;
45
46import static com.google.common.base.Preconditions.checkNotNull;
47import static org.slf4j.LoggerFactory.getLogger;
48
49/**
50 * Implementation to get and set parameters available in VOLT NE
51 * through the Netconf protocol.
52 */
53public class FujitsuVoltControllerConfig extends AbstractHandlerBehaviour
54 implements ControllerConfig {
55
56 private final Logger log = getLogger(FujitsuVoltControllerConfig.class);
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +090057 private static final String RESOURCE_XML = "voltcontrollers.xml";
Akihiro Yamanouchi5e5d4df2016-06-08 17:06:33 +090058
59 private static final String DOT = ".";
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +090060 private static final String L_ANGLE_BR = "<";
61 private static final String R_ANGLE_BR = "/>";
Akihiro Yamanouchi5e5d4df2016-06-08 17:06:33 +090062 private static final String VOLT_NE_NAMESPACE =
63 "xmlns=\"http://fujitsu.com/ns/volt/1.1\"";
64 private static final String DATA = "data";
65 private static final String VOLT_NE = "volt-ne";
66 private static final String VOLT_OFCONFIG = "volt-ofconfig";
67 private static final String OF_CONTROLLERS = "of-controllers";
68 private static final String OF_CONTROLLER = "of-controller";
69 private static final String CONTROLLER_INFO = "controller-info";
70 private static final String REPORT_ALL = "report-all";
71 private static final String IP_ADDRESS = "ip-address";
72 private static final String PORT = "port";
73 private static final String PROTOCOL = "protocol";
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +090074 private static final String CONFIG = "config";
75 private static final String OFCONFIG_ID = "ofconfig-id";
76 private static final String EDIT_CONFIG = "edit-config";
77 private static final String TARGET = "target";
78 private static final String RUNNING = "running";
79 private static final String MERGE = "merge";
80 private static final String DEFAULT_OPERATION = "default-operation";
Akihiro Yamanouchi5e5d4df2016-06-08 17:06:33 +090081
82 private static final String VOLT_NE_OPEN = "<" + VOLT_NE + " ";
83 private static final String VOLT_NE_CLOSE = "</" + VOLT_NE + ">";
84 private static final String VOLT_OFCONFIG_EL = "<" + VOLT_OFCONFIG + "/>\n";
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +090085 private static final String TARGET_OPEN = "<" + TARGET + ">";
86 private static final String TARGET_CLOSE = "</" + TARGET + ">";
87 private static final String END_LICENSE_HEADER = "-->";
Akihiro Yamanouchi5e5d4df2016-06-08 17:06:33 +090088
89 private static final String VOLT_DATACONFIG = DATA + DOT + VOLT_NE + DOT +
90 VOLT_OFCONFIG + DOT + OF_CONTROLLERS + DOT + OF_CONTROLLER;
91
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +090092 private static final String EDIT_CONFIG_TG = EDIT_CONFIG + DOT + TARGET;
93 private static final String EDIT_CONFIG_DO = EDIT_CONFIG + DOT + DEFAULT_OPERATION;
94 private static final String CONTROLLER_INFO_ID = CONTROLLER_INFO + DOT + "id";
95 private static final String CONTROLLER_INFO_IP = CONTROLLER_INFO + DOT + IP_ADDRESS;
96 private static final String CONTROLLER_INFO_PORT = CONTROLLER_INFO + DOT + PORT;
97 private static final String CONTROLLER_INFO_PROTOCOL = CONTROLLER_INFO + DOT + PROTOCOL;
98
99 private static final String VOLT_EDITCONFIG = EDIT_CONFIG + DOT +
100 CONFIG + DOT + VOLT_NE + DOT + VOLT_OFCONFIG + DOT + OF_CONTROLLERS;
101
102
Akihiro Yamanouchi5e5d4df2016-06-08 17:06:33 +0900103 @Override
104 public List<ControllerInfo> getControllers() {
105 DriverHandler handler = handler();
106 NetconfController controller = handler.get(NetconfController.class);
107 MastershipService mastershipService = handler.get(MastershipService.class);
108 DeviceId ncDeviceId = handler.data().deviceId();
109 checkNotNull(controller, "Netconf controller is null");
110 List<ControllerInfo> controllers = new ArrayList<>();
111 if (mastershipService.isLocalMaster(ncDeviceId)) {
112 try {
113 StringBuilder request = new StringBuilder();
114 request.append(VOLT_NE_OPEN).append(VOLT_NE_NAMESPACE).append(">\n");
115 request.append(VOLT_OFCONFIG_EL);
116 request.append(VOLT_NE_CLOSE);
117
118 String reply;
119 reply = controller.
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +0900120 getDevicesMap().get(ncDeviceId).getSession().
121 get(request.toString(), REPORT_ALL);
Akihiro Yamanouchi5e5d4df2016-06-08 17:06:33 +0900122 log.debug("Reply XML {}", reply);
123 controllers.addAll(parseStreamVoltControllers(XmlConfigParser.
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +0900124 loadXml(new ByteArrayInputStream(reply.getBytes(StandardCharsets.UTF_8)))));
Akihiro Yamanouchi5e5d4df2016-06-08 17:06:33 +0900125 } catch (IOException e) {
126 log.error("Cannot communicate to device {} ", ncDeviceId);
127 }
128 } else {
129 log.warn("I'm not master for {} please use master, {} to execute command",
130 ncDeviceId,
131 mastershipService.getMasterFor(ncDeviceId));
132 }
133 return ImmutableList.copyOf(controllers);
134 }
135
136 @Override
137 public void setControllers(List<ControllerInfo> controllers) {
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +0900138 DriverHandler handler = handler();
139 NetconfController controller = handler.get(NetconfController.class);
140 MastershipService mastershipService = handler.get(MastershipService.class);
141 DeviceId ncdeviceId = handler.data().deviceId();
142 checkNotNull(controller, "Netconf controller is null");
143 if (mastershipService.isLocalMaster(ncdeviceId)) {
144 try {
145 NetconfDevice device = controller.getNetconfDevice(ncdeviceId);
146 String config = createVoltControllersConfig(
147 XmlConfigParser.loadXml(getClass().
148 getResourceAsStream(RESOURCE_XML)),
149 RUNNING, MERGE, controllers);
150 device.getSession().editConfig(config.substring(
151 config.indexOf(END_LICENSE_HEADER) + END_LICENSE_HEADER.length()));
152 } catch (NetconfException e) {
153 log.error("Cannot communicate to device {} , exception ", ncdeviceId, e);
154 }
155 } else {
156 log.warn("I'm not master for {} please use master, {} to execute command",
157 ncdeviceId,
158 mastershipService.getMasterFor(ncdeviceId));
159 }
Akihiro Yamanouchi5e5d4df2016-06-08 17:06:33 +0900160 }
161
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +0900162
Akihiro Yamanouchi5e5d4df2016-06-08 17:06:33 +0900163 /**
164 * Parses XML string to get controller information.
165 *
166 * @param cfg a hierarchical configuration
167 * @return a list of controllers
168 */
169 private List<ControllerInfo> parseStreamVoltControllers(HierarchicalConfiguration cfg) {
170 List<ControllerInfo> controllers = new ArrayList<>();
171 List<HierarchicalConfiguration> fields =
172 cfg.configurationsAt(VOLT_DATACONFIG);
173
174 for (HierarchicalConfiguration sub : fields) {
175 List<HierarchicalConfiguration> childFields =
176 sub.configurationsAt(CONTROLLER_INFO);
177
178 for (HierarchicalConfiguration child : childFields) {
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +0900179 Annotations annotations = DefaultAnnotations.builder()
180 .set(OFCONFIG_ID, sub.getString(OFCONFIG_ID)).build();
Akihiro Yamanouchi5e5d4df2016-06-08 17:06:33 +0900181 ControllerInfo controller = new ControllerInfo(
182 IpAddress.valueOf(child.getString(IP_ADDRESS)),
183 Integer.parseInt(child.getString(PORT)),
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +0900184 child.getString(PROTOCOL), annotations);
Akihiro Yamanouchi5e5d4df2016-06-08 17:06:33 +0900185
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +0900186 log.debug("VOLT: OFCONTROLLER: PROTOCOL={}, IP={}, PORT={}, ID={} ",
187 controller.type(), controller.ip(),
188 controller.port(), controller.annotations().value(OFCONFIG_ID));
Akihiro Yamanouchi5e5d4df2016-06-08 17:06:33 +0900189 controllers.add(controller);
190 }
191 }
192 return controllers;
193 }
194
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +0900195 /**
196 * Forms XML string to change controller information.
197 *
198 * @param cfg a hierarchical configuration
199 * @param target the type of configuration
200 * @param netconfOperation operation type
201 * @param controllers list of controllers
202 * @return XML string
203 */
204 public static String createVoltControllersConfig(HierarchicalConfiguration cfg,
205 String target, String netconfOperation,
206 List<ControllerInfo> controllers) {
207 XMLConfiguration editcfg = null;
208
209 cfg.setProperty(EDIT_CONFIG_TG, target);
210 cfg.setProperty(EDIT_CONFIG_DO, netconfOperation);
211
212 List<ConfigurationNode> newControllers = new ArrayList<>();
213 for (ControllerInfo ci : controllers) {
214 XMLConfiguration controller = new XMLConfiguration();
215 controller.setRoot(new HierarchicalConfiguration.Node(OF_CONTROLLER));
216 controller.setProperty(OFCONFIG_ID, ci.annotations().value(OFCONFIG_ID));
217 controller.setProperty(CONTROLLER_INFO_ID, ci.annotations().value(OFCONFIG_ID));
218 controller.setProperty(CONTROLLER_INFO_IP, ci.ip());
219 controller.setProperty(CONTROLLER_INFO_PORT, ci.port());
220 controller.setProperty(CONTROLLER_INFO_PROTOCOL, ci.type());
221 newControllers.add(controller.getRootNode());
222 }
223 cfg.addNodes(VOLT_EDITCONFIG, newControllers);
224
225 try {
226 editcfg = (XMLConfiguration) cfg;
227 } catch (ClassCastException e) {
228 e.printStackTrace();
229 }
230 StringWriter stringWriter = new StringWriter();
231 try {
232 editcfg.save(stringWriter);
233 } catch (ConfigurationException e) {
234 e.printStackTrace();
235 }
236 String s = stringWriter.toString();
237 s = s.replace(TARGET_OPEN + target + TARGET_CLOSE,
238 TARGET_OPEN + L_ANGLE_BR + target + R_ANGLE_BR + TARGET_CLOSE);
239 return s;
240 }
241
Akihiro Yamanouchi5e5d4df2016-06-08 17:06:33 +0900242}