Yuta HIGUCHI | 57ba1e1 | 2017-04-26 15:51:47 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Yuta HIGUCHI | 57ba1e1 | 2017-04-26 15:51:47 -0700 | [diff] [blame] | 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.netconf.cli.impl; |
| 17 | |
Yuta HIGUCHI | d31bc6e | 2017-05-03 17:23:34 -0700 | [diff] [blame] | 18 | import static com.google.common.base.Preconditions.checkNotNull; |
Yuta HIGUCHI | 5ac3443 | 2018-02-13 16:29:15 -0800 | [diff] [blame] | 19 | import java.util.concurrent.ExecutionException; |
| 20 | import java.util.concurrent.TimeUnit; |
| 21 | import java.util.concurrent.TimeoutException; |
Yuta HIGUCHI | d31bc6e | 2017-05-03 17:23:34 -0700 | [diff] [blame] | 22 | |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame^] | 23 | import org.apache.karaf.shell.api.action.Argument; |
| 24 | import org.apache.karaf.shell.api.action.Command; |
| 25 | import org.apache.karaf.shell.api.action.Option; |
Yuta HIGUCHI | 57ba1e1 | 2017-04-26 15:51:47 -0700 | [diff] [blame] | 26 | import org.onosproject.cli.AbstractShellCommand; |
| 27 | import org.onosproject.net.DeviceId; |
Yuta HIGUCHI | d31bc6e | 2017-05-03 17:23:34 -0700 | [diff] [blame] | 28 | import org.onosproject.netconf.NetconfController; |
| 29 | import org.onosproject.netconf.NetconfDevice; |
Yuta HIGUCHI | 234eaf3 | 2017-09-06 13:45:05 -0700 | [diff] [blame] | 30 | import org.onosproject.netconf.NetconfException; |
Yuta HIGUCHI | d31bc6e | 2017-05-03 17:23:34 -0700 | [diff] [blame] | 31 | import org.onosproject.netconf.NetconfSession; |
Yuta HIGUCHI | 57ba1e1 | 2017-04-26 15:51:47 -0700 | [diff] [blame] | 32 | |
| 33 | /** |
Yuta HIGUCHI | 5ac3443 | 2018-02-13 16:29:15 -0800 | [diff] [blame] | 34 | * Command that retrieves running configuration and device state. |
| 35 | * If configuration cannot be retrieved it prints an error string. |
Yuta HIGUCHI | 57ba1e1 | 2017-04-26 15:51:47 -0700 | [diff] [blame] | 36 | */ |
Yuta HIGUCHI | 5ac3443 | 2018-02-13 16:29:15 -0800 | [diff] [blame] | 37 | @Command(scope = "onos", name = "netconf-get", |
| 38 | description = "Retrieve running configuration and " |
| 39 | + "device state information from specified device.") |
| 40 | public class NetconfGetCommand extends AbstractShellCommand { |
Yuta HIGUCHI | 57ba1e1 | 2017-04-26 15:51:47 -0700 | [diff] [blame] | 41 | |
Yuta HIGUCHI | 5ac3443 | 2018-02-13 16:29:15 -0800 | [diff] [blame] | 42 | @Argument(index = 0, name = "deviceId", description = "Device ID", |
Yuta HIGUCHI | 57ba1e1 | 2017-04-26 15:51:47 -0700 | [diff] [blame] | 43 | required = true, multiValued = false) |
| 44 | String uri = null; |
| 45 | |
Yuta HIGUCHI | 5ac3443 | 2018-02-13 16:29:15 -0800 | [diff] [blame] | 46 | @Option(name = "--timeout", |
| 47 | description = "Timeout in seconds", |
| 48 | required = false) |
| 49 | long timeoutSec = 30; |
Yuta HIGUCHI | 57ba1e1 | 2017-04-26 15:51:47 -0700 | [diff] [blame] | 50 | |
| 51 | @Override |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 52 | protected void doExecute() { |
Yuta HIGUCHI | 5ac3443 | 2018-02-13 16:29:15 -0800 | [diff] [blame] | 53 | DeviceId deviceId = DeviceId.deviceId(uri); |
Yuta HIGUCHI | d31bc6e | 2017-05-03 17:23:34 -0700 | [diff] [blame] | 54 | |
| 55 | NetconfController controller = get(NetconfController.class); |
| 56 | checkNotNull(controller, "Netconf controller is null"); |
| 57 | |
| 58 | NetconfDevice device = controller.getDevicesMap().get(deviceId); |
| 59 | if (device == null) { |
| 60 | print("Netconf device object not found for %s", deviceId); |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | NetconfSession session = device.getSession(); |
| 65 | if (session == null) { |
| 66 | print("Netconf session not found for %s", deviceId); |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | try { |
Yuta HIGUCHI | 5ac3443 | 2018-02-13 16:29:15 -0800 | [diff] [blame] | 71 | CharSequence res = session.asyncGet() |
| 72 | .get(timeoutSec, TimeUnit.SECONDS); |
Yuta HIGUCHI | d31bc6e | 2017-05-03 17:23:34 -0700 | [diff] [blame] | 73 | print("%s", res); |
Yuta HIGUCHI | 5ac3443 | 2018-02-13 16:29:15 -0800 | [diff] [blame] | 74 | } catch (NetconfException | InterruptedException | ExecutionException | TimeoutException e) { |
Yuta HIGUCHI | d31bc6e | 2017-05-03 17:23:34 -0700 | [diff] [blame] | 75 | log.error("Configuration could not be retrieved", e); |
Frank Wang | d8ab096 | 2017-08-11 11:09:30 +0800 | [diff] [blame] | 76 | print("Error occurred retrieving configuration"); |
Yuta HIGUCHI | d31bc6e | 2017-05-03 17:23:34 -0700 | [diff] [blame] | 77 | } |
Yuta HIGUCHI | 57ba1e1 | 2017-04-26 15:51:47 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | } |