blob: ba6f2f8161b0b43a449b57d36bd02264d842a305 [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 HIGUCHI5ac34432018-02-13 16:29:15 -080019import java.util.concurrent.ExecutionException;
20import java.util.concurrent.TimeUnit;
21import java.util.concurrent.TimeoutException;
Yuta HIGUCHId31bc6e2017-05-03 17:23:34 -070022
Ray Milkey86ad7bb2018-09-27 12:32:28 -070023import org.apache.karaf.shell.api.action.Argument;
24import org.apache.karaf.shell.api.action.Command;
25import org.apache.karaf.shell.api.action.Option;
Ray Milkey7a2dee52018-09-28 10:58:28 -070026import org.apache.karaf.shell.api.action.lifecycle.Service;
Yuta HIGUCHI57ba1e12017-04-26 15:51:47 -070027import org.onosproject.cli.AbstractShellCommand;
28import org.onosproject.net.DeviceId;
Yuta HIGUCHId31bc6e2017-05-03 17:23:34 -070029import org.onosproject.netconf.NetconfController;
30import org.onosproject.netconf.NetconfDevice;
Yuta HIGUCHI234eaf32017-09-06 13:45:05 -070031import org.onosproject.netconf.NetconfException;
Yuta HIGUCHId31bc6e2017-05-03 17:23:34 -070032import org.onosproject.netconf.NetconfSession;
Yuta HIGUCHI57ba1e12017-04-26 15:51:47 -070033
34/**
Yuta HIGUCHI5ac34432018-02-13 16:29:15 -080035 * Command that retrieves running configuration and device state.
36 * If configuration cannot be retrieved it prints an error string.
Yuta HIGUCHI57ba1e12017-04-26 15:51:47 -070037 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070038@Service
Yuta HIGUCHI5ac34432018-02-13 16:29:15 -080039@Command(scope = "onos", name = "netconf-get",
40 description = "Retrieve running configuration and "
41 + "device state information from specified device.")
42public class NetconfGetCommand extends AbstractShellCommand {
Yuta HIGUCHI57ba1e12017-04-26 15:51:47 -070043
Yuta HIGUCHI5ac34432018-02-13 16:29:15 -080044 @Argument(index = 0, name = "deviceId", description = "Device ID",
Yuta HIGUCHI57ba1e12017-04-26 15:51:47 -070045 required = true, multiValued = false)
46 String uri = null;
47
Yuta HIGUCHI5ac34432018-02-13 16:29:15 -080048 @Option(name = "--timeout",
49 description = "Timeout in seconds",
50 required = false)
51 long timeoutSec = 30;
Yuta HIGUCHI57ba1e12017-04-26 15:51:47 -070052
53 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070054 protected void doExecute() {
Yuta HIGUCHI5ac34432018-02-13 16:29:15 -080055 DeviceId deviceId = DeviceId.deviceId(uri);
Yuta HIGUCHId31bc6e2017-05-03 17:23:34 -070056
57 NetconfController controller = get(NetconfController.class);
58 checkNotNull(controller, "Netconf controller is null");
59
60 NetconfDevice device = controller.getDevicesMap().get(deviceId);
61 if (device == null) {
62 print("Netconf device object not found for %s", deviceId);
63 return;
64 }
65
66 NetconfSession session = device.getSession();
67 if (session == null) {
68 print("Netconf session not found for %s", deviceId);
69 return;
70 }
71
72 try {
Yuta HIGUCHI5ac34432018-02-13 16:29:15 -080073 CharSequence res = session.asyncGet()
74 .get(timeoutSec, TimeUnit.SECONDS);
Yuta HIGUCHId31bc6e2017-05-03 17:23:34 -070075 print("%s", res);
Yuta HIGUCHI5ac34432018-02-13 16:29:15 -080076 } catch (NetconfException | InterruptedException | ExecutionException | TimeoutException e) {
Yuta HIGUCHId31bc6e2017-05-03 17:23:34 -070077 log.error("Configuration could not be retrieved", e);
Frank Wangd8ab0962017-08-11 11:09:30 +080078 print("Error occurred retrieving configuration");
Yuta HIGUCHId31bc6e2017-05-03 17:23:34 -070079 }
Yuta HIGUCHI57ba1e12017-04-26 15:51:47 -070080 }
81
82}