Akihiro Yamanouchi | d491284 | 2016-07-01 10:38:46 +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 org.onosproject.mastership.MastershipService; |
| 20 | import org.onosproject.net.DeviceId; |
Akihiro Yamanouchi | 8d3a9d3 | 2016-07-12 11:41:44 +0900 | [diff] [blame] | 21 | import org.onosproject.drivers.fujitsu.behaviour.VoltPonLinkConfig; |
Akihiro Yamanouchi | d491284 | 2016-07-01 10:38:46 +0900 | [diff] [blame] | 22 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 23 | import org.onosproject.net.driver.DriverHandler; |
| 24 | import org.onosproject.netconf.NetconfController; |
| 25 | import org.slf4j.Logger; |
| 26 | |
| 27 | import java.io.IOException; |
xueliang | 714dd2b | 2016-09-13 16:43:32 +0900 | [diff] [blame] | 28 | import java.util.Arrays; |
| 29 | import java.util.List; |
| 30 | import java.util.HashMap; |
| 31 | import java.util.Map; |
Akihiro Yamanouchi | d491284 | 2016-07-01 10:38:46 +0900 | [diff] [blame] | 32 | import java.util.Set; |
| 33 | |
| 34 | import com.google.common.collect.ImmutableSet; |
| 35 | import static com.google.common.base.Preconditions.checkNotNull; |
| 36 | import static org.onosproject.drivers.fujitsu.FujitsuVoltXmlUtility.*; |
| 37 | import static org.slf4j.LoggerFactory.getLogger; |
| 38 | |
| 39 | /** |
| 40 | * Implementation to get and set parameters available in vOLT |
| 41 | * through the Netconf protocol. |
| 42 | */ |
| 43 | public class FujitsuVoltPonLinkConfig extends AbstractHandlerBehaviour |
| 44 | implements VoltPonLinkConfig { |
| 45 | |
| 46 | private final Logger log = getLogger(FujitsuVoltPonLinkConfig.class); |
xueliang | 714dd2b | 2016-09-13 16:43:32 +0900 | [diff] [blame] | 47 | private static final Map<String, List<Integer>> PON_LINK_PARAMS = new HashMap<String, List<Integer>>() { |
| 48 | { |
| 49 | put("onu-discovery-interval", Arrays.asList(ONU_DISCOVERY_INTERVAL_MIN, ONU_DISCOVERY_INTERVAL_MAX)); |
| 50 | put("dba-cycle-time", Arrays.asList(DBA_CYCLE_TIME_MIN, DBA_CYCLE_TIME_MAX)); |
| 51 | put("mac-age-time", Arrays.asList(MAC_AGE_TIME_MIN, MAC_AGE_TIME_MAX)); |
| 52 | put("lof-threshold", Arrays.asList(LOF_THRESHOLD_MIN, LOF_THRESHOLD_MAX)); |
| 53 | put("los-threshold", Arrays.asList(LOS_THRESHOLD_MIN, LOS_THRESHOLD_MAX)); |
| 54 | put(ONU_DISCOVERY_MODE, null); |
| 55 | put(PM_ENABLE, null); |
| 56 | put(ADMIN_STATE, null); |
| 57 | } |
| 58 | }; |
Akihiro Yamanouchi | d491284 | 2016-07-01 10:38:46 +0900 | [diff] [blame] | 59 | private static final String VOLT_PORTS = "volt-ports"; |
| 60 | private static final String GPON_PONLINK_PORTS = "gpon-ponlink-ports"; |
| 61 | private static final String GPON_PONLINK_PORT = "gpon-ponlink-port"; |
xueliang | 714dd2b | 2016-09-13 16:43:32 +0900 | [diff] [blame] | 62 | private static final String ADMIN_STATE = "admin-state"; |
| 63 | private static final String ONU_DISCOVERY_MODE = "onu-discovery-mode"; |
| 64 | private static final String PM_ENABLE = "pm-enable"; |
| 65 | private static final Set<String> ADMINSTATES = |
| 66 | ImmutableSet.of("enable", "disable"); |
| 67 | private static final Set<String> ONUDISCOVERYMODES = |
| 68 | ImmutableSet.of("auto", "manual", "disabled"); |
| 69 | private static final Set<String> PMENABLES = |
| 70 | ImmutableSet.of("true", "false"); |
| 71 | |
| 72 | private static final int ONU_DISCOVERY_INTERVAL_MIN = 1; |
| 73 | private static final int ONU_DISCOVERY_INTERVAL_MAX = 3600; |
| 74 | private static final int DBA_CYCLE_TIME_MIN = 2; |
| 75 | private static final int DBA_CYCLE_TIME_MAX = 40; |
| 76 | private static final int MAC_AGE_TIME_MIN = 1000; |
| 77 | private static final int MAC_AGE_TIME_MAX = 3600000; |
| 78 | private static final int LOF_THRESHOLD_MIN = 1; |
| 79 | private static final int LOF_THRESHOLD_MAX = 10; |
| 80 | private static final int LOS_THRESHOLD_MIN = 1; |
| 81 | private static final int LOS_THRESHOLD_MAX = 10; |
xueliang | 714dd2b | 2016-09-13 16:43:32 +0900 | [diff] [blame] | 82 | private static final int RANGE_MIN = 0; |
| 83 | private static final int RANGE_MAX = 1; |
Akihiro Yamanouchi | d491284 | 2016-07-01 10:38:46 +0900 | [diff] [blame] | 84 | |
| 85 | @Override |
| 86 | public String getPonLinks(String target) { |
| 87 | DriverHandler handler = handler(); |
| 88 | NetconfController controller = handler.get(NetconfController.class); |
| 89 | MastershipService mastershipService = handler.get(MastershipService.class); |
| 90 | DeviceId ncDeviceId = handler.data().deviceId(); |
| 91 | checkNotNull(controller, "Netconf controller is null"); |
| 92 | String reply = null; |
| 93 | |
| 94 | if (!mastershipService.isLocalMaster(ncDeviceId)) { |
| 95 | log.warn("Not master for {} Use {} to execute command", |
| 96 | ncDeviceId, |
| 97 | mastershipService.getMasterFor(ncDeviceId)); |
xueliang | 5b3d3444 | 2016-09-23 10:37:33 +0900 | [diff] [blame] | 98 | return null; |
Akihiro Yamanouchi | d491284 | 2016-07-01 10:38:46 +0900 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | try { |
| 102 | StringBuilder request = new StringBuilder(); |
xueliang | c6e47e2 | 2016-10-20 16:37:24 +0900 | [diff] [blame] | 103 | request.append(VOLT_NE_OPEN + VOLT_NE_NAMESPACE); |
| 104 | request.append(ANGLE_RIGHT + NEW_LINE); |
Akihiro Yamanouchi | d491284 | 2016-07-01 10:38:46 +0900 | [diff] [blame] | 105 | request.append(buildStartTag(VOLT_PORTS)); |
| 106 | if (target != null) { |
xueliang | 714dd2b | 2016-09-13 16:43:32 +0900 | [diff] [blame] | 107 | int pon; |
Akihiro Yamanouchi | d491284 | 2016-07-01 10:38:46 +0900 | [diff] [blame] | 108 | try { |
| 109 | pon = Integer.parseInt(target); |
xueliang | 714dd2b | 2016-09-13 16:43:32 +0900 | [diff] [blame] | 110 | if (pon <= ZERO) { |
| 111 | log.error("Invalid integer for ponlink-id:{}", target); |
xueliang | 5b3d3444 | 2016-09-23 10:37:33 +0900 | [diff] [blame] | 112 | return null; |
xueliang | 714dd2b | 2016-09-13 16:43:32 +0900 | [diff] [blame] | 113 | } |
Akihiro Yamanouchi | d491284 | 2016-07-01 10:38:46 +0900 | [diff] [blame] | 114 | } catch (NumberFormatException e) { |
xueliang | 714dd2b | 2016-09-13 16:43:32 +0900 | [diff] [blame] | 115 | log.error("Non-number input for ponlink-id:{}", target); |
xueliang | 5b3d3444 | 2016-09-23 10:37:33 +0900 | [diff] [blame] | 116 | return null; |
Akihiro Yamanouchi | d491284 | 2016-07-01 10:38:46 +0900 | [diff] [blame] | 117 | } |
xueliang | c6e47e2 | 2016-10-20 16:37:24 +0900 | [diff] [blame] | 118 | request.append(buildStartTag(GPON_PONLINK_PORTS)) |
| 119 | .append(buildStartTag(GPON_PONLINK_PORT)) |
| 120 | .append(buildStartTag(PONLINK_ID, false)) |
| 121 | .append(target) |
| 122 | .append(buildEndTag(PONLINK_ID)) |
| 123 | .append(buildEndTag(GPON_PONLINK_PORT)) |
| 124 | .append(buildEndTag(GPON_PONLINK_PORTS)); |
Akihiro Yamanouchi | d491284 | 2016-07-01 10:38:46 +0900 | [diff] [blame] | 125 | } else { |
| 126 | request.append(buildEmptyTag(GPON_PONLINK_PORTS)); |
| 127 | } |
| 128 | request.append(buildEndTag(VOLT_PORTS)); |
| 129 | request.append(VOLT_NE_CLOSE); |
| 130 | |
xueliang | 714dd2b | 2016-09-13 16:43:32 +0900 | [diff] [blame] | 131 | reply = controller |
| 132 | .getDevicesMap() |
| 133 | .get(ncDeviceId) |
| 134 | .getSession() |
| 135 | .get(request.toString(), REPORT_ALL); |
Akihiro Yamanouchi | d491284 | 2016-07-01 10:38:46 +0900 | [diff] [blame] | 136 | } catch (IOException e) { |
xueliang | 714dd2b | 2016-09-13 16:43:32 +0900 | [diff] [blame] | 137 | log.error("Cannot communicate to device {} exception {}", ncDeviceId, e); |
Akihiro Yamanouchi | d491284 | 2016-07-01 10:38:46 +0900 | [diff] [blame] | 138 | } |
| 139 | return reply; |
| 140 | } |
| 141 | |
| 142 | @Override |
xueliang | 714dd2b | 2016-09-13 16:43:32 +0900 | [diff] [blame] | 143 | public boolean setPonLink(String target) { |
Akihiro Yamanouchi | d491284 | 2016-07-01 10:38:46 +0900 | [diff] [blame] | 144 | DriverHandler handler = handler(); |
| 145 | NetconfController controller = handler.get(NetconfController.class); |
| 146 | MastershipService mastershipService = handler.get(MastershipService.class); |
| 147 | DeviceId ncDeviceId = handler.data().deviceId(); |
| 148 | checkNotNull(controller, "Netconf controller is null"); |
| 149 | |
| 150 | if (!mastershipService.isLocalMaster(ncDeviceId)) { |
| 151 | log.warn("Not master for {} Use {} to execute command", |
| 152 | ncDeviceId, |
| 153 | mastershipService.getMasterFor(ncDeviceId)); |
xueliang | 714dd2b | 2016-09-13 16:43:32 +0900 | [diff] [blame] | 154 | return false; |
Akihiro Yamanouchi | d491284 | 2016-07-01 10:38:46 +0900 | [diff] [blame] | 155 | } |
| 156 | |
xueliang | 714dd2b | 2016-09-13 16:43:32 +0900 | [diff] [blame] | 157 | String[] data = checkSetInput(target); |
| 158 | if (data == null) { |
| 159 | log.error("Failed to check input: {}", target); |
| 160 | return false; |
Akihiro Yamanouchi | d491284 | 2016-07-01 10:38:46 +0900 | [diff] [blame] | 161 | } |
| 162 | |
xueliang | 714dd2b | 2016-09-13 16:43:32 +0900 | [diff] [blame] | 163 | boolean result = false; |
Akihiro Yamanouchi | d491284 | 2016-07-01 10:38:46 +0900 | [diff] [blame] | 164 | try { |
| 165 | StringBuilder request = new StringBuilder(); |
xueliang | c6e47e2 | 2016-10-20 16:37:24 +0900 | [diff] [blame] | 166 | request.append(VOLT_NE_OPEN + VOLT_NE_NAMESPACE); |
| 167 | request.append(ANGLE_RIGHT + NEW_LINE); |
| 168 | request.append(buildStartTag(VOLT_PORTS)) |
| 169 | .append(buildStartTag(GPON_PONLINK_PORTS)) |
| 170 | .append(buildStartTag(GPON_PONLINK_PORT)) |
| 171 | .append(buildStartTag(PONLINK_ID, false)) |
| 172 | .append(data[FIRST_PART]) |
| 173 | .append(buildEndTag(PONLINK_ID)) |
| 174 | .append(buildStartTag(data[SECOND_PART], false)) |
| 175 | .append(data[THIRD_PART]) |
| 176 | .append(buildEndTag(data[SECOND_PART])) |
| 177 | .append(buildEndTag(GPON_PONLINK_PORT)) |
| 178 | .append(buildEndTag(GPON_PONLINK_PORTS)) |
| 179 | .append(buildEndTag(VOLT_PORTS)) |
| 180 | .append(VOLT_NE_CLOSE); |
Akihiro Yamanouchi | d491284 | 2016-07-01 10:38:46 +0900 | [diff] [blame] | 181 | |
xueliang | 714dd2b | 2016-09-13 16:43:32 +0900 | [diff] [blame] | 182 | result = controller.getDevicesMap().get(ncDeviceId).getSession(). |
Akihiro Yamanouchi | d491284 | 2016-07-01 10:38:46 +0900 | [diff] [blame] | 183 | editConfig(RUNNING, null, request.toString()); |
| 184 | } catch (IOException e) { |
xueliang | 714dd2b | 2016-09-13 16:43:32 +0900 | [diff] [blame] | 185 | log.error("Cannot communicate to device {} exception {}", ncDeviceId, e); |
Akihiro Yamanouchi | d491284 | 2016-07-01 10:38:46 +0900 | [diff] [blame] | 186 | } |
xueliang | 714dd2b | 2016-09-13 16:43:32 +0900 | [diff] [blame] | 187 | return result; |
Akihiro Yamanouchi | d491284 | 2016-07-01 10:38:46 +0900 | [diff] [blame] | 188 | } |
| 189 | |
xueliang | 714dd2b | 2016-09-13 16:43:32 +0900 | [diff] [blame] | 190 | /** |
| 191 | * Verifies input string for valid options. |
| 192 | * |
| 193 | * @param target input data in string |
xueliang | c6e47e2 | 2016-10-20 16:37:24 +0900 | [diff] [blame] | 194 | * @return String array containing IDs; may be null if an error is detected |
xueliang | 714dd2b | 2016-09-13 16:43:32 +0900 | [diff] [blame] | 195 | */ |
| 196 | private String[] checkSetInput(String target) { |
| 197 | String[] data = target.split(COLON); |
| 198 | String paramName = data[SECOND_PART]; |
| 199 | String paramValue = data[THIRD_PART]; |
| 200 | int pon; |
| 201 | |
| 202 | if (data.length != THREE) { |
| 203 | log.error("Invalid number of arguments {}", data.length); |
| 204 | return null; |
| 205 | } |
| 206 | |
| 207 | try { |
| 208 | pon = Integer.parseInt(data[FIRST_PART]); |
| 209 | if (pon <= ZERO) { |
| 210 | log.error("Invalid integer for ponlink-id: {}", |
| 211 | data[FIRST_PART]); |
| 212 | return null; |
| 213 | } |
| 214 | } catch (NumberFormatException e) { |
| 215 | log.error("Non-number input for ponlink-id: {}", |
| 216 | data[FIRST_PART]); |
| 217 | return null; |
| 218 | } |
| 219 | |
| 220 | if (!PON_LINK_PARAMS.containsKey(paramName)) { |
| 221 | log.error("Unsupported parameter: {}", paramName); |
| 222 | return null; |
| 223 | } |
| 224 | |
| 225 | List<Integer> range = PON_LINK_PARAMS.get(paramName); |
| 226 | if (range == null) { |
| 227 | switch (paramName) { |
| 228 | case ADMIN_STATE: |
| 229 | if (!validState(ADMINSTATES, paramName, paramValue)) { |
| 230 | return null; |
| 231 | } |
| 232 | break; |
| 233 | case ONU_DISCOVERY_MODE: |
| 234 | if (!validState(ONUDISCOVERYMODES, paramName, paramValue)) { |
| 235 | return null; |
| 236 | } |
| 237 | break; |
| 238 | default: |
| 239 | if (!validState(PMENABLES, paramName, paramValue)) { |
| 240 | return null; |
| 241 | } |
| 242 | break; |
| 243 | } |
| 244 | } else { |
| 245 | int value; |
| 246 | |
| 247 | try { |
| 248 | value = Integer.parseInt(paramValue); |
| 249 | } catch (NumberFormatException e) { |
| 250 | log.error("Non-number input for Name {} : Value {}.", |
| 251 | paramName, paramValue); |
| 252 | return null; |
| 253 | } |
| 254 | |
| 255 | if ((value < range.get(RANGE_MIN)) || |
| 256 | (value > range.get(RANGE_MAX))) { |
| 257 | log.error("Out of value range for Name {} : Value {}.", |
| 258 | paramName, paramValue); |
| 259 | return null; |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | return data; |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * Verifies input string for valid options. |
| 268 | * |
| 269 | * @param states input data in string for parameter state |
| 270 | * @param name input data in string for parameter name |
| 271 | * @param value input data in string for parameter value |
xueliang | c6e47e2 | 2016-10-20 16:37:24 +0900 | [diff] [blame] | 272 | * @return true/false if the param is valid/invalid |
xueliang | 714dd2b | 2016-09-13 16:43:32 +0900 | [diff] [blame] | 273 | */ |
| 274 | private boolean validState(Set<String> states, String name, String value) { |
| 275 | if (!states.contains(value)) { |
| 276 | log.error("Invalid value for Name {} : Value {}.", name, value); |
| 277 | return false; |
| 278 | } |
| 279 | return true; |
| 280 | } |
Akihiro Yamanouchi | d491284 | 2016-07-01 10:38:46 +0900 | [diff] [blame] | 281 | } |