blob: 7d937d03e497a59c65b64ab1fb7ebfe7b103d354 [file] [log] [blame]
Sean Condonfae8e662016-12-15 10:25:13 +00001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Sean Condonfae8e662016-12-15 10:25:13 +00003 *
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
17package org.onosproject.drivers.microsemi;
18
Yuta HIGUCHIdd7c3f82017-09-03 14:18:01 -070019import static org.onosproject.netconf.DatastoreId.datastore;
Sean Condonfae8e662016-12-15 10:25:13 +000020import static org.slf4j.LoggerFactory.getLogger;
21
Ray Milkeyd84f89b2018-08-17 14:54:17 -070022import org.osgi.service.component.annotations.Reference;
23import org.osgi.service.component.annotations.ReferenceCardinality;
Sean Condonfae8e662016-12-15 10:25:13 +000024import org.onosproject.net.DeviceId;
25import org.onosproject.net.behaviour.ConfigGetter;
26import org.onosproject.net.driver.AbstractHandlerBehaviour;
27import org.onosproject.net.driver.DriverHandler;
28import org.onosproject.net.packet.PacketProcessor;
29import org.onosproject.net.packet.PacketService;
30import org.onosproject.netconf.NetconfController;
Yuta HIGUCHI234eaf32017-09-06 13:45:05 -070031import org.onosproject.netconf.NetconfException;
Sean Condonfae8e662016-12-15 10:25:13 +000032import org.slf4j.Logger;
33
34import com.google.common.base.Preconditions;
35
36/**
37 * Used with the onos:device-configuration CLI command.
38 *
39 * This allows the full configuration to be retrieved from the device
40 */
41public class NetconfConfigGetter extends AbstractHandlerBehaviour implements ConfigGetter {
42
43 private final Logger log = getLogger(getClass());
44
Ray Milkeyd84f89b2018-08-17 14:54:17 -070045 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Sean Condonfae8e662016-12-15 10:25:13 +000046 protected PacketService packetService;
47
48 private PacketProcessor testProcessor;
49
50 // FIXME the error string should be universal for all implementations of
51 // ConfigGetter
52 public static final String UNABLE_TO_READ_CONFIG = "config retrieval error";
53
54 @Override
55 public String getConfiguration(String type) {
56 DriverHandler handler = handler();
57 NetconfController controller = handler.get(NetconfController.class);
58
59 DeviceId ofDeviceId = handler.data().deviceId();
60 Preconditions.checkNotNull(controller, "Netconf controller is null");
61 if (type == null ||
62 (!type.equalsIgnoreCase("running")
63 && !type.equalsIgnoreCase("candidate")
64 && !type.equalsIgnoreCase("startup"))) {
65 log.error("Configuration type must be either 'running', 'startup' or 'candidate'. '{}' is invalid", type);
66 return UNABLE_TO_READ_CONFIG;
67 }
68 try {
Yuta HIGUCHIdd7c3f82017-09-03 14:18:01 -070069 return controller.getDevicesMap().get(ofDeviceId).getSession()
70 .getConfig(datastore(type.replace("cfgType=", "")));
Yuta HIGUCHI234eaf32017-09-06 13:45:05 -070071 } catch (NetconfException e) {
Sean Condonfae8e662016-12-15 10:25:13 +000072 log.error("Configuration could not be retrieved {}", e.getMessage());
73 }
74 return UNABLE_TO_READ_CONFIG;
75 }
76}