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 | 89111d9 | 2017-05-04 11:29:17 -0700 | [diff] [blame] | 19 | import static org.onosproject.netconf.DatastoreId.datastore; |
Yuta HIGUCHI | d31bc6e | 2017-05-03 17:23:34 -0700 | [diff] [blame] | 20 | |
Yuta HIGUCHI | 5ac3443 | 2018-02-13 16:29:15 -0800 | [diff] [blame] | 21 | import java.util.concurrent.ExecutionException; |
| 22 | import java.util.concurrent.TimeUnit; |
| 23 | import java.util.concurrent.TimeoutException; |
| 24 | |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 25 | import org.apache.karaf.shell.api.action.Argument; |
| 26 | import org.apache.karaf.shell.api.action.Command; |
Ray Milkey | ec20a29 | 2018-10-11 15:53:33 -0700 | [diff] [blame] | 27 | import org.apache.karaf.shell.api.action.Completion; |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 28 | import org.apache.karaf.shell.api.action.Option; |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame] | 29 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
Yuta HIGUCHI | 57ba1e1 | 2017-04-26 15:51:47 -0700 | [diff] [blame] | 30 | import org.onosproject.cli.AbstractShellCommand; |
Ray Milkey | ec20a29 | 2018-10-11 15:53:33 -0700 | [diff] [blame] | 31 | import org.onosproject.cli.net.DeviceIdCompleter; |
Yuta HIGUCHI | 57ba1e1 | 2017-04-26 15:51:47 -0700 | [diff] [blame] | 32 | import org.onosproject.net.DeviceId; |
Yuta HIGUCHI | d31bc6e | 2017-05-03 17:23:34 -0700 | [diff] [blame] | 33 | import org.onosproject.netconf.NetconfController; |
| 34 | import org.onosproject.netconf.NetconfDevice; |
Yuta HIGUCHI | 234eaf3 | 2017-09-06 13:45:05 -0700 | [diff] [blame] | 35 | import org.onosproject.netconf.NetconfException; |
Yuta HIGUCHI | d31bc6e | 2017-05-03 17:23:34 -0700 | [diff] [blame] | 36 | import org.onosproject.netconf.NetconfSession; |
Ray Milkey | ec20a29 | 2018-10-11 15:53:33 -0700 | [diff] [blame] | 37 | import org.onosproject.netconf.cli.impl.completers.DatastoreIdCompleter; |
Yuta HIGUCHI | 57ba1e1 | 2017-04-26 15:51:47 -0700 | [diff] [blame] | 38 | |
| 39 | /** |
| 40 | * Command that gets the configuration of the specified type from the specified |
| 41 | * device. If configuration cannot be retrieved it prints an error string. |
Yuta HIGUCHI | 57ba1e1 | 2017-04-26 15:51:47 -0700 | [diff] [blame] | 42 | */ |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame] | 43 | @Service |
Yuta HIGUCHI | 57ba1e1 | 2017-04-26 15:51:47 -0700 | [diff] [blame] | 44 | @Command(scope = "onos", name = "netconf-get-config", |
| 45 | description = "Gets the configuration of the specified type from the" + |
| 46 | "specified device.") |
Yuta HIGUCHI | 5ac3443 | 2018-02-13 16:29:15 -0800 | [diff] [blame] | 47 | public class NetconfGetConfigCommand extends AbstractShellCommand { |
Yuta HIGUCHI | 57ba1e1 | 2017-04-26 15:51:47 -0700 | [diff] [blame] | 48 | |
Yuta HIGUCHI | 5ac3443 | 2018-02-13 16:29:15 -0800 | [diff] [blame] | 49 | @Argument(index = 0, name = "deviceId", description = "Device ID", |
Yuta HIGUCHI | 57ba1e1 | 2017-04-26 15:51:47 -0700 | [diff] [blame] | 50 | required = true, multiValued = false) |
Ray Milkey | ec20a29 | 2018-10-11 15:53:33 -0700 | [diff] [blame] | 51 | @Completion(DeviceIdCompleter.class) |
Yuta HIGUCHI | 57ba1e1 | 2017-04-26 15:51:47 -0700 | [diff] [blame] | 52 | String uri = null; |
| 53 | |
Yuta HIGUCHI | 5ac3443 | 2018-02-13 16:29:15 -0800 | [diff] [blame] | 54 | @Argument(index = 1, name = "datastore", |
Yuta HIGUCHI | 89111d9 | 2017-05-04 11:29:17 -0700 | [diff] [blame] | 55 | description = "Configuration datastore name (running, etc.)", |
Yuta HIGUCHI | 5ac3443 | 2018-02-13 16:29:15 -0800 | [diff] [blame] | 56 | required = false, multiValued = false) |
Ray Milkey | ec20a29 | 2018-10-11 15:53:33 -0700 | [diff] [blame] | 57 | @Completion(DatastoreIdCompleter.class) |
Yuta HIGUCHI | 5ac3443 | 2018-02-13 16:29:15 -0800 | [diff] [blame] | 58 | String datastore = "running"; |
Yuta HIGUCHI | 57ba1e1 | 2017-04-26 15:51:47 -0700 | [diff] [blame] | 59 | |
Yuta HIGUCHI | 5ac3443 | 2018-02-13 16:29:15 -0800 | [diff] [blame] | 60 | @Option(name = "--timeout", |
| 61 | description = "Timeout in seconds", |
| 62 | required = false) |
| 63 | long timeoutSec = 30; |
Yuta HIGUCHI | 57ba1e1 | 2017-04-26 15:51:47 -0700 | [diff] [blame] | 64 | |
| 65 | private DeviceId deviceId; |
| 66 | |
| 67 | @Override |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 68 | protected void doExecute() { |
Yuta HIGUCHI | 57ba1e1 | 2017-04-26 15:51:47 -0700 | [diff] [blame] | 69 | deviceId = DeviceId.deviceId(uri); |
Yuta HIGUCHI | d31bc6e | 2017-05-03 17:23:34 -0700 | [diff] [blame] | 70 | |
| 71 | NetconfController controller = get(NetconfController.class); |
| 72 | checkNotNull(controller, "Netconf controller is null"); |
| 73 | |
| 74 | NetconfDevice device = controller.getDevicesMap().get(deviceId); |
| 75 | if (device == null) { |
| 76 | print("Netconf device object not found for %s", deviceId); |
| 77 | return; |
| 78 | } |
| 79 | |
| 80 | NetconfSession session = device.getSession(); |
| 81 | if (session == null) { |
| 82 | print("Netconf session not found for %s", deviceId); |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | try { |
Yuta HIGUCHI | 5ac3443 | 2018-02-13 16:29:15 -0800 | [diff] [blame] | 87 | CharSequence res = session.asyncGetConfig(datastore(datastore.toLowerCase())) |
| 88 | .get(timeoutSec, TimeUnit.SECONDS); |
Yuta HIGUCHI | d31bc6e | 2017-05-03 17:23:34 -0700 | [diff] [blame] | 89 | print("%s", res); |
Yuta HIGUCHI | 5ac3443 | 2018-02-13 16:29:15 -0800 | [diff] [blame] | 90 | } catch (NetconfException | InterruptedException | ExecutionException | TimeoutException e) { |
Yuta HIGUCHI | d31bc6e | 2017-05-03 17:23:34 -0700 | [diff] [blame] | 91 | log.error("Configuration could not be retrieved", e); |
Frank Wang | d8ab096 | 2017-08-11 11:09:30 +0800 | [diff] [blame] | 92 | print("Error occurred retrieving configuration"); |
Yuta HIGUCHI | d31bc6e | 2017-05-03 17:23:34 -0700 | [diff] [blame] | 93 | } |
Yuta HIGUCHI | 57ba1e1 | 2017-04-26 15:51:47 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | } |