blob: 05c44661777263c089fec42f44b10bc63d394596 [file] [log] [blame]
Akihiro Yamanouchi38a38672016-07-20 20:05:57 +09001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Akihiro Yamanouchi38a38672016-07-20 20:05:57 +09003 *
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;
Yuta HIGUCHI234eaf32017-09-06 13:45:05 -070025import org.onosproject.netconf.NetconfException;
Akihiro Yamanouchi38a38672016-07-20 20:05:57 +090026import org.slf4j.Logger;
27
Akihiro Yamanouchi38a38672016-07-20 20:05:57 +090028import static com.google.common.base.Preconditions.checkNotNull;
29import static org.onosproject.drivers.fujitsu.FujitsuVoltXmlUtility.*;
30import static org.slf4j.LoggerFactory.getLogger;
31
32/**
33 * Implementation to upgrade firmware in ONUs manually
34 * through the Netconf protocol.
35 */
36public 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";
xueliangadec4dc2016-10-03 16:57:04 +090045 private static final String AUTO = "auto";
46 private static final String COMMA = ",";
Akihiro Yamanouchi38a38672016-07-20 20:05:57 +090047
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));
xueliangadec4dc2016-10-03 16:57:04 +090062 return null;
Akihiro Yamanouchi38a38672016-07-20 20:05:57 +090063 }
64
xueliangadec4dc2016-10-03 16:57:04 +090065 String[] data = target.split(COLON);
66 if ((data.length < TWO) || (data.length > THREE)) {
Akihiro Yamanouchi38a38672016-07-20 20:05:57 +090067 log.error("Invalid number of arguments");
xueliangadec4dc2016-10-03 16:57:04 +090068 return null;
Akihiro Yamanouchi38a38672016-07-20 20:05:57 +090069 }
70
xueliangadec4dc2016-10-03 16:57:04 +090071 String[] onuList = data[SECOND_PART].split(COMMA);
72 if (onuList.length == ZERO) {
Akihiro Yamanouchi38a38672016-07-20 20:05:57 +090073 log.error("No ONU listed");
xueliangadec4dc2016-10-03 16:57:04 +090074 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 Yamanouchi38a38672016-07-20 20:05:57 +090080 }
81
82 try {
83 StringBuilder request = new StringBuilder();
xueliangadec4dc2016-10-03 16:57:04 +090084 request.append(ANGLE_LEFT + ONDEMAND_FIRMWARE_UPGRADE + SPACE);
85 request.append(VOLT_NE_NAMESPACE + ANGLE_RIGHT + NEW_LINE);
Akihiro Yamanouchi38a38672016-07-20 20:05:57 +090086 request.append(buildStartTag(PARTICIPANT_LIST));
87
xueliangadec4dc2016-10-03 16:57:04 +090088 for (count = ZERO; count < onuList.length; count++) {
89 String[] onuId = onuList[count].split(HYPHEN);
90 if (onuId.length != TWO) {
Akihiro Yamanouchi38a38672016-07-20 20:05:57 +090091 log.error("Invalid ONU identifier");
xueliangadec4dc2016-10-03 16:57:04 +090092 return null;
Akihiro Yamanouchi38a38672016-07-20 20:05:57 +090093 }
94
95 try {
xueliangadec4dc2016-10-03 16:57:04 +090096 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 Yamanouchi38a38672016-07-20 20:05:57 +0900108 } catch (NumberFormatException e) {
109 log.error("Non-number input");
xueliangadec4dc2016-10-03 16:57:04 +0900110 return null;
Akihiro Yamanouchi38a38672016-07-20 20:05:57 +0900111 }
112
xueliangadec4dc2016-10-03 16:57:04 +0900113 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 Yamanouchi38a38672016-07-20 20:05:57 +0900121 }
xueliangadec4dc2016-10-03 16:57:04 +0900122 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 Yamanouchi38a38672016-07-20 20:05:57 +0900130 }
131 request.append(buildEndTag(ONDEMAND_FIRMWARE_UPGRADE));
132
xueliangadec4dc2016-10-03 16:57:04 +0900133 reply = controller
134 .getDevicesMap()
135 .get(ncDeviceId)
136 .getSession()
137 .doWrappedRpc(request.toString());
Yuta HIGUCHI234eaf32017-09-06 13:45:05 -0700138 } catch (NetconfException e) {
xueliangadec4dc2016-10-03 16:57:04 +0900139 log.error("Cannot communicate to device {} exception {}", ncDeviceId, e);
Akihiro Yamanouchi38a38672016-07-20 20:05:57 +0900140 }
141 return reply;
142 }
143
144}