blob: 9d5ba5ccf4ba1e6afa1a891768088b39dac03c22 [file] [log] [blame]
Akihiro Yamanouchi5e5d4df2016-06-08 17:06:33 +09001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Akihiro Yamanouchi5e5d4df2016-06-08 17:06:33 +09003 *
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;
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +090040import java.io.StringWriter;
Akihiro Yamanouchi5e5d4df2016-06-08 17:06:33 +090041import java.nio.charset.StandardCharsets;
42import java.util.ArrayList;
43import java.util.List;
44
45import static com.google.common.base.Preconditions.checkNotNull;
xueliangf83fbc62016-09-16 10:55:28 +090046import static org.onosproject.drivers.fujitsu.FujitsuVoltXmlUtility.*;
Akihiro Yamanouchi5e5d4df2016-06-08 17:06:33 +090047import 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 Yamanouchi5e5d4df2016-06-08 17:06:33 +090060 private static final String DATA = "data";
Akihiro Yamanouchi5e5d4df2016-06-08 17:06:33 +090061 private static final String VOLT_OFCONFIG = "volt-ofconfig";
62 private static final String OF_CONTROLLERS = "of-controllers";
63 private static final String OF_CONTROLLER = "of-controller";
64 private static final String CONTROLLER_INFO = "controller-info";
Akihiro Yamanouchi5e5d4df2016-06-08 17:06:33 +090065 private static final String IP_ADDRESS = "ip-address";
66 private static final String PORT = "port";
67 private static final String PROTOCOL = "protocol";
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +090068 private static final String CONFIG = "config";
69 private static final String OFCONFIG_ID = "ofconfig-id";
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +090070 private static final String TARGET = "target";
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +090071 private static final String MERGE = "merge";
72 private static final String DEFAULT_OPERATION = "default-operation";
Akihiro Yamanouchi5e5d4df2016-06-08 17:06:33 +090073
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +090074 private static final String END_LICENSE_HEADER = "-->";
Akihiro Yamanouchi5e5d4df2016-06-08 17:06:33 +090075
76 private static final String VOLT_DATACONFIG = DATA + DOT + VOLT_NE + DOT +
77 VOLT_OFCONFIG + DOT + OF_CONTROLLERS + DOT + OF_CONTROLLER;
78
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +090079 private static final String EDIT_CONFIG_TG = EDIT_CONFIG + DOT + TARGET;
80 private static final String EDIT_CONFIG_DO = EDIT_CONFIG + DOT + DEFAULT_OPERATION;
81 private static final String CONTROLLER_INFO_ID = CONTROLLER_INFO + DOT + "id";
82 private static final String CONTROLLER_INFO_IP = CONTROLLER_INFO + DOT + IP_ADDRESS;
83 private static final String CONTROLLER_INFO_PORT = CONTROLLER_INFO + DOT + PORT;
84 private static final String CONTROLLER_INFO_PROTOCOL = CONTROLLER_INFO + DOT + PROTOCOL;
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +090085 private static final String VOLT_EDITCONFIG = EDIT_CONFIG + DOT +
86 CONFIG + DOT + VOLT_NE + DOT + VOLT_OFCONFIG + DOT + OF_CONTROLLERS;
87
Akihiro Yamanouchi5e5d4df2016-06-08 17:06:33 +090088 @Override
89 public List<ControllerInfo> getControllers() {
90 DriverHandler handler = handler();
91 NetconfController controller = handler.get(NetconfController.class);
92 MastershipService mastershipService = handler.get(MastershipService.class);
93 DeviceId ncDeviceId = handler.data().deviceId();
94 checkNotNull(controller, "Netconf controller is null");
95 List<ControllerInfo> controllers = new ArrayList<>();
96 if (mastershipService.isLocalMaster(ncDeviceId)) {
97 try {
98 StringBuilder request = new StringBuilder();
xueliangc6e47e22016-10-20 16:37:24 +090099 request.append(VOLT_NE_OPEN + VOLT_NE_NAMESPACE + ">\n");
xueliangf83fbc62016-09-16 10:55:28 +0900100 request.append(buildEmptyTag(VOLT_OFCONFIG));
Akihiro Yamanouchi5e5d4df2016-06-08 17:06:33 +0900101 request.append(VOLT_NE_CLOSE);
102
103 String reply;
xueliangf83fbc62016-09-16 10:55:28 +0900104 reply = controller
105 .getDevicesMap()
106 .get(ncDeviceId)
107 .getSession()
108 .get(request.toString(), REPORT_ALL);
Akihiro Yamanouchi5e5d4df2016-06-08 17:06:33 +0900109 log.debug("Reply XML {}", reply);
110 controllers.addAll(parseStreamVoltControllers(XmlConfigParser.
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +0900111 loadXml(new ByteArrayInputStream(reply.getBytes(StandardCharsets.UTF_8)))));
Yuta HIGUCHI234eaf32017-09-06 13:45:05 -0700112 } catch (NetconfException e) {
Akihiro Yamanouchi5e5d4df2016-06-08 17:06:33 +0900113 log.error("Cannot communicate to device {} ", ncDeviceId);
114 }
115 } else {
116 log.warn("I'm not master for {} please use master, {} to execute command",
117 ncDeviceId,
118 mastershipService.getMasterFor(ncDeviceId));
119 }
120 return ImmutableList.copyOf(controllers);
121 }
122
123 @Override
124 public void setControllers(List<ControllerInfo> controllers) {
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +0900125 DriverHandler handler = handler();
126 NetconfController controller = handler.get(NetconfController.class);
127 MastershipService mastershipService = handler.get(MastershipService.class);
128 DeviceId ncdeviceId = handler.data().deviceId();
129 checkNotNull(controller, "Netconf controller is null");
130 if (mastershipService.isLocalMaster(ncdeviceId)) {
131 try {
132 NetconfDevice device = controller.getNetconfDevice(ncdeviceId);
133 String config = createVoltControllersConfig(
134 XmlConfigParser.loadXml(getClass().
135 getResourceAsStream(RESOURCE_XML)),
136 RUNNING, MERGE, controllers);
137 device.getSession().editConfig(config.substring(
138 config.indexOf(END_LICENSE_HEADER) + END_LICENSE_HEADER.length()));
139 } catch (NetconfException e) {
xueliangf83fbc62016-09-16 10:55:28 +0900140 log.error("Cannot communicate to device {} , exception {}", ncdeviceId, e);
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +0900141 }
142 } else {
143 log.warn("I'm not master for {} please use master, {} to execute command",
144 ncdeviceId,
145 mastershipService.getMasterFor(ncdeviceId));
146 }
Akihiro Yamanouchi5e5d4df2016-06-08 17:06:33 +0900147 }
148
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +0900149
Akihiro Yamanouchi5e5d4df2016-06-08 17:06:33 +0900150 /**
151 * Parses XML string to get controller information.
152 *
153 * @param cfg a hierarchical configuration
154 * @return a list of controllers
155 */
156 private List<ControllerInfo> parseStreamVoltControllers(HierarchicalConfiguration cfg) {
157 List<ControllerInfo> controllers = new ArrayList<>();
158 List<HierarchicalConfiguration> fields =
159 cfg.configurationsAt(VOLT_DATACONFIG);
160
161 for (HierarchicalConfiguration sub : fields) {
162 List<HierarchicalConfiguration> childFields =
163 sub.configurationsAt(CONTROLLER_INFO);
164
165 for (HierarchicalConfiguration child : childFields) {
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +0900166 Annotations annotations = DefaultAnnotations.builder()
167 .set(OFCONFIG_ID, sub.getString(OFCONFIG_ID)).build();
Akihiro Yamanouchi5e5d4df2016-06-08 17:06:33 +0900168 ControllerInfo controller = new ControllerInfo(
169 IpAddress.valueOf(child.getString(IP_ADDRESS)),
170 Integer.parseInt(child.getString(PORT)),
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +0900171 child.getString(PROTOCOL), annotations);
Akihiro Yamanouchi5e5d4df2016-06-08 17:06:33 +0900172
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +0900173 log.debug("VOLT: OFCONTROLLER: PROTOCOL={}, IP={}, PORT={}, ID={} ",
174 controller.type(), controller.ip(),
175 controller.port(), controller.annotations().value(OFCONFIG_ID));
Akihiro Yamanouchi5e5d4df2016-06-08 17:06:33 +0900176 controllers.add(controller);
177 }
178 }
179 return controllers;
180 }
181
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +0900182 /**
183 * Forms XML string to change controller information.
184 *
185 * @param cfg a hierarchical configuration
186 * @param target the type of configuration
187 * @param netconfOperation operation type
188 * @param controllers list of controllers
189 * @return XML string
190 */
xueliangf83fbc62016-09-16 10:55:28 +0900191 private String createVoltControllersConfig(HierarchicalConfiguration cfg,
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +0900192 String target, String netconfOperation,
193 List<ControllerInfo> controllers) {
194 XMLConfiguration editcfg = null;
195
196 cfg.setProperty(EDIT_CONFIG_TG, target);
197 cfg.setProperty(EDIT_CONFIG_DO, netconfOperation);
198
199 List<ConfigurationNode> newControllers = new ArrayList<>();
200 for (ControllerInfo ci : controllers) {
201 XMLConfiguration controller = new XMLConfiguration();
202 controller.setRoot(new HierarchicalConfiguration.Node(OF_CONTROLLER));
203 controller.setProperty(OFCONFIG_ID, ci.annotations().value(OFCONFIG_ID));
204 controller.setProperty(CONTROLLER_INFO_ID, ci.annotations().value(OFCONFIG_ID));
205 controller.setProperty(CONTROLLER_INFO_IP, ci.ip());
206 controller.setProperty(CONTROLLER_INFO_PORT, ci.port());
207 controller.setProperty(CONTROLLER_INFO_PROTOCOL, ci.type());
208 newControllers.add(controller.getRootNode());
209 }
210 cfg.addNodes(VOLT_EDITCONFIG, newControllers);
211
212 try {
213 editcfg = (XMLConfiguration) cfg;
214 } catch (ClassCastException e) {
215 e.printStackTrace();
216 }
217 StringWriter stringWriter = new StringWriter();
218 try {
219 editcfg.save(stringWriter);
220 } catch (ConfigurationException e) {
221 e.printStackTrace();
222 }
223 String s = stringWriter.toString();
xueliangf83fbc62016-09-16 10:55:28 +0900224 String fromStr = buildStartTag(TARGET, false) + target +
225 buildEndTag(TARGET, false);
226 String toStr = buildStartTag(TARGET, false) +
227 buildEmptyTag(target, false) + buildEndTag(TARGET, false);
228 s = s.replace(fromStr, toStr);
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +0900229 return s;
230 }
231
Akihiro Yamanouchi5e5d4df2016-06-08 17:06:33 +0900232}