Akihiro Yamanouchi | 5e5d4df | 2016-06-08 17:06:33 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Akihiro Yamanouchi | 5e5d4df | 2016-06-08 17:06:33 +0900 | [diff] [blame] | 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 | |
| 17 | package org.onosproject.drivers.fujitsu; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableList; |
| 20 | import org.apache.commons.configuration.HierarchicalConfiguration; |
| 21 | import org.onosproject.drivers.utilities.XmlConfigParser; |
Akihiro Yamanouchi | 54f28e2 | 2016-06-24 13:25:21 +0900 | [diff] [blame] | 22 | import org.apache.commons.configuration.ConfigurationException; |
| 23 | import org.apache.commons.configuration.XMLConfiguration; |
| 24 | import org.apache.commons.configuration.tree.ConfigurationNode; |
Akihiro Yamanouchi | 5e5d4df | 2016-06-08 17:06:33 +0900 | [diff] [blame] | 25 | import org.onlab.packet.IpAddress; |
| 26 | import org.onosproject.mastership.MastershipService; |
Akihiro Yamanouchi | 54f28e2 | 2016-06-24 13:25:21 +0900 | [diff] [blame] | 27 | import org.onosproject.net.Annotations; |
| 28 | import org.onosproject.net.DefaultAnnotations; |
Akihiro Yamanouchi | 5e5d4df | 2016-06-08 17:06:33 +0900 | [diff] [blame] | 29 | import org.onosproject.net.DeviceId; |
| 30 | import org.onosproject.net.behaviour.ControllerConfig; |
| 31 | import org.onosproject.net.behaviour.ControllerInfo; |
| 32 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 33 | import org.onosproject.net.driver.DriverHandler; |
| 34 | import org.onosproject.netconf.NetconfController; |
Akihiro Yamanouchi | 54f28e2 | 2016-06-24 13:25:21 +0900 | [diff] [blame] | 35 | import org.onosproject.netconf.NetconfDevice; |
| 36 | import org.onosproject.netconf.NetconfException; |
Akihiro Yamanouchi | 5e5d4df | 2016-06-08 17:06:33 +0900 | [diff] [blame] | 37 | import org.slf4j.Logger; |
| 38 | |
| 39 | import java.io.ByteArrayInputStream; |
| 40 | import java.io.IOException; |
Akihiro Yamanouchi | 54f28e2 | 2016-06-24 13:25:21 +0900 | [diff] [blame] | 41 | import java.io.StringWriter; |
Akihiro Yamanouchi | 5e5d4df | 2016-06-08 17:06:33 +0900 | [diff] [blame] | 42 | import java.nio.charset.StandardCharsets; |
| 43 | import java.util.ArrayList; |
| 44 | import java.util.List; |
| 45 | |
| 46 | import static com.google.common.base.Preconditions.checkNotNull; |
xueliang | f83fbc6 | 2016-09-16 10:55:28 +0900 | [diff] [blame] | 47 | import static org.onosproject.drivers.fujitsu.FujitsuVoltXmlUtility.*; |
Akihiro Yamanouchi | 5e5d4df | 2016-06-08 17:06:33 +0900 | [diff] [blame] | 48 | import static org.slf4j.LoggerFactory.getLogger; |
| 49 | |
| 50 | /** |
| 51 | * Implementation to get and set parameters available in VOLT NE |
| 52 | * through the Netconf protocol. |
| 53 | */ |
| 54 | public class FujitsuVoltControllerConfig extends AbstractHandlerBehaviour |
| 55 | implements ControllerConfig { |
| 56 | |
| 57 | private final Logger log = getLogger(FujitsuVoltControllerConfig.class); |
Akihiro Yamanouchi | 54f28e2 | 2016-06-24 13:25:21 +0900 | [diff] [blame] | 58 | private static final String RESOURCE_XML = "voltcontrollers.xml"; |
Akihiro Yamanouchi | 5e5d4df | 2016-06-08 17:06:33 +0900 | [diff] [blame] | 59 | |
| 60 | private static final String DOT = "."; |
Akihiro Yamanouchi | 5e5d4df | 2016-06-08 17:06:33 +0900 | [diff] [blame] | 61 | private static final String DATA = "data"; |
Akihiro Yamanouchi | 5e5d4df | 2016-06-08 17:06:33 +0900 | [diff] [blame] | 62 | 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 Yamanouchi | 5e5d4df | 2016-06-08 17:06:33 +0900 | [diff] [blame] | 66 | private static final String IP_ADDRESS = "ip-address"; |
| 67 | private static final String PORT = "port"; |
| 68 | private static final String PROTOCOL = "protocol"; |
Akihiro Yamanouchi | 54f28e2 | 2016-06-24 13:25:21 +0900 | [diff] [blame] | 69 | private static final String CONFIG = "config"; |
| 70 | private static final String OFCONFIG_ID = "ofconfig-id"; |
Akihiro Yamanouchi | 54f28e2 | 2016-06-24 13:25:21 +0900 | [diff] [blame] | 71 | private static final String TARGET = "target"; |
Akihiro Yamanouchi | 54f28e2 | 2016-06-24 13:25:21 +0900 | [diff] [blame] | 72 | private static final String MERGE = "merge"; |
| 73 | private static final String DEFAULT_OPERATION = "default-operation"; |
Akihiro Yamanouchi | 5e5d4df | 2016-06-08 17:06:33 +0900 | [diff] [blame] | 74 | |
Akihiro Yamanouchi | 54f28e2 | 2016-06-24 13:25:21 +0900 | [diff] [blame] | 75 | private static final String END_LICENSE_HEADER = "-->"; |
Akihiro Yamanouchi | 5e5d4df | 2016-06-08 17:06:33 +0900 | [diff] [blame] | 76 | |
| 77 | private static final String VOLT_DATACONFIG = DATA + DOT + VOLT_NE + DOT + |
| 78 | VOLT_OFCONFIG + DOT + OF_CONTROLLERS + DOT + OF_CONTROLLER; |
| 79 | |
Akihiro Yamanouchi | 54f28e2 | 2016-06-24 13:25:21 +0900 | [diff] [blame] | 80 | 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 Yamanouchi | 54f28e2 | 2016-06-24 13:25:21 +0900 | [diff] [blame] | 86 | private static final String VOLT_EDITCONFIG = EDIT_CONFIG + DOT + |
| 87 | CONFIG + DOT + VOLT_NE + DOT + VOLT_OFCONFIG + DOT + OF_CONTROLLERS; |
| 88 | |
Akihiro Yamanouchi | 5e5d4df | 2016-06-08 17:06:33 +0900 | [diff] [blame] | 89 | @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(); |
xueliang | c6e47e2 | 2016-10-20 16:37:24 +0900 | [diff] [blame] | 100 | request.append(VOLT_NE_OPEN + VOLT_NE_NAMESPACE + ">\n"); |
xueliang | f83fbc6 | 2016-09-16 10:55:28 +0900 | [diff] [blame] | 101 | request.append(buildEmptyTag(VOLT_OFCONFIG)); |
Akihiro Yamanouchi | 5e5d4df | 2016-06-08 17:06:33 +0900 | [diff] [blame] | 102 | request.append(VOLT_NE_CLOSE); |
| 103 | |
| 104 | String reply; |
xueliang | f83fbc6 | 2016-09-16 10:55:28 +0900 | [diff] [blame] | 105 | reply = controller |
| 106 | .getDevicesMap() |
| 107 | .get(ncDeviceId) |
| 108 | .getSession() |
| 109 | .get(request.toString(), REPORT_ALL); |
Akihiro Yamanouchi | 5e5d4df | 2016-06-08 17:06:33 +0900 | [diff] [blame] | 110 | log.debug("Reply XML {}", reply); |
| 111 | controllers.addAll(parseStreamVoltControllers(XmlConfigParser. |
Akihiro Yamanouchi | 54f28e2 | 2016-06-24 13:25:21 +0900 | [diff] [blame] | 112 | loadXml(new ByteArrayInputStream(reply.getBytes(StandardCharsets.UTF_8))))); |
Akihiro Yamanouchi | 5e5d4df | 2016-06-08 17:06:33 +0900 | [diff] [blame] | 113 | } 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 Yamanouchi | 54f28e2 | 2016-06-24 13:25:21 +0900 | [diff] [blame] | 126 | 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) { |
xueliang | f83fbc6 | 2016-09-16 10:55:28 +0900 | [diff] [blame] | 141 | log.error("Cannot communicate to device {} , exception {}", ncdeviceId, e); |
Akihiro Yamanouchi | 54f28e2 | 2016-06-24 13:25:21 +0900 | [diff] [blame] | 142 | } |
| 143 | } else { |
| 144 | log.warn("I'm not master for {} please use master, {} to execute command", |
| 145 | ncdeviceId, |
| 146 | mastershipService.getMasterFor(ncdeviceId)); |
| 147 | } |
Akihiro Yamanouchi | 5e5d4df | 2016-06-08 17:06:33 +0900 | [diff] [blame] | 148 | } |
| 149 | |
Akihiro Yamanouchi | 54f28e2 | 2016-06-24 13:25:21 +0900 | [diff] [blame] | 150 | |
Akihiro Yamanouchi | 5e5d4df | 2016-06-08 17:06:33 +0900 | [diff] [blame] | 151 | /** |
| 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 Yamanouchi | 54f28e2 | 2016-06-24 13:25:21 +0900 | [diff] [blame] | 167 | Annotations annotations = DefaultAnnotations.builder() |
| 168 | .set(OFCONFIG_ID, sub.getString(OFCONFIG_ID)).build(); |
Akihiro Yamanouchi | 5e5d4df | 2016-06-08 17:06:33 +0900 | [diff] [blame] | 169 | ControllerInfo controller = new ControllerInfo( |
| 170 | IpAddress.valueOf(child.getString(IP_ADDRESS)), |
| 171 | Integer.parseInt(child.getString(PORT)), |
Akihiro Yamanouchi | 54f28e2 | 2016-06-24 13:25:21 +0900 | [diff] [blame] | 172 | child.getString(PROTOCOL), annotations); |
Akihiro Yamanouchi | 5e5d4df | 2016-06-08 17:06:33 +0900 | [diff] [blame] | 173 | |
Akihiro Yamanouchi | 54f28e2 | 2016-06-24 13:25:21 +0900 | [diff] [blame] | 174 | log.debug("VOLT: OFCONTROLLER: PROTOCOL={}, IP={}, PORT={}, ID={} ", |
| 175 | controller.type(), controller.ip(), |
| 176 | controller.port(), controller.annotations().value(OFCONFIG_ID)); |
Akihiro Yamanouchi | 5e5d4df | 2016-06-08 17:06:33 +0900 | [diff] [blame] | 177 | controllers.add(controller); |
| 178 | } |
| 179 | } |
| 180 | return controllers; |
| 181 | } |
| 182 | |
Akihiro Yamanouchi | 54f28e2 | 2016-06-24 13:25:21 +0900 | [diff] [blame] | 183 | /** |
| 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 | */ |
xueliang | f83fbc6 | 2016-09-16 10:55:28 +0900 | [diff] [blame] | 192 | private String createVoltControllersConfig(HierarchicalConfiguration cfg, |
Akihiro Yamanouchi | 54f28e2 | 2016-06-24 13:25:21 +0900 | [diff] [blame] | 193 | 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) { |
Ray Milkey | fd4f8d3 | 2018-01-17 15:24:52 -0800 | [diff] [blame^] | 216 | return null; |
Akihiro Yamanouchi | 54f28e2 | 2016-06-24 13:25:21 +0900 | [diff] [blame] | 217 | } |
| 218 | StringWriter stringWriter = new StringWriter(); |
| 219 | try { |
| 220 | editcfg.save(stringWriter); |
| 221 | } catch (ConfigurationException e) { |
Ray Milkey | fd4f8d3 | 2018-01-17 15:24:52 -0800 | [diff] [blame^] | 222 | return null; |
Akihiro Yamanouchi | 54f28e2 | 2016-06-24 13:25:21 +0900 | [diff] [blame] | 223 | } |
| 224 | String s = stringWriter.toString(); |
xueliang | f83fbc6 | 2016-09-16 10:55:28 +0900 | [diff] [blame] | 225 | 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 Yamanouchi | 54f28e2 | 2016-06-24 13:25:21 +0900 | [diff] [blame] | 230 | return s; |
| 231 | } |
| 232 | |
Akihiro Yamanouchi | 5e5d4df | 2016-06-08 17:06:33 +0900 | [diff] [blame] | 233 | } |