Yuta HIGUCHI | 57ba1e1 | 2017-04-26 15:51:47 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 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 | |
| 21 | import java.io.IOException; |
| 22 | |
Yuta HIGUCHI | 57ba1e1 | 2017-04-26 15:51:47 -0700 | [diff] [blame] | 23 | import org.apache.karaf.shell.commands.Argument; |
| 24 | import org.apache.karaf.shell.commands.Command; |
| 25 | import org.onosproject.cli.AbstractShellCommand; |
| 26 | import org.onosproject.net.DeviceId; |
Yuta HIGUCHI | d31bc6e | 2017-05-03 17:23:34 -0700 | [diff] [blame] | 27 | import org.onosproject.netconf.NetconfController; |
| 28 | import org.onosproject.netconf.NetconfDevice; |
| 29 | import org.onosproject.netconf.NetconfSession; |
Yuta HIGUCHI | 57ba1e1 | 2017-04-26 15:51:47 -0700 | [diff] [blame] | 30 | |
| 31 | /** |
| 32 | * Command that gets the configuration of the specified type from the specified |
| 33 | * device. If configuration cannot be retrieved it prints an error string. |
Yuta HIGUCHI | 57ba1e1 | 2017-04-26 15:51:47 -0700 | [diff] [blame] | 34 | */ |
Yuta HIGUCHI | 57ba1e1 | 2017-04-26 15:51:47 -0700 | [diff] [blame] | 35 | @Command(scope = "onos", name = "netconf-get-config", |
| 36 | description = "Gets the configuration of the specified type from the" + |
| 37 | "specified device.") |
| 38 | public class NetconfConfigGetCommand extends AbstractShellCommand { |
| 39 | |
| 40 | @Argument(index = 0, name = "uri", description = "Device ID", |
| 41 | required = true, multiValued = false) |
| 42 | String uri = null; |
| 43 | |
| 44 | @Argument(index = 1, name = "cfgType", |
Yuta HIGUCHI | 89111d9 | 2017-05-04 11:29:17 -0700 | [diff] [blame] | 45 | description = "Configuration datastore name (running, etc.)", |
Yuta HIGUCHI | 57ba1e1 | 2017-04-26 15:51:47 -0700 | [diff] [blame] | 46 | required = true, multiValued = false) |
| 47 | String cfgType = null; |
| 48 | |
| 49 | |
| 50 | private DeviceId deviceId; |
| 51 | |
| 52 | @Override |
| 53 | protected void execute() { |
Yuta HIGUCHI | 57ba1e1 | 2017-04-26 15:51:47 -0700 | [diff] [blame] | 54 | deviceId = DeviceId.deviceId(uri); |
Yuta HIGUCHI | d31bc6e | 2017-05-03 17:23:34 -0700 | [diff] [blame] | 55 | |
| 56 | NetconfController controller = get(NetconfController.class); |
| 57 | checkNotNull(controller, "Netconf controller is null"); |
| 58 | |
| 59 | NetconfDevice device = controller.getDevicesMap().get(deviceId); |
| 60 | if (device == null) { |
| 61 | print("Netconf device object not found for %s", deviceId); |
| 62 | return; |
| 63 | } |
| 64 | |
| 65 | NetconfSession session = device.getSession(); |
| 66 | if (session == null) { |
| 67 | print("Netconf session not found for %s", deviceId); |
| 68 | return; |
| 69 | } |
| 70 | |
| 71 | try { |
Yuta HIGUCHI | 89111d9 | 2017-05-04 11:29:17 -0700 | [diff] [blame] | 72 | String res = session.getConfig(datastore(cfgType.toLowerCase())); |
Yuta HIGUCHI | d31bc6e | 2017-05-03 17:23:34 -0700 | [diff] [blame] | 73 | print("%s", res); |
| 74 | } catch (IOException e) { |
| 75 | log.error("Configuration could not be retrieved", e); |
| 76 | print("Error occured retrieving configuration"); |
| 77 | } |
Yuta HIGUCHI | 57ba1e1 | 2017-04-26 15:51:47 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | } |