blob: e6eb7ee06798e2449b184c871ee3f74a2b365a6f [file] [log] [blame]
oleksandr.yashchuk@plvision.eu3dbcaaf2019-03-13 14:44:46 +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.Completion;
21import org.apache.karaf.shell.api.action.lifecycle.Service;
22import org.onosproject.cli.AbstractShellCommand;
23import org.onosproject.net.Device;
24import org.onosproject.net.DeviceId;
25import org.onosproject.net.behaviour.BasicSystemOperations;
26import org.onosproject.net.device.DeviceService;
27import java.util.concurrent.ExecutionException;
28import java.util.concurrent.CompletableFuture;
29
30/**
31 * Gets time since epoch.
32 */
33@Service
34@Command(scope = "onos", name = "device-time",
35 description = "Returns the current time on the target device")
36public class DeviceTimeCommand extends AbstractShellCommand {
37 @Argument(index = 0, name = "deviceId", description = "Device ID",
38 required = true, multiValued = false)
39 @Completion(DeviceIdCompleter.class)
40 String deviceId = null;
41
42 @Override
43 protected void doExecute() {
44 Device dev = get(DeviceService.class).getDevice(DeviceId.deviceId(deviceId));
45 if (dev == null) {
46 print(" %s", "Device does not exist");
47 return;
48 }
49
50 if (dev.is(BasicSystemOperations.class)) {
51 try {
52 CompletableFuture<Long> timeFuture = dev.as(BasicSystemOperations.class).time();
53 print("Current time on the device: %s %d", deviceId, timeFuture.get());
54 } catch (InterruptedException | ExecutionException e) {
55 log.error("Exception while getting system time for device" + deviceId, e);
56 }
57 } else {
58 log.error("Device does not support {} behaviour", BasicSystemOperations.class.getName());
59 }
60 }
61}