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 | |
xueliang | ffe73e4 | 2016-09-27 10:38:56 +0900 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableSet; |
Akihiro Yamanouchi | 8d3a9d3 | 2016-07-12 11:41:44 +0900 | [diff] [blame] | 20 | import org.onosproject.mastership.MastershipService; |
| 21 | import org.onosproject.net.DeviceId; |
| 22 | import org.onosproject.drivers.fujitsu.behaviour.VoltOnuOperConfig; |
| 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; |
xueliang | ffe73e4 | 2016-09-27 10:38:56 +0900 | [diff] [blame] | 29 | import java.util.Set; |
Akihiro Yamanouchi | 8d3a9d3 | 2016-07-12 11:41:44 +0900 | [diff] [blame] | 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 take actions on ONU available in vOLT |
| 37 | * through the Netconf protocol. |
| 38 | */ |
| 39 | public class FujitsuVoltOnuOperConfig extends AbstractHandlerBehaviour |
| 40 | implements VoltOnuOperConfig { |
| 41 | |
| 42 | private final Logger log = getLogger(FujitsuVoltOnuOperConfig.class); |
| 43 | private static final String ONU_REBOOT = "onu-reboot"; |
| 44 | private static final String ONU_ETHPORT_LOOPBACK = "onu-ethport-loopback"; |
| 45 | private static final String ETHPORT_ID = "ethport-id"; |
xueliang | ffe73e4 | 2016-09-27 10:38:56 +0900 | [diff] [blame] | 46 | private static final String LOOPBACK_MODE = "mode"; |
| 47 | private static final Set<String> LOOPBACKMODES = |
| 48 | ImmutableSet.of("operate", "release"); |
Akihiro Yamanouchi | 8d3a9d3 | 2016-07-12 11:41:44 +0900 | [diff] [blame] | 49 | |
| 50 | @Override |
| 51 | public String rebootOnu(String target) { |
| 52 | DriverHandler handler = handler(); |
| 53 | NetconfController controller = handler.get(NetconfController.class); |
| 54 | MastershipService mastershipService = handler.get(MastershipService.class); |
| 55 | DeviceId ncDeviceId = handler.data().deviceId(); |
| 56 | checkNotNull(controller, "Netconf controller is null"); |
| 57 | String reply = null; |
| 58 | String[] onuId = null; |
| 59 | |
| 60 | if (!mastershipService.isLocalMaster(ncDeviceId)) { |
| 61 | log.warn("Not master for {} Use {} to execute command", |
| 62 | ncDeviceId, |
| 63 | mastershipService.getMasterFor(ncDeviceId)); |
xueliang | ffe73e4 | 2016-09-27 10:38:56 +0900 | [diff] [blame] | 64 | return null; |
Akihiro Yamanouchi | 8d3a9d3 | 2016-07-12 11:41:44 +0900 | [diff] [blame] | 65 | } |
| 66 | |
xueliang | ffe73e4 | 2016-09-27 10:38:56 +0900 | [diff] [blame] | 67 | onuId = checkIdString(target, TWO); |
| 68 | if (onuId == null) { |
| 69 | log.error("Invalid ONU identifier {}", target); |
| 70 | return null; |
Akihiro Yamanouchi | 8d3a9d3 | 2016-07-12 11:41:44 +0900 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | try { |
| 74 | StringBuilder request = new StringBuilder(); |
xueliang | ffe73e4 | 2016-09-27 10:38:56 +0900 | [diff] [blame] | 75 | request.append(ANGLE_LEFT + ONU_REBOOT + SPACE); |
| 76 | request.append(VOLT_NE_NAMESPACE + ANGLE_RIGHT + NEW_LINE); |
Akihiro Yamanouchi | 8d3a9d3 | 2016-07-12 11:41:44 +0900 | [diff] [blame] | 77 | |
xueliang | ffe73e4 | 2016-09-27 10:38:56 +0900 | [diff] [blame] | 78 | request.append(buildStartTag(PONLINK_ID, false)) |
| 79 | .append(onuId[FIRST_PART]) |
| 80 | .append(buildEndTag(PONLINK_ID)) |
| 81 | .append(buildStartTag(ONU_ID, false)) |
| 82 | .append(onuId[SECOND_PART]) |
| 83 | .append(buildEndTag(ONU_ID)) |
| 84 | .append(buildEndTag(ONU_REBOOT)); |
| 85 | |
| 86 | reply = controller |
| 87 | .getDevicesMap() |
| 88 | .get(ncDeviceId) |
| 89 | .getSession() |
| 90 | .doWrappedRpc(request.toString()); |
Akihiro Yamanouchi | 8d3a9d3 | 2016-07-12 11:41:44 +0900 | [diff] [blame] | 91 | } catch (IOException e) { |
xueliang | ffe73e4 | 2016-09-27 10:38:56 +0900 | [diff] [blame] | 92 | log.error("Cannot communicate to device {} exception {}", ncDeviceId, e); |
Akihiro Yamanouchi | 8d3a9d3 | 2016-07-12 11:41:44 +0900 | [diff] [blame] | 93 | } |
| 94 | return reply; |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public String loopbackEthOnu(String target) { |
| 99 | DriverHandler handler = handler(); |
| 100 | NetconfController controller = handler.get(NetconfController.class); |
| 101 | MastershipService mastershipService = handler.get(MastershipService.class); |
| 102 | DeviceId ncDeviceId = handler.data().deviceId(); |
| 103 | checkNotNull(controller, "Netconf controller is null"); |
| 104 | String reply = null; |
xueliang | ffe73e4 | 2016-09-27 10:38:56 +0900 | [diff] [blame] | 105 | String[] data = null; |
Akihiro Yamanouchi | 8d3a9d3 | 2016-07-12 11:41:44 +0900 | [diff] [blame] | 106 | String[] ethId = null; |
| 107 | |
| 108 | if (!mastershipService.isLocalMaster(ncDeviceId)) { |
| 109 | log.warn("Not master for {} Use {} to execute command", |
| 110 | ncDeviceId, |
| 111 | mastershipService.getMasterFor(ncDeviceId)); |
xueliang | ffe73e4 | 2016-09-27 10:38:56 +0900 | [diff] [blame] | 112 | return null; |
Akihiro Yamanouchi | 8d3a9d3 | 2016-07-12 11:41:44 +0900 | [diff] [blame] | 113 | } |
| 114 | |
xueliang | ffe73e4 | 2016-09-27 10:38:56 +0900 | [diff] [blame] | 115 | data = target.split(COLON); |
| 116 | if (data.length > TWO) { |
| 117 | log.error("Invalid number of parameters {}", target); |
| 118 | return null; |
Akihiro Yamanouchi | 8d3a9d3 | 2016-07-12 11:41:44 +0900 | [diff] [blame] | 119 | } |
xueliang | ffe73e4 | 2016-09-27 10:38:56 +0900 | [diff] [blame] | 120 | |
| 121 | ethId = checkIdString(data[FIRST_PART], THREE); |
| 122 | if (ethId == null) { |
| 123 | log.error("Invalid ETH port identifier {}", data[FIRST_PART]); |
| 124 | return null; |
| 125 | } |
| 126 | |
| 127 | if (data.length > ONE) { |
| 128 | if (!LOOPBACKMODES.contains(data[SECOND_PART])) { |
| 129 | log.error("Unsupported parameter: {}", data[SECOND_PART]); |
| 130 | return null; |
| 131 | } |
Akihiro Yamanouchi | 8d3a9d3 | 2016-07-12 11:41:44 +0900 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | try { |
| 135 | StringBuilder request = new StringBuilder(); |
xueliang | ffe73e4 | 2016-09-27 10:38:56 +0900 | [diff] [blame] | 136 | request.append(ANGLE_LEFT + ONU_ETHPORT_LOOPBACK + SPACE); |
| 137 | request.append(VOLT_NE_NAMESPACE + ANGLE_RIGHT + NEW_LINE); |
| 138 | |
| 139 | request.append(buildStartTag(PONLINK_ID, false)) |
| 140 | .append(ethId[FIRST_PART]) |
| 141 | .append(buildEndTag(PONLINK_ID)) |
| 142 | .append(buildStartTag(ONU_ID, false)) |
| 143 | .append(ethId[SECOND_PART]) |
| 144 | .append(buildEndTag(ONU_ID)) |
| 145 | .append(buildStartTag(ETHPORT_ID, false)) |
| 146 | .append(ethId[THIRD_PART]) |
| 147 | .append(buildEndTag(ETHPORT_ID)); |
| 148 | if (data.length > ONE) { |
| 149 | request.append(buildStartTag(LOOPBACK_MODE, false)) |
| 150 | .append(data[SECOND_PART]) |
| 151 | .append(buildEndTag(LOOPBACK_MODE)); |
| 152 | } |
Akihiro Yamanouchi | 8d3a9d3 | 2016-07-12 11:41:44 +0900 | [diff] [blame] | 153 | request.append(buildEndTag(ONU_ETHPORT_LOOPBACK)); |
| 154 | |
xueliang | ffe73e4 | 2016-09-27 10:38:56 +0900 | [diff] [blame] | 155 | reply = controller |
| 156 | .getDevicesMap() |
| 157 | .get(ncDeviceId) |
| 158 | .getSession() |
| 159 | .doWrappedRpc(request.toString()); |
Akihiro Yamanouchi | 8d3a9d3 | 2016-07-12 11:41:44 +0900 | [diff] [blame] | 160 | } catch (IOException e) { |
xueliang | ffe73e4 | 2016-09-27 10:38:56 +0900 | [diff] [blame] | 161 | log.error("Cannot communicate to device {} exception {}", ncDeviceId, e); |
Akihiro Yamanouchi | 8d3a9d3 | 2016-07-12 11:41:44 +0900 | [diff] [blame] | 162 | } |
| 163 | return reply; |
| 164 | } |
| 165 | |
xueliang | ffe73e4 | 2016-09-27 10:38:56 +0900 | [diff] [blame] | 166 | /** |
| 167 | * Verifies input string for ponlink-id{-onu-id}{-ethport-id}. |
| 168 | * |
| 169 | * @param target input data in string |
| 170 | * @param expected number of IDs expected |
| 171 | * @return String array containing IDs; may be null if an error is detected |
| 172 | */ |
| 173 | private String[] checkIdString(String target, int expected) { |
| 174 | String[] id = target.split(HYPHEN); |
| 175 | int pon; |
| 176 | int onu; |
| 177 | |
| 178 | if (id.length < TWO) { |
| 179 | log.error("Invalid number of arguments for id: {}", id.length); |
| 180 | return null; |
| 181 | } |
| 182 | if (id.length != expected) { |
| 183 | log.error("Invalid number of arguments for id: {}", id.length); |
| 184 | return null; |
| 185 | } |
| 186 | try { |
| 187 | pon = Integer.parseInt(id[FIRST_PART]); |
| 188 | if (pon <= ZERO) { |
| 189 | log.error("Invalid integer for ponlink-id: {}", id[FIRST_PART]); |
| 190 | return null; |
| 191 | } |
| 192 | onu = Integer.parseInt(id[SECOND_PART]); |
| 193 | if (onu <= ZERO) { |
| 194 | log.error("Invalid integer for onu-id: {}", id[SECOND_PART]); |
| 195 | return null; |
| 196 | } |
| 197 | if (expected > TWO) { |
| 198 | int port = Integer.parseInt(id[THIRD_PART]); |
| 199 | if (port <= ZERO) { |
| 200 | log.error("Invalid integer for port-id: {}", id[THIRD_PART]); |
| 201 | return null; |
| 202 | } |
| 203 | } |
| 204 | } catch (NumberFormatException e) { |
| 205 | log.error("Non-number input for id: {}", target); |
| 206 | return null; |
| 207 | } |
| 208 | return id; |
| 209 | } |
| 210 | |
Akihiro Yamanouchi | 8d3a9d3 | 2016-07-12 11:41:44 +0900 | [diff] [blame] | 211 | } |