Akihiro Yamanouchi | 5e5d4df | 2016-06-08 17:06:33 +0900 | [diff] [blame] | 1 | /* |
| 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 | |
| 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; |
| 47 | import static org.slf4j.LoggerFactory.getLogger; |
| 48 | |
| 49 | /** |
| 50 | * Implementation to get and set parameters available in VOLT NE |
| 51 | * through the Netconf protocol. |
| 52 | */ |
| 53 | public class FujitsuVoltControllerConfig extends AbstractHandlerBehaviour |
| 54 | implements ControllerConfig { |
| 55 | |
| 56 | private final Logger log = getLogger(FujitsuVoltControllerConfig.class); |
Akihiro Yamanouchi | 54f28e2 | 2016-06-24 13:25:21 +0900 | [diff] [blame] | 57 | private static final String RESOURCE_XML = "voltcontrollers.xml"; |
Akihiro Yamanouchi | 5e5d4df | 2016-06-08 17:06:33 +0900 | [diff] [blame] | 58 | |
| 59 | private static final String DOT = "."; |
Akihiro Yamanouchi | 54f28e2 | 2016-06-24 13:25:21 +0900 | [diff] [blame] | 60 | private static final String L_ANGLE_BR = "<"; |
| 61 | private static final String R_ANGLE_BR = "/>"; |
Akihiro Yamanouchi | 5e5d4df | 2016-06-08 17:06:33 +0900 | [diff] [blame] | 62 | 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 Yamanouchi | 54f28e2 | 2016-06-24 13:25:21 +0900 | [diff] [blame] | 74 | 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 Yamanouchi | 5e5d4df | 2016-06-08 17:06:33 +0900 | [diff] [blame] | 81 | |
| 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 Yamanouchi | 54f28e2 | 2016-06-24 13:25:21 +0900 | [diff] [blame] | 85 | private static final String TARGET_OPEN = "<" + TARGET + ">"; |
| 86 | private static final String TARGET_CLOSE = "</" + TARGET + ">"; |
| 87 | private static final String END_LICENSE_HEADER = "-->"; |
Akihiro Yamanouchi | 5e5d4df | 2016-06-08 17:06:33 +0900 | [diff] [blame] | 88 | |
| 89 | private static final String VOLT_DATACONFIG = DATA + DOT + VOLT_NE + DOT + |
| 90 | VOLT_OFCONFIG + DOT + OF_CONTROLLERS + DOT + OF_CONTROLLER; |
| 91 | |
Akihiro Yamanouchi | 54f28e2 | 2016-06-24 13:25:21 +0900 | [diff] [blame] | 92 | 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 Yamanouchi | 5e5d4df | 2016-06-08 17:06:33 +0900 | [diff] [blame] | 103 | @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 Yamanouchi | 54f28e2 | 2016-06-24 13:25:21 +0900 | [diff] [blame] | 120 | getDevicesMap().get(ncDeviceId).getSession(). |
| 121 | get(request.toString(), REPORT_ALL); |
Akihiro Yamanouchi | 5e5d4df | 2016-06-08 17:06:33 +0900 | [diff] [blame] | 122 | log.debug("Reply XML {}", reply); |
| 123 | controllers.addAll(parseStreamVoltControllers(XmlConfigParser. |
Akihiro Yamanouchi | 54f28e2 | 2016-06-24 13:25:21 +0900 | [diff] [blame] | 124 | loadXml(new ByteArrayInputStream(reply.getBytes(StandardCharsets.UTF_8))))); |
Akihiro Yamanouchi | 5e5d4df | 2016-06-08 17:06:33 +0900 | [diff] [blame] | 125 | } 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 Yamanouchi | 54f28e2 | 2016-06-24 13:25:21 +0900 | [diff] [blame] | 138 | 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 Yamanouchi | 5e5d4df | 2016-06-08 17:06:33 +0900 | [diff] [blame] | 160 | } |
| 161 | |
Akihiro Yamanouchi | 54f28e2 | 2016-06-24 13:25:21 +0900 | [diff] [blame] | 162 | |
Akihiro Yamanouchi | 5e5d4df | 2016-06-08 17:06:33 +0900 | [diff] [blame] | 163 | /** |
| 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 Yamanouchi | 54f28e2 | 2016-06-24 13:25:21 +0900 | [diff] [blame] | 179 | Annotations annotations = DefaultAnnotations.builder() |
| 180 | .set(OFCONFIG_ID, sub.getString(OFCONFIG_ID)).build(); |
Akihiro Yamanouchi | 5e5d4df | 2016-06-08 17:06:33 +0900 | [diff] [blame] | 181 | ControllerInfo controller = new ControllerInfo( |
| 182 | IpAddress.valueOf(child.getString(IP_ADDRESS)), |
| 183 | Integer.parseInt(child.getString(PORT)), |
Akihiro Yamanouchi | 54f28e2 | 2016-06-24 13:25:21 +0900 | [diff] [blame] | 184 | child.getString(PROTOCOL), annotations); |
Akihiro Yamanouchi | 5e5d4df | 2016-06-08 17:06:33 +0900 | [diff] [blame] | 185 | |
Akihiro Yamanouchi | 54f28e2 | 2016-06-24 13:25:21 +0900 | [diff] [blame] | 186 | log.debug("VOLT: OFCONTROLLER: PROTOCOL={}, IP={}, PORT={}, ID={} ", |
| 187 | controller.type(), controller.ip(), |
| 188 | controller.port(), controller.annotations().value(OFCONFIG_ID)); |
Akihiro Yamanouchi | 5e5d4df | 2016-06-08 17:06:33 +0900 | [diff] [blame] | 189 | controllers.add(controller); |
| 190 | } |
| 191 | } |
| 192 | return controllers; |
| 193 | } |
| 194 | |
Akihiro Yamanouchi | 54f28e2 | 2016-06-24 13:25:21 +0900 | [diff] [blame] | 195 | /** |
| 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 Yamanouchi | 5e5d4df | 2016-06-08 17:06:33 +0900 | [diff] [blame] | 242 | } |