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