Akihiro Yamanouchi | 38a3867 | 2016-07-20 20:05:57 +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 | 38a3867 | 2016-07-20 20:05:57 +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 org.onosproject.drivers.fujitsu.behaviour.VoltFwdlConfig; |
| 20 | import org.onosproject.mastership.MastershipService; |
| 21 | import org.onosproject.net.DeviceId; |
| 22 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 23 | import org.onosproject.net.driver.DriverHandler; |
| 24 | import org.onosproject.netconf.NetconfController; |
Yuta HIGUCHI | 234eaf3 | 2017-09-06 13:45:05 -0700 | [diff] [blame^] | 25 | import org.onosproject.netconf.NetconfException; |
Akihiro Yamanouchi | 38a3867 | 2016-07-20 20:05:57 +0900 | [diff] [blame] | 26 | import org.slf4j.Logger; |
| 27 | |
Akihiro Yamanouchi | 38a3867 | 2016-07-20 20:05:57 +0900 | [diff] [blame] | 28 | import static com.google.common.base.Preconditions.checkNotNull; |
| 29 | import static org.onosproject.drivers.fujitsu.FujitsuVoltXmlUtility.*; |
| 30 | import static org.slf4j.LoggerFactory.getLogger; |
| 31 | |
| 32 | /** |
| 33 | * Implementation to upgrade firmware in ONUs manually |
| 34 | * through the Netconf protocol. |
| 35 | */ |
| 36 | public class FujitsuVoltFwdlConfig extends AbstractHandlerBehaviour |
| 37 | implements VoltFwdlConfig { |
| 38 | |
| 39 | private final Logger log = getLogger(FujitsuVoltFwdlConfig.class); |
| 40 | private static final String ONDEMAND_FIRMWARE_UPGRADE = "ondemand-firmware-upgrade"; |
| 41 | private static final String PARTICIPANT_LIST = "participant-list"; |
| 42 | private static final String MEMBER = "member"; |
| 43 | private static final String IMAGE_NAME = "image-name"; |
| 44 | private static final String REBOOT_MODE = "reboot-mode"; |
xueliang | adec4dc | 2016-10-03 16:57:04 +0900 | [diff] [blame] | 45 | private static final String AUTO = "auto"; |
| 46 | private static final String COMMA = ","; |
Akihiro Yamanouchi | 38a3867 | 2016-07-20 20:05:57 +0900 | [diff] [blame] | 47 | |
| 48 | @Override |
| 49 | public String upgradeFirmwareOndemand(String target) { |
| 50 | DriverHandler handler = handler(); |
| 51 | NetconfController controller = handler.get(NetconfController.class); |
| 52 | MastershipService mastershipService = handler.get(MastershipService.class); |
| 53 | DeviceId ncDeviceId = handler.data().deviceId(); |
| 54 | checkNotNull(controller, "Netconf controller is null"); |
| 55 | String reply = null; |
| 56 | int count; |
| 57 | |
| 58 | if (!mastershipService.isLocalMaster(ncDeviceId)) { |
| 59 | log.warn("Not master for {} Use {} to execute command", |
| 60 | ncDeviceId, |
| 61 | mastershipService.getMasterFor(ncDeviceId)); |
xueliang | adec4dc | 2016-10-03 16:57:04 +0900 | [diff] [blame] | 62 | return null; |
Akihiro Yamanouchi | 38a3867 | 2016-07-20 20:05:57 +0900 | [diff] [blame] | 63 | } |
| 64 | |
xueliang | adec4dc | 2016-10-03 16:57:04 +0900 | [diff] [blame] | 65 | String[] data = target.split(COLON); |
| 66 | if ((data.length < TWO) || (data.length > THREE)) { |
Akihiro Yamanouchi | 38a3867 | 2016-07-20 20:05:57 +0900 | [diff] [blame] | 67 | log.error("Invalid number of arguments"); |
xueliang | adec4dc | 2016-10-03 16:57:04 +0900 | [diff] [blame] | 68 | return null; |
Akihiro Yamanouchi | 38a3867 | 2016-07-20 20:05:57 +0900 | [diff] [blame] | 69 | } |
| 70 | |
xueliang | adec4dc | 2016-10-03 16:57:04 +0900 | [diff] [blame] | 71 | String[] onuList = data[SECOND_PART].split(COMMA); |
| 72 | if (onuList.length == ZERO) { |
Akihiro Yamanouchi | 38a3867 | 2016-07-20 20:05:57 +0900 | [diff] [blame] | 73 | log.error("No ONU listed"); |
xueliang | adec4dc | 2016-10-03 16:57:04 +0900 | [diff] [blame] | 74 | return null; |
| 75 | } |
| 76 | |
| 77 | if ((data.length > TWO) && (!AUTO.equals(data[THIRD_PART]))) { |
| 78 | log.error("Invalid reboot-mode {}", data[THIRD_PART]); |
| 79 | return null; |
Akihiro Yamanouchi | 38a3867 | 2016-07-20 20:05:57 +0900 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | try { |
| 83 | StringBuilder request = new StringBuilder(); |
xueliang | adec4dc | 2016-10-03 16:57:04 +0900 | [diff] [blame] | 84 | request.append(ANGLE_LEFT + ONDEMAND_FIRMWARE_UPGRADE + SPACE); |
| 85 | request.append(VOLT_NE_NAMESPACE + ANGLE_RIGHT + NEW_LINE); |
Akihiro Yamanouchi | 38a3867 | 2016-07-20 20:05:57 +0900 | [diff] [blame] | 86 | request.append(buildStartTag(PARTICIPANT_LIST)); |
| 87 | |
xueliang | adec4dc | 2016-10-03 16:57:04 +0900 | [diff] [blame] | 88 | for (count = ZERO; count < onuList.length; count++) { |
| 89 | String[] onuId = onuList[count].split(HYPHEN); |
| 90 | if (onuId.length != TWO) { |
Akihiro Yamanouchi | 38a3867 | 2016-07-20 20:05:57 +0900 | [diff] [blame] | 91 | log.error("Invalid ONU identifier"); |
xueliang | adec4dc | 2016-10-03 16:57:04 +0900 | [diff] [blame] | 92 | return null; |
Akihiro Yamanouchi | 38a3867 | 2016-07-20 20:05:57 +0900 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | try { |
xueliang | adec4dc | 2016-10-03 16:57:04 +0900 | [diff] [blame] | 96 | int pon; |
| 97 | pon = Integer.parseInt(onuId[FIRST_PART]); |
| 98 | if (pon <= ZERO) { |
| 99 | log.error("Invalid integer for ponlink-id:{}", onuId[FIRST_PART]); |
| 100 | return null; |
| 101 | } |
| 102 | int onu; |
| 103 | onu = Integer.parseInt(onuId[SECOND_PART]); |
| 104 | if (onu <= ZERO) { |
| 105 | log.error("Invalid integer for onu-id:{}", onuId[SECOND_PART]); |
| 106 | return null; |
| 107 | } |
Akihiro Yamanouchi | 38a3867 | 2016-07-20 20:05:57 +0900 | [diff] [blame] | 108 | } catch (NumberFormatException e) { |
| 109 | log.error("Non-number input"); |
xueliang | adec4dc | 2016-10-03 16:57:04 +0900 | [diff] [blame] | 110 | return null; |
Akihiro Yamanouchi | 38a3867 | 2016-07-20 20:05:57 +0900 | [diff] [blame] | 111 | } |
| 112 | |
xueliang | adec4dc | 2016-10-03 16:57:04 +0900 | [diff] [blame] | 113 | request.append(buildStartTag(MEMBER)) |
| 114 | .append(buildStartTag(PONLINK_ID)) |
| 115 | .append(onuId[FIRST_PART]) |
| 116 | .append(buildEndTag(PONLINK_ID)) |
| 117 | .append(buildStartTag(ONU_ID)) |
| 118 | .append(onuId[SECOND_PART]) |
| 119 | .append(buildEndTag(ONU_ID)) |
| 120 | .append(buildEndTag(MEMBER)); |
Akihiro Yamanouchi | 38a3867 | 2016-07-20 20:05:57 +0900 | [diff] [blame] | 121 | } |
xueliang | adec4dc | 2016-10-03 16:57:04 +0900 | [diff] [blame] | 122 | request.append(buildEndTag(PARTICIPANT_LIST)) |
| 123 | .append(buildStartTag(IMAGE_NAME)) |
| 124 | .append(data[FIRST_PART]) |
| 125 | .append(buildEndTag(IMAGE_NAME)); |
| 126 | if (data.length == THREE) { |
| 127 | request.append(buildStartTag(REBOOT_MODE)) |
| 128 | .append(data[THIRD_PART]) |
| 129 | .append(buildEndTag(REBOOT_MODE)); |
Akihiro Yamanouchi | 38a3867 | 2016-07-20 20:05:57 +0900 | [diff] [blame] | 130 | } |
| 131 | request.append(buildEndTag(ONDEMAND_FIRMWARE_UPGRADE)); |
| 132 | |
xueliang | adec4dc | 2016-10-03 16:57:04 +0900 | [diff] [blame] | 133 | reply = controller |
| 134 | .getDevicesMap() |
| 135 | .get(ncDeviceId) |
| 136 | .getSession() |
| 137 | .doWrappedRpc(request.toString()); |
Yuta HIGUCHI | 234eaf3 | 2017-09-06 13:45:05 -0700 | [diff] [blame^] | 138 | } catch (NetconfException e) { |
xueliang | adec4dc | 2016-10-03 16:57:04 +0900 | [diff] [blame] | 139 | log.error("Cannot communicate to device {} exception {}", ncDeviceId, e); |
Akihiro Yamanouchi | 38a3867 | 2016-07-20 20:05:57 +0900 | [diff] [blame] | 140 | } |
| 141 | return reply; |
| 142 | } |
| 143 | |
| 144 | } |