blob: 43465126dfe2d9d0db46ed57edb368f81b9a14d1 [file] [log] [blame]
Akihiro Yamanouchi38a38672016-07-20 20:05:57 +09001/*
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
17package org.onosproject.drivers.fujitsu;
18
19import org.onosproject.drivers.fujitsu.behaviour.VoltFwdlConfig;
20import org.onosproject.mastership.MastershipService;
21import org.onosproject.net.DeviceId;
22import org.onosproject.net.driver.AbstractHandlerBehaviour;
23import org.onosproject.net.driver.DriverHandler;
24import org.onosproject.netconf.NetconfController;
25import org.slf4j.Logger;
26
27import java.io.IOException;
28
29import static com.google.common.base.Preconditions.checkNotNull;
30import static org.onosproject.drivers.fujitsu.FujitsuVoltXmlUtility.*;
31import static org.slf4j.LoggerFactory.getLogger;
32
33/**
34 * Implementation to upgrade firmware in ONUs manually
35 * through the Netconf protocol.
36 */
37public 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";
xueliangadec4dc2016-10-03 16:57:04 +090046 private static final String AUTO = "auto";
47 private static final String COMMA = ",";
Akihiro Yamanouchi38a38672016-07-20 20:05:57 +090048
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));
xueliangadec4dc2016-10-03 16:57:04 +090063 return null;
Akihiro Yamanouchi38a38672016-07-20 20:05:57 +090064 }
65
xueliangadec4dc2016-10-03 16:57:04 +090066 String[] data = target.split(COLON);
67 if ((data.length < TWO) || (data.length > THREE)) {
Akihiro Yamanouchi38a38672016-07-20 20:05:57 +090068 log.error("Invalid number of arguments");
xueliangadec4dc2016-10-03 16:57:04 +090069 return null;
Akihiro Yamanouchi38a38672016-07-20 20:05:57 +090070 }
71
xueliangadec4dc2016-10-03 16:57:04 +090072 String[] onuList = data[SECOND_PART].split(COMMA);
73 if (onuList.length == ZERO) {
Akihiro Yamanouchi38a38672016-07-20 20:05:57 +090074 log.error("No ONU listed");
xueliangadec4dc2016-10-03 16:57:04 +090075 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 Yamanouchi38a38672016-07-20 20:05:57 +090081 }
82
83 try {
84 StringBuilder request = new StringBuilder();
xueliangadec4dc2016-10-03 16:57:04 +090085 request.append(ANGLE_LEFT + ONDEMAND_FIRMWARE_UPGRADE + SPACE);
86 request.append(VOLT_NE_NAMESPACE + ANGLE_RIGHT + NEW_LINE);
Akihiro Yamanouchi38a38672016-07-20 20:05:57 +090087 request.append(buildStartTag(PARTICIPANT_LIST));
88
xueliangadec4dc2016-10-03 16:57:04 +090089 for (count = ZERO; count < onuList.length; count++) {
90 String[] onuId = onuList[count].split(HYPHEN);
91 if (onuId.length != TWO) {
Akihiro Yamanouchi38a38672016-07-20 20:05:57 +090092 log.error("Invalid ONU identifier");
xueliangadec4dc2016-10-03 16:57:04 +090093 return null;
Akihiro Yamanouchi38a38672016-07-20 20:05:57 +090094 }
95
96 try {
xueliangadec4dc2016-10-03 16:57:04 +090097 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 Yamanouchi38a38672016-07-20 20:05:57 +0900109 } catch (NumberFormatException e) {
110 log.error("Non-number input");
xueliangadec4dc2016-10-03 16:57:04 +0900111 return null;
Akihiro Yamanouchi38a38672016-07-20 20:05:57 +0900112 }
113
xueliangadec4dc2016-10-03 16:57:04 +0900114 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 Yamanouchi38a38672016-07-20 20:05:57 +0900122 }
xueliangadec4dc2016-10-03 16:57:04 +0900123 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 Yamanouchi38a38672016-07-20 20:05:57 +0900131 }
132 request.append(buildEndTag(ONDEMAND_FIRMWARE_UPGRADE));
133
xueliangadec4dc2016-10-03 16:57:04 +0900134 reply = controller
135 .getDevicesMap()
136 .get(ncDeviceId)
137 .getSession()
138 .doWrappedRpc(request.toString());
Akihiro Yamanouchi38a38672016-07-20 20:05:57 +0900139 } catch (IOException e) {
xueliangadec4dc2016-10-03 16:57:04 +0900140 log.error("Cannot communicate to device {} exception {}", ncDeviceId, e);
Akihiro Yamanouchi38a38672016-07-20 20:05:57 +0900141 }
142 return reply;
143 }
144
145}