blob: 07edd32e9560171006e54adf48471d4357c71c60 [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;
xueliangf83fbc62016-09-16 10:55:28 +090047import static org.onosproject.drivers.fujitsu.FujitsuVoltXmlUtility.*;
Akihiro Yamanouchi5e5d4df2016-06-08 17:06:33 +090048import static org.slf4j.LoggerFactory.getLogger;
49
50/**
51 * Implementation to get and set parameters available in VOLT NE
52 * through the Netconf protocol.
53 */
54public class FujitsuVoltControllerConfig extends AbstractHandlerBehaviour
55 implements ControllerConfig {
56
57 private final Logger log = getLogger(FujitsuVoltControllerConfig.class);
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +090058 private static final String RESOURCE_XML = "voltcontrollers.xml";
Akihiro Yamanouchi5e5d4df2016-06-08 17:06:33 +090059
60 private static final String DOT = ".";
Akihiro Yamanouchi5e5d4df2016-06-08 17:06:33 +090061 private static final String DATA = "data";
Akihiro Yamanouchi5e5d4df2016-06-08 17:06:33 +090062 private static final String VOLT_OFCONFIG = "volt-ofconfig";
63 private static final String OF_CONTROLLERS = "of-controllers";
64 private static final String OF_CONTROLLER = "of-controller";
65 private static final String CONTROLLER_INFO = "controller-info";
Akihiro Yamanouchi5e5d4df2016-06-08 17:06:33 +090066 private static final String IP_ADDRESS = "ip-address";
67 private static final String PORT = "port";
68 private static final String PROTOCOL = "protocol";
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +090069 private static final String CONFIG = "config";
70 private static final String OFCONFIG_ID = "ofconfig-id";
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +090071 private static final String TARGET = "target";
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +090072 private static final String MERGE = "merge";
73 private static final String DEFAULT_OPERATION = "default-operation";
Akihiro Yamanouchi5e5d4df2016-06-08 17:06:33 +090074
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +090075 private static final String END_LICENSE_HEADER = "-->";
Akihiro Yamanouchi5e5d4df2016-06-08 17:06:33 +090076
77 private static final String VOLT_DATACONFIG = DATA + DOT + VOLT_NE + DOT +
78 VOLT_OFCONFIG + DOT + OF_CONTROLLERS + DOT + OF_CONTROLLER;
79
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +090080 private static final String EDIT_CONFIG_TG = EDIT_CONFIG + DOT + TARGET;
81 private static final String EDIT_CONFIG_DO = EDIT_CONFIG + DOT + DEFAULT_OPERATION;
82 private static final String CONTROLLER_INFO_ID = CONTROLLER_INFO + DOT + "id";
83 private static final String CONTROLLER_INFO_IP = CONTROLLER_INFO + DOT + IP_ADDRESS;
84 private static final String CONTROLLER_INFO_PORT = CONTROLLER_INFO + DOT + PORT;
85 private static final String CONTROLLER_INFO_PROTOCOL = CONTROLLER_INFO + DOT + PROTOCOL;
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +090086 private static final String VOLT_EDITCONFIG = EDIT_CONFIG + DOT +
87 CONFIG + DOT + VOLT_NE + DOT + VOLT_OFCONFIG + DOT + OF_CONTROLLERS;
88
Akihiro Yamanouchi5e5d4df2016-06-08 17:06:33 +090089 @Override
90 public List<ControllerInfo> getControllers() {
91 DriverHandler handler = handler();
92 NetconfController controller = handler.get(NetconfController.class);
93 MastershipService mastershipService = handler.get(MastershipService.class);
94 DeviceId ncDeviceId = handler.data().deviceId();
95 checkNotNull(controller, "Netconf controller is null");
96 List<ControllerInfo> controllers = new ArrayList<>();
97 if (mastershipService.isLocalMaster(ncDeviceId)) {
98 try {
99 StringBuilder request = new StringBuilder();
100 request.append(VOLT_NE_OPEN).append(VOLT_NE_NAMESPACE).append(">\n");
xueliangf83fbc62016-09-16 10:55:28 +0900101 request.append(buildEmptyTag(VOLT_OFCONFIG));
Akihiro Yamanouchi5e5d4df2016-06-08 17:06:33 +0900102 request.append(VOLT_NE_CLOSE);
103
104 String reply;
xueliangf83fbc62016-09-16 10:55:28 +0900105 reply = controller
106 .getDevicesMap()
107 .get(ncDeviceId)
108 .getSession()
109 .get(request.toString(), REPORT_ALL);
Akihiro Yamanouchi5e5d4df2016-06-08 17:06:33 +0900110 log.debug("Reply XML {}", reply);
111 controllers.addAll(parseStreamVoltControllers(XmlConfigParser.
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +0900112 loadXml(new ByteArrayInputStream(reply.getBytes(StandardCharsets.UTF_8)))));
Akihiro Yamanouchi5e5d4df2016-06-08 17:06:33 +0900113 } catch (IOException e) {
114 log.error("Cannot communicate to device {} ", ncDeviceId);
115 }
116 } else {
117 log.warn("I'm not master for {} please use master, {} to execute command",
118 ncDeviceId,
119 mastershipService.getMasterFor(ncDeviceId));
120 }
121 return ImmutableList.copyOf(controllers);
122 }
123
124 @Override
125 public void setControllers(List<ControllerInfo> controllers) {
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +0900126 DriverHandler handler = handler();
127 NetconfController controller = handler.get(NetconfController.class);
128 MastershipService mastershipService = handler.get(MastershipService.class);
129 DeviceId ncdeviceId = handler.data().deviceId();
130 checkNotNull(controller, "Netconf controller is null");
131 if (mastershipService.isLocalMaster(ncdeviceId)) {
132 try {
133 NetconfDevice device = controller.getNetconfDevice(ncdeviceId);
134 String config = createVoltControllersConfig(
135 XmlConfigParser.loadXml(getClass().
136 getResourceAsStream(RESOURCE_XML)),
137 RUNNING, MERGE, controllers);
138 device.getSession().editConfig(config.substring(
139 config.indexOf(END_LICENSE_HEADER) + END_LICENSE_HEADER.length()));
140 } catch (NetconfException e) {
xueliangf83fbc62016-09-16 10:55:28 +0900141 log.error("Cannot communicate to device {} , exception {}", ncdeviceId, e);
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +0900142 }
143 } else {
144 log.warn("I'm not master for {} please use master, {} to execute command",
145 ncdeviceId,
146 mastershipService.getMasterFor(ncdeviceId));
147 }
Akihiro Yamanouchi5e5d4df2016-06-08 17:06:33 +0900148 }
149
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +0900150
Akihiro Yamanouchi5e5d4df2016-06-08 17:06:33 +0900151 /**
152 * Parses XML string to get controller information.
153 *
154 * @param cfg a hierarchical configuration
155 * @return a list of controllers
156 */
157 private List<ControllerInfo> parseStreamVoltControllers(HierarchicalConfiguration cfg) {
158 List<ControllerInfo> controllers = new ArrayList<>();
159 List<HierarchicalConfiguration> fields =
160 cfg.configurationsAt(VOLT_DATACONFIG);
161
162 for (HierarchicalConfiguration sub : fields) {
163 List<HierarchicalConfiguration> childFields =
164 sub.configurationsAt(CONTROLLER_INFO);
165
166 for (HierarchicalConfiguration child : childFields) {
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +0900167 Annotations annotations = DefaultAnnotations.builder()
168 .set(OFCONFIG_ID, sub.getString(OFCONFIG_ID)).build();
Akihiro Yamanouchi5e5d4df2016-06-08 17:06:33 +0900169 ControllerInfo controller = new ControllerInfo(
170 IpAddress.valueOf(child.getString(IP_ADDRESS)),
171 Integer.parseInt(child.getString(PORT)),
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +0900172 child.getString(PROTOCOL), annotations);
Akihiro Yamanouchi5e5d4df2016-06-08 17:06:33 +0900173
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +0900174 log.debug("VOLT: OFCONTROLLER: PROTOCOL={}, IP={}, PORT={}, ID={} ",
175 controller.type(), controller.ip(),
176 controller.port(), controller.annotations().value(OFCONFIG_ID));
Akihiro Yamanouchi5e5d4df2016-06-08 17:06:33 +0900177 controllers.add(controller);
178 }
179 }
180 return controllers;
181 }
182
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +0900183 /**
184 * Forms XML string to change controller information.
185 *
186 * @param cfg a hierarchical configuration
187 * @param target the type of configuration
188 * @param netconfOperation operation type
189 * @param controllers list of controllers
190 * @return XML string
191 */
xueliangf83fbc62016-09-16 10:55:28 +0900192 private String createVoltControllersConfig(HierarchicalConfiguration cfg,
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +0900193 String target, String netconfOperation,
194 List<ControllerInfo> controllers) {
195 XMLConfiguration editcfg = null;
196
197 cfg.setProperty(EDIT_CONFIG_TG, target);
198 cfg.setProperty(EDIT_CONFIG_DO, netconfOperation);
199
200 List<ConfigurationNode> newControllers = new ArrayList<>();
201 for (ControllerInfo ci : controllers) {
202 XMLConfiguration controller = new XMLConfiguration();
203 controller.setRoot(new HierarchicalConfiguration.Node(OF_CONTROLLER));
204 controller.setProperty(OFCONFIG_ID, ci.annotations().value(OFCONFIG_ID));
205 controller.setProperty(CONTROLLER_INFO_ID, ci.annotations().value(OFCONFIG_ID));
206 controller.setProperty(CONTROLLER_INFO_IP, ci.ip());
207 controller.setProperty(CONTROLLER_INFO_PORT, ci.port());
208 controller.setProperty(CONTROLLER_INFO_PROTOCOL, ci.type());
209 newControllers.add(controller.getRootNode());
210 }
211 cfg.addNodes(VOLT_EDITCONFIG, newControllers);
212
213 try {
214 editcfg = (XMLConfiguration) cfg;
215 } catch (ClassCastException e) {
216 e.printStackTrace();
217 }
218 StringWriter stringWriter = new StringWriter();
219 try {
220 editcfg.save(stringWriter);
221 } catch (ConfigurationException e) {
222 e.printStackTrace();
223 }
224 String s = stringWriter.toString();
xueliangf83fbc62016-09-16 10:55:28 +0900225 String fromStr = buildStartTag(TARGET, false) + target +
226 buildEndTag(TARGET, false);
227 String toStr = buildStartTag(TARGET, false) +
228 buildEmptyTag(target, false) + buildEndTag(TARGET, false);
229 s = s.replace(fromStr, toStr);
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +0900230 return s;
231 }
232
Akihiro Yamanouchi5e5d4df2016-06-08 17:06:33 +0900233}