blob: 51cc1ee3ae7851db143f1902b75b1c3519c8334e [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";
46 private int pon;
47 private int onu;
48
49
50 @Override
51 public String upgradeFirmwareOndemand(String target) {
52 DriverHandler handler = handler();
53 NetconfController controller = handler.get(NetconfController.class);
54 MastershipService mastershipService = handler.get(MastershipService.class);
55 DeviceId ncDeviceId = handler.data().deviceId();
56 checkNotNull(controller, "Netconf controller is null");
57 String reply = null;
58 int count;
59
60 if (!mastershipService.isLocalMaster(ncDeviceId)) {
61 log.warn("Not master for {} Use {} to execute command",
62 ncDeviceId,
63 mastershipService.getMasterFor(ncDeviceId));
64 return reply;
65 }
66
67 String[] data = target.split(":");
68 if ((data.length < 2) || (data.length > 3)) {
69 log.error("Invalid number of arguments");
70 return reply;
71 }
72
73 String[] onuList = data[1].split(",");
74 if (onuList.length == 0) {
75 log.error("No ONU listed");
76 return reply;
77 }
78
79 try {
80 StringBuilder request = new StringBuilder();
81 request.append(ANGLE_LEFT).append(ONDEMAND_FIRMWARE_UPGRADE).append(SPACE);
82 request.append(VOLT_NE_NAMESPACE).append(ANGLE_RIGHT).append(NEW_LINE);
83 request.append(buildStartTag(PARTICIPANT_LIST));
84
85 for (count = 0; count < onuList.length; count++) {
86 String[] onuId = onuList[count].split("-");
87 if (onuId.length != 2) {
88 log.error("Invalid ONU identifier");
89 return reply;
90 }
91
92 try {
93 pon = Integer.parseInt(onuId[0]);
94 onu = Integer.parseInt(onuId[1]);
95 } catch (NumberFormatException e) {
96 log.error("Non-number input");
97 return reply;
98 }
99
100 request.append(buildStartTag(MEMBER));
101 request.append(buildStartTag(PONLINK_ID));
102 request.append(onuId[0]);
103 request.append(buildEndTag(PONLINK_ID));
104 request.append(buildStartTag(ONU_ID));
105 request.append(onuId[1]);
106 request.append(buildEndTag(ONU_ID));
107 request.append(buildEndTag(MEMBER));
108 }
109 request.append(buildEndTag(PARTICIPANT_LIST));
110 request.append(buildStartTag(IMAGE_NAME));
111 request.append(data[0]);
112 request.append(buildEndTag(IMAGE_NAME));
113 if (data.length == 3) {
114 request.append(buildStartTag(REBOOT_MODE));
115 request.append(data[2]);
116 request.append(buildEndTag(REBOOT_MODE));
117 }
118 request.append(buildEndTag(ONDEMAND_FIRMWARE_UPGRADE));
119
120 reply = controller.
121 getDevicesMap().get(ncDeviceId).getSession().
122 doWrappedRpc(request.toString());
123 } catch (IOException e) {
124 log.error("Cannot communicate to device {} exception ", ncDeviceId, e);
125 }
126 return reply;
127 }
128
129}