blob: 7031a78fc81ee5d6158a3d91f75d6d93f7c10665 [file] [log] [blame]
Serhii Boiko992bf522019-03-28 18:38:04 +02001/*
2 * Copyright 2019-present Open Networking Foundation
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 */
16package org.onosproject.cli.net;
17
18import org.apache.karaf.shell.api.action.Argument;
19import org.apache.karaf.shell.api.action.Command;
20import org.apache.karaf.shell.api.action.Option;
21import org.apache.karaf.shell.api.action.Completion;
22import org.apache.karaf.shell.api.action.lifecycle.Service;
23import org.onosproject.cli.AbstractShellCommand;
24import org.onosproject.net.Device;
25import org.onosproject.net.DeviceId;
26import org.onosproject.net.device.DeviceService;
27import org.onosproject.net.behaviour.SoftwareUpgrade;
28import org.onosproject.net.behaviour.SoftwareUpgrade.Response;
29import java.util.concurrent.CompletableFuture;
30
31/**
32 * CLI command to Administratively upgrade device.
33 */
34@Service
35@Command(scope = "onos", name = "device-upgrade",
36 description = "Administratively upgrades a device")
37public class SoftwareUpgradeCommand extends AbstractShellCommand {
38 @Argument(index = 0, name = "deviceId", description = "Device ID",
39 required = true, multiValued = false)
40 @Completion(DeviceIdCompleter.class)
41 String deviceId = null;
42
43 @Option(name = "-p", aliases = "--package-to-upload",
44 description = "Path to the package to be installed",
45 required = false, multiValued = false)
46 String packageToUpload = null;
47
48 @Option(name = "-d", aliases = "--device-local-path",
49 description = "Path on target device where (if specified) the package will be uploaded "
50 + "and/or the package to be installed",
51 required = false, multiValued = false)
52 String deviceLocalPath = null;
53
54 @Override
55 protected void doExecute() {
56 if (packageToUpload == null && deviceLocalPath == null) {
57 log.warn("Please specify the path to the file you want to install");
58 return;
59 }
60 Device device = get(DeviceService.class).getDevice(DeviceId.deviceId(deviceId));
61
62 if (device == null) {
63 log.warn("Device {} does not exist", deviceId);
64 return;
65 }
66
67 if (!device.is(SoftwareUpgrade.class)) {
68 log.warn("Device {} does not support {} behaviour", deviceId, SoftwareUpgrade.class.getName());
69 return;
70 }
71
72 log.info("Starting upgrade for {} (check log for errors)...", deviceId);
73 CompletableFuture.supplyAsync(() -> {
74 if (packageToUpload != null) {
75 return device.as(SoftwareUpgrade.class)
76 .uploadPackage(packageToUpload, deviceLocalPath)
77 .join();
78 } else {
79 return deviceLocalPath;
80 }
81 })
82 .thenCompose((String pathOnDevice) -> {
83 if (pathOnDevice == null) {
84 log.warn("Package Upload for {} on {} failed", packageToUpload, deviceId);
85 return CompletableFuture.completedFuture(new Response());
86 }
87 return device.as(SoftwareUpgrade.class)
88 .swapAgent(pathOnDevice);
89 })
90 .whenComplete((Response result, Throwable exception) -> {
91 if (exception != null) {
92 log.error("Error while upgrading device {}", deviceId, exception);
93 } else if (result == null || !result.isSuccess()) {
94 log.warn("Upgrade on {} failed", deviceId);
95 } else {
96 log.info("Upgrade on {} succeeded! \n" +
97 "New SW version: {} \n" +
98 "Uptime: {} ns",
99 deviceId, result.getVersion(), result.getUptime());
100 }
101 });
102 }
103}