oleksandr.yashchuk@plvision.eu | 3dbcaaf | 2019-03-13 14:44:46 +0200 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.cli.net; |
| 17 | |
| 18 | import org.apache.karaf.shell.api.action.Argument; |
| 19 | import org.apache.karaf.shell.api.action.Command; |
| 20 | import org.apache.karaf.shell.api.action.Completion; |
| 21 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
| 22 | import org.onosproject.cli.AbstractShellCommand; |
| 23 | import org.onosproject.net.Device; |
| 24 | import org.onosproject.net.DeviceId; |
| 25 | import org.onosproject.net.behaviour.BasicSystemOperations; |
| 26 | import org.onosproject.net.device.DeviceService; |
| 27 | import java.util.concurrent.ExecutionException; |
| 28 | import 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") |
| 36 | public 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 | } |