blob: 8198102a7ab3510d1891067ef7206d7e405c8583 [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
Yuta HIGUCHI5ac34432018-02-13 16:29:15 -080021import java.util.concurrent.ExecutionException;
22import java.util.concurrent.TimeUnit;
23import java.util.concurrent.TimeoutException;
24
Ray Milkey86ad7bb2018-09-27 12:32:28 -070025import org.apache.karaf.shell.api.action.Argument;
26import org.apache.karaf.shell.api.action.Command;
Ray Milkeyec20a292018-10-11 15:53:33 -070027import org.apache.karaf.shell.api.action.Completion;
Ray Milkey86ad7bb2018-09-27 12:32:28 -070028import org.apache.karaf.shell.api.action.Option;
Ray Milkey7a2dee52018-09-28 10:58:28 -070029import org.apache.karaf.shell.api.action.lifecycle.Service;
Yuta HIGUCHI57ba1e12017-04-26 15:51:47 -070030import org.onosproject.cli.AbstractShellCommand;
Ray Milkeyec20a292018-10-11 15:53:33 -070031import org.onosproject.cli.net.DeviceIdCompleter;
Yuta HIGUCHI57ba1e12017-04-26 15:51:47 -070032import org.onosproject.net.DeviceId;
Yuta HIGUCHId31bc6e2017-05-03 17:23:34 -070033import org.onosproject.netconf.NetconfController;
34import org.onosproject.netconf.NetconfDevice;
Yuta HIGUCHI234eaf32017-09-06 13:45:05 -070035import org.onosproject.netconf.NetconfException;
Yuta HIGUCHId31bc6e2017-05-03 17:23:34 -070036import org.onosproject.netconf.NetconfSession;
Ray Milkeyec20a292018-10-11 15:53:33 -070037import org.onosproject.netconf.cli.impl.completers.DatastoreIdCompleter;
Yuta HIGUCHI57ba1e12017-04-26 15:51:47 -070038
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 HIGUCHI57ba1e12017-04-26 15:51:47 -070042 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070043@Service
Yuta HIGUCHI57ba1e12017-04-26 15:51:47 -070044@Command(scope = "onos", name = "netconf-get-config",
45 description = "Gets the configuration of the specified type from the" +
46 "specified device.")
Yuta HIGUCHI5ac34432018-02-13 16:29:15 -080047public class NetconfGetConfigCommand extends AbstractShellCommand {
Yuta HIGUCHI57ba1e12017-04-26 15:51:47 -070048
Yuta HIGUCHI5ac34432018-02-13 16:29:15 -080049 @Argument(index = 0, name = "deviceId", description = "Device ID",
Yuta HIGUCHI57ba1e12017-04-26 15:51:47 -070050 required = true, multiValued = false)
Ray Milkeyec20a292018-10-11 15:53:33 -070051 @Completion(DeviceIdCompleter.class)
Yuta HIGUCHI57ba1e12017-04-26 15:51:47 -070052 String uri = null;
53
Yuta HIGUCHI5ac34432018-02-13 16:29:15 -080054 @Argument(index = 1, name = "datastore",
Yuta HIGUCHI89111d92017-05-04 11:29:17 -070055 description = "Configuration datastore name (running, etc.)",
Yuta HIGUCHI5ac34432018-02-13 16:29:15 -080056 required = false, multiValued = false)
Ray Milkeyec20a292018-10-11 15:53:33 -070057 @Completion(DatastoreIdCompleter.class)
Yuta HIGUCHI5ac34432018-02-13 16:29:15 -080058 String datastore = "running";
Yuta HIGUCHI57ba1e12017-04-26 15:51:47 -070059
Yuta HIGUCHI5ac34432018-02-13 16:29:15 -080060 @Option(name = "--timeout",
61 description = "Timeout in seconds",
62 required = false)
63 long timeoutSec = 30;
Yuta HIGUCHI57ba1e12017-04-26 15:51:47 -070064
65 private DeviceId deviceId;
66
67 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070068 protected void doExecute() {
Yuta HIGUCHI57ba1e12017-04-26 15:51:47 -070069 deviceId = DeviceId.deviceId(uri);
Yuta HIGUCHId31bc6e2017-05-03 17:23:34 -070070
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 HIGUCHI5ac34432018-02-13 16:29:15 -080087 CharSequence res = session.asyncGetConfig(datastore(datastore.toLowerCase()))
88 .get(timeoutSec, TimeUnit.SECONDS);
Yuta HIGUCHId31bc6e2017-05-03 17:23:34 -070089 print("%s", res);
Yuta HIGUCHI5ac34432018-02-13 16:29:15 -080090 } catch (NetconfException | InterruptedException | ExecutionException | TimeoutException e) {
Yuta HIGUCHId31bc6e2017-05-03 17:23:34 -070091 log.error("Configuration could not be retrieved", e);
Frank Wangd8ab0962017-08-11 11:09:30 +080092 print("Error occurred retrieving configuration");
Yuta HIGUCHId31bc6e2017-05-03 17:23:34 -070093 }
Yuta HIGUCHI57ba1e12017-04-26 15:51:47 -070094 }
95
96}