blob: 087af0cd3c12eab22e95b9fedc83a94417ffa8f7 [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
22import java.io.IOException;
23
24import org.apache.felix.scr.annotations.Reference;
25import org.apache.felix.scr.annotations.ReferenceCardinality;
26import org.onosproject.net.DeviceId;
27import org.onosproject.net.behaviour.ConfigGetter;
28import org.onosproject.net.driver.AbstractHandlerBehaviour;
29import org.onosproject.net.driver.DriverHandler;
30import org.onosproject.net.packet.PacketProcessor;
31import org.onosproject.net.packet.PacketService;
32import org.onosproject.netconf.NetconfController;
33import org.slf4j.Logger;
34
35import com.google.common.base.Preconditions;
36
37/**
38 * Used with the onos:device-configuration CLI command.
39 *
40 * This allows the full configuration to be retrieved from the device
41 */
42public class NetconfConfigGetter extends AbstractHandlerBehaviour implements ConfigGetter {
43
44 private final Logger log = getLogger(getClass());
45
46 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
47 protected PacketService packetService;
48
49 private PacketProcessor testProcessor;
50
51 // FIXME the error string should be universal for all implementations of
52 // ConfigGetter
53 public static final String UNABLE_TO_READ_CONFIG = "config retrieval error";
54
55 @Override
56 public String getConfiguration(String type) {
57 DriverHandler handler = handler();
58 NetconfController controller = handler.get(NetconfController.class);
59
60 DeviceId ofDeviceId = handler.data().deviceId();
61 Preconditions.checkNotNull(controller, "Netconf controller is null");
62 if (type == null ||
63 (!type.equalsIgnoreCase("running")
64 && !type.equalsIgnoreCase("candidate")
65 && !type.equalsIgnoreCase("startup"))) {
66 log.error("Configuration type must be either 'running', 'startup' or 'candidate'. '{}' is invalid", type);
67 return UNABLE_TO_READ_CONFIG;
68 }
69 try {
Yuta HIGUCHIdd7c3f82017-09-03 14:18:01 -070070 return controller.getDevicesMap().get(ofDeviceId).getSession()
71 .getConfig(datastore(type.replace("cfgType=", "")));
Sean Condonfae8e662016-12-15 10:25:13 +000072 } catch (IOException e) {
73 log.error("Configuration could not be retrieved {}", e.getMessage());
74 }
75 return UNABLE_TO_READ_CONFIG;
76 }
77}