blob: b7651a5f40b7e377056a06f6c0976f6ef85a253a [file] [log] [blame]
Yuta HIGUCHI57ba1e12017-04-26 15:51:47 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Yuta HIGUCHI57ba1e12017-04-26 15:51:47 -07003 *
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.netconf.cli.impl;
17
Yuta HIGUCHId31bc6e2017-05-03 17:23:34 -070018import static com.google.common.base.Preconditions.checkNotNull;
Yuta HIGUCHI89111d92017-05-04 11:29:17 -070019import static org.onosproject.netconf.DatastoreId.datastore;
Yuta HIGUCHId31bc6e2017-05-03 17:23:34 -070020
21import java.io.IOException;
22
Yuta HIGUCHI57ba1e12017-04-26 15:51:47 -070023import org.apache.karaf.shell.commands.Argument;
24import org.apache.karaf.shell.commands.Command;
25import org.onosproject.cli.AbstractShellCommand;
26import org.onosproject.net.DeviceId;
Yuta HIGUCHId31bc6e2017-05-03 17:23:34 -070027import org.onosproject.netconf.NetconfController;
28import org.onosproject.netconf.NetconfDevice;
29import org.onosproject.netconf.NetconfSession;
Yuta HIGUCHI57ba1e12017-04-26 15:51:47 -070030
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 HIGUCHI57ba1e12017-04-26 15:51:47 -070034 */
Yuta HIGUCHI57ba1e12017-04-26 15:51:47 -070035@Command(scope = "onos", name = "netconf-get-config",
36 description = "Gets the configuration of the specified type from the" +
37 "specified device.")
38public 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 HIGUCHI89111d92017-05-04 11:29:17 -070045 description = "Configuration datastore name (running, etc.)",
Yuta HIGUCHI57ba1e12017-04-26 15:51:47 -070046 required = true, multiValued = false)
47 String cfgType = null;
48
49
50 private DeviceId deviceId;
51
52 @Override
53 protected void execute() {
Yuta HIGUCHI57ba1e12017-04-26 15:51:47 -070054 deviceId = DeviceId.deviceId(uri);
Yuta HIGUCHId31bc6e2017-05-03 17:23:34 -070055
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 HIGUCHI89111d92017-05-04 11:29:17 -070072 String res = session.getConfig(datastore(cfgType.toLowerCase()));
Yuta HIGUCHId31bc6e2017-05-03 17:23:34 -070073 print("%s", res);
74 } catch (IOException e) {
75 log.error("Configuration could not be retrieved", e);
Frank Wangd8ab0962017-08-11 11:09:30 +080076 print("Error occurred retrieving configuration");
Yuta HIGUCHId31bc6e2017-05-03 17:23:34 -070077 }
Yuta HIGUCHI57ba1e12017-04-26 15:51:47 -070078 }
79
80}