blob: 53151bbf093f5725feddaf2fcb4bef716e149601 [file] [log] [blame]
Sean Condonfae8e662016-12-15 10:25:13 +00001/*
2 * Copyright 2016-present Open Networking Laboratory
3 *
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
19import static org.slf4j.LoggerFactory.getLogger;
20
21import java.io.IOException;
22
23import org.apache.felix.scr.annotations.Reference;
24import org.apache.felix.scr.annotations.ReferenceCardinality;
25import org.onosproject.net.DeviceId;
26import org.onosproject.net.behaviour.ConfigGetter;
27import org.onosproject.net.driver.AbstractHandlerBehaviour;
28import org.onosproject.net.driver.DriverHandler;
29import org.onosproject.net.packet.PacketProcessor;
30import org.onosproject.net.packet.PacketService;
31import org.onosproject.netconf.NetconfController;
32import 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
45 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
46 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 {
69 return controller.getDevicesMap().get(ofDeviceId).getSession().getConfig(type.replace("cfgType=", ""));
70 } catch (IOException e) {
71 log.error("Configuration could not be retrieved {}", e.getMessage());
72 }
73 return UNABLE_TO_READ_CONFIG;
74 }
75}