Akihiro Yamanouchi | 8d3a9d3 | 2016-07-12 11:41:44 +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.ImmutableSet; |
| 20 | import org.onosproject.mastership.MastershipService; |
| 21 | import org.onosproject.net.DeviceId; |
| 22 | import org.onosproject.drivers.fujitsu.behaviour.VoltOnuConfig; |
| 23 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 24 | import org.onosproject.net.driver.DriverHandler; |
| 25 | import org.onosproject.netconf.NetconfController; |
| 26 | import org.slf4j.Logger; |
| 27 | |
| 28 | import java.io.IOException; |
| 29 | import java.util.Set; |
| 30 | |
| 31 | import static com.google.common.base.Preconditions.checkNotNull; |
| 32 | import static org.onosproject.drivers.fujitsu.FujitsuVoltXmlUtility.*; |
| 33 | import static org.slf4j.LoggerFactory.getLogger; |
| 34 | |
| 35 | /** |
| 36 | * Implementation to get and set parameters available in vOLT |
| 37 | * through the Netconf protocol. |
| 38 | */ |
| 39 | public class FujitsuVoltOnuConfig extends AbstractHandlerBehaviour |
| 40 | implements VoltOnuConfig { |
| 41 | |
| 42 | private final Logger log = getLogger(FujitsuVoltOnuConfig.class); |
| 43 | private final Set<String> onuConfigParams = ImmutableSet.of( |
| 44 | "admin-state", "pm-enable", "fec-enable", |
| 45 | "security-enable", "password"); |
| 46 | private static final String VOLT_ONUS = "volt-onus"; |
| 47 | private static final String ONUS_PERLINK = "onus-perlink"; |
| 48 | private static final String ONUS_LIST = "onus-list"; |
| 49 | private static final String ONU_INFO = "onu-info"; |
| 50 | private static final String ONU_SET_CONFIG = "onu-set-config"; |
| 51 | private static final String CONFIG_INFO = "config-info"; |
| 52 | private static final String VOLT_STATISTICS = "volt-statistics"; |
| 53 | private static final String ONU_STATISTICS = "onu-statistics"; |
| 54 | private static final String ONU_ETH_STATS = "onu-eth-stats"; |
| 55 | private static final String ETH_STATS = "eth-stats"; |
| 56 | private static final String ONU_GEM_STATS = "onu-gem-stats"; |
| 57 | private static final String GEM_STATS = "gem-stats"; |
| 58 | private int pon; |
| 59 | private int onu; |
| 60 | |
| 61 | |
| 62 | @Override |
| 63 | public String getOnus(String target) { |
| 64 | DriverHandler handler = handler(); |
| 65 | NetconfController controller = handler.get(NetconfController.class); |
| 66 | MastershipService mastershipService = handler.get(MastershipService.class); |
| 67 | DeviceId ncDeviceId = handler.data().deviceId(); |
| 68 | checkNotNull(controller, "Netconf controller is null"); |
| 69 | String reply = null; |
| 70 | String[] onuId = null; |
| 71 | |
| 72 | if (!mastershipService.isLocalMaster(ncDeviceId)) { |
| 73 | log.warn("Not master for {} Use {} to execute command", |
| 74 | ncDeviceId, |
| 75 | mastershipService.getMasterFor(ncDeviceId)); |
| 76 | return reply; |
| 77 | } |
| 78 | |
| 79 | if (target != null) { |
| 80 | onuId = target.split(HYPHEN); |
| 81 | if (onuId.length > 2) { |
| 82 | log.error("Invalid number of arguments"); |
| 83 | return reply; |
| 84 | } |
| 85 | try { |
| 86 | pon = Integer.parseInt(onuId[0]); |
| 87 | if (onuId.length > 1) { |
| 88 | onu = Integer.parseInt(onuId[1]); |
| 89 | } |
| 90 | } catch (NumberFormatException e) { |
| 91 | log.error("Non-number input"); |
| 92 | return reply; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | try { |
| 97 | StringBuilder request = new StringBuilder(); |
| 98 | request.append(VOLT_NE_OPEN).append(VOLT_NE_NAMESPACE); |
| 99 | request.append(ANGLE_RIGHT).append(NEW_LINE); |
| 100 | if (onuId != null) { |
| 101 | request.append(buildStartTag(VOLT_ONUS)); |
| 102 | request.append(buildStartTag(ONUS_PERLINK)); |
| 103 | request.append(buildStartTag(PONLINK_ID, false)); |
| 104 | request.append(onuId[0]); |
| 105 | request.append(buildEndTag(PONLINK_ID)); |
| 106 | if (onuId.length > 1) { |
| 107 | request.append(buildStartTag(ONUS_LIST)); |
| 108 | request.append(buildStartTag(ONU_INFO)); |
| 109 | request.append(buildStartTag(ONU_ID, false)); |
| 110 | request.append(onuId[1]); |
| 111 | request.append(buildEndTag(ONU_ID)); |
| 112 | request.append(buildEndTag(ONU_INFO)); |
| 113 | request.append(buildEndTag(ONUS_LIST)); |
| 114 | } |
| 115 | request.append(buildEndTag(ONUS_PERLINK)); |
| 116 | request.append(buildEndTag(VOLT_ONUS)); |
| 117 | } else { |
| 118 | request.append(buildEmptyTag(VOLT_ONUS)); |
| 119 | } |
| 120 | request.append(VOLT_NE_CLOSE); |
| 121 | |
| 122 | reply = controller. |
| 123 | getDevicesMap().get(ncDeviceId).getSession(). |
| 124 | get(request.toString(), REPORT_ALL); |
| 125 | } catch (IOException e) { |
| 126 | log.error("Cannot communicate to device {} exception ", ncDeviceId, e); |
| 127 | } |
| 128 | return reply; |
| 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public String setOnu(String target) { |
| 133 | DriverHandler handler = handler(); |
| 134 | NetconfController controller = handler.get(NetconfController.class); |
| 135 | MastershipService mastershipService = handler.get(MastershipService.class); |
| 136 | DeviceId ncDeviceId = handler.data().deviceId(); |
| 137 | checkNotNull(controller, "Netconf controller is null"); |
| 138 | String reply = null; |
| 139 | |
| 140 | if (!mastershipService.isLocalMaster(ncDeviceId)) { |
| 141 | log.warn("Not master for {} Use {} to execute command", |
| 142 | ncDeviceId, |
| 143 | mastershipService.getMasterFor(ncDeviceId)); |
| 144 | return reply; |
| 145 | } |
| 146 | |
| 147 | String[] data = target.split(COLON); |
| 148 | if (data.length != 3) { |
| 149 | log.error("Invalid number of arguments"); |
| 150 | return reply; |
| 151 | } |
| 152 | |
| 153 | String[] onuId = data[0].split(HYPHEN); |
| 154 | if (onuId.length != 2) { |
| 155 | log.error("Invalid ONU identifier"); |
| 156 | return reply; |
| 157 | } |
| 158 | |
| 159 | try { |
| 160 | pon = Integer.parseInt(onuId[0]); |
| 161 | onu = Integer.parseInt(onuId[1]); |
| 162 | } catch (NumberFormatException e) { |
| 163 | log.error("Non-number input"); |
| 164 | return reply; |
| 165 | } |
| 166 | |
| 167 | if (!onuConfigParams.contains(data[1])) { |
| 168 | log.error("Unsupported parameter: " + data[1]); |
| 169 | return reply; |
| 170 | } |
| 171 | |
| 172 | try { |
| 173 | StringBuilder request = new StringBuilder(); |
| 174 | request.append(ANGLE_LEFT).append(ONU_SET_CONFIG).append(SPACE); |
| 175 | request.append(VOLT_NE_NAMESPACE).append(ANGLE_RIGHT).append(NEW_LINE); |
| 176 | request.append(buildStartTag(PONLINK_ID, false)); |
| 177 | request.append(onuId[0]); |
| 178 | request.append(buildEndTag(PONLINK_ID)); |
| 179 | request.append(buildStartTag(ONU_ID, false)); |
| 180 | request.append(onuId[1]); |
| 181 | request.append(buildEndTag(ONU_ID)); |
| 182 | request.append(buildStartTag(CONFIG_INFO)); |
| 183 | request.append(buildStartTag(data[1], false)); |
| 184 | request.append(data[2]); |
| 185 | request.append(buildEndTag(data[1])); |
| 186 | request.append(buildEndTag(CONFIG_INFO)); |
| 187 | request.append(buildEndTag(ONU_SET_CONFIG)); |
| 188 | |
| 189 | reply = controller. |
| 190 | getDevicesMap().get(ncDeviceId).getSession(). |
| 191 | doWrappedRpc(request.toString()); |
| 192 | } catch (IOException e) { |
| 193 | log.error("Cannot communicate to device {} exception ", ncDeviceId, e); |
| 194 | } |
| 195 | return reply; |
| 196 | } |
| 197 | |
| 198 | @Override |
| 199 | public String getOnuStatistics(String target) { |
| 200 | DriverHandler handler = handler(); |
| 201 | NetconfController controller = handler.get(NetconfController.class); |
| 202 | MastershipService mastershipService = handler.get(MastershipService.class); |
| 203 | DeviceId ncDeviceId = handler.data().deviceId(); |
| 204 | checkNotNull(controller, "Netconf controller is null"); |
| 205 | String reply = null; |
| 206 | String[] onuId = null; |
| 207 | |
| 208 | if (!mastershipService.isLocalMaster(ncDeviceId)) { |
| 209 | log.warn("Not master for {} Use {} to execute command", |
| 210 | ncDeviceId, |
| 211 | mastershipService.getMasterFor(ncDeviceId)); |
| 212 | return reply; |
| 213 | } |
| 214 | |
| 215 | if (target != null) { |
| 216 | onuId = target.split(HYPHEN); |
| 217 | if (onuId.length > 2) { |
| 218 | log.error("Invalid number of arguments:" + onuId.length); |
| 219 | return reply; |
| 220 | } |
| 221 | try { |
| 222 | pon = Integer.parseInt(onuId[0]); |
| 223 | if (onuId.length > 1) { |
| 224 | onu = Integer.parseInt(onuId[1]); |
| 225 | } |
| 226 | } catch (NumberFormatException e) { |
| 227 | log.error("Non-number input"); |
| 228 | return reply; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | try { |
| 233 | StringBuilder request = new StringBuilder(); |
| 234 | request.append(VOLT_NE_OPEN).append(VOLT_NE_NAMESPACE); |
| 235 | request.append(ANGLE_RIGHT).append(NEW_LINE); |
| 236 | request.append(buildStartTag(VOLT_STATISTICS)); |
| 237 | if (onuId != null) { |
| 238 | request.append(buildStartTag(ONU_STATISTICS)); |
| 239 | request.append(buildStartTag(ONU_GEM_STATS)); |
| 240 | request.append(buildStartTag(GEM_STATS)); |
| 241 | request.append(buildStartTag(PONLINK_ID, false)); |
| 242 | request.append(onuId[0]); |
| 243 | request.append(buildEndTag(PONLINK_ID)); |
| 244 | if (onuId.length > 1) { |
| 245 | request.append(buildStartTag(ONU_ID, false)); |
| 246 | request.append(onuId[1]); |
| 247 | request.append(buildEndTag(ONU_ID)); |
| 248 | } |
| 249 | request.append(buildEndTag(GEM_STATS)); |
| 250 | request.append(buildEndTag(ONU_GEM_STATS)); |
| 251 | |
| 252 | request.append(buildStartTag(ONU_ETH_STATS)); |
| 253 | request.append(buildStartTag(ETH_STATS)); |
| 254 | request.append(buildStartTag(PONLINK_ID, false)); |
| 255 | request.append(onuId[0]); |
| 256 | request.append(buildEndTag(PONLINK_ID)); |
| 257 | if (onuId.length > 1) { |
| 258 | request.append(buildStartTag(ONU_ID, false)); |
| 259 | request.append(onuId[1]); |
| 260 | request.append(buildEndTag(ONU_ID)); |
| 261 | } |
| 262 | request.append(buildEndTag(ETH_STATS)); |
| 263 | request.append(buildEndTag(ONU_ETH_STATS)); |
| 264 | |
| 265 | request.append(buildEndTag(ONU_STATISTICS)); |
| 266 | } else { |
| 267 | request.append(buildEmptyTag(ONU_STATISTICS)); |
| 268 | } |
| 269 | request.append(buildEndTag(VOLT_STATISTICS)); |
| 270 | request.append(VOLT_NE_CLOSE); |
| 271 | |
| 272 | reply = controller. |
| 273 | getDevicesMap().get(ncDeviceId).getSession(). |
| 274 | get(request.toString(), REPORT_ALL); |
| 275 | } catch (IOException e) { |
| 276 | log.error("Cannot communicate to device {} exception ", ncDeviceId, e); |
| 277 | } |
| 278 | return reply; |
| 279 | } |
| 280 | |
| 281 | } |