Sean Condon | fae8e66 | 2016-12-15 10:25:13 +0000 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Sean Condon | fae8e66 | 2016-12-15 10:25:13 +0000 | [diff] [blame] | 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 | |
| 17 | package org.onosproject.drivers.microsemi; |
| 18 | |
Yuta HIGUCHI | dd7c3f8 | 2017-09-03 14:18:01 -0700 | [diff] [blame] | 19 | import static org.onosproject.netconf.DatastoreId.datastore; |
Sean Condon | fae8e66 | 2016-12-15 10:25:13 +0000 | [diff] [blame] | 20 | import static org.slf4j.LoggerFactory.getLogger; |
| 21 | |
Sean Condon | fae8e66 | 2016-12-15 10:25:13 +0000 | [diff] [blame] | 22 | import org.apache.felix.scr.annotations.Reference; |
| 23 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 24 | import org.onosproject.net.DeviceId; |
| 25 | import org.onosproject.net.behaviour.ConfigGetter; |
| 26 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 27 | import org.onosproject.net.driver.DriverHandler; |
| 28 | import org.onosproject.net.packet.PacketProcessor; |
| 29 | import org.onosproject.net.packet.PacketService; |
| 30 | import org.onosproject.netconf.NetconfController; |
Yuta HIGUCHI | 234eaf3 | 2017-09-06 13:45:05 -0700 | [diff] [blame] | 31 | import org.onosproject.netconf.NetconfException; |
Sean Condon | fae8e66 | 2016-12-15 10:25:13 +0000 | [diff] [blame] | 32 | import org.slf4j.Logger; |
| 33 | |
| 34 | import 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 | */ |
| 41 | public 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 { |
Yuta HIGUCHI | dd7c3f8 | 2017-09-03 14:18:01 -0700 | [diff] [blame] | 69 | return controller.getDevicesMap().get(ofDeviceId).getSession() |
| 70 | .getConfig(datastore(type.replace("cfgType=", ""))); |
Yuta HIGUCHI | 234eaf3 | 2017-09-06 13:45:05 -0700 | [diff] [blame] | 71 | } catch (NetconfException e) { |
Sean Condon | fae8e66 | 2016-12-15 10:25:13 +0000 | [diff] [blame] | 72 | log.error("Configuration could not be retrieved {}", e.getMessage()); |
| 73 | } |
| 74 | return UNABLE_TO_READ_CONFIG; |
| 75 | } |
| 76 | } |