andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | |
Andrea Campanella | 238d96e | 2016-01-20 11:52:02 -0800 | [diff] [blame] | 17 | package org.onosproject.drivers.netconf; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 18 | |
| 19 | import com.google.common.base.Preconditions; |
Andrea Campanella | 238d96e | 2016-01-20 11:52:02 -0800 | [diff] [blame] | 20 | import org.onosproject.drivers.utilities.XmlConfigParser; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 21 | import org.onosproject.net.DeviceId; |
| 22 | import org.onosproject.net.behaviour.ControllerConfig; |
| 23 | import org.onosproject.net.behaviour.ControllerInfo; |
| 24 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 25 | import org.onosproject.net.driver.DriverHandler; |
| 26 | import org.onosproject.netconf.NetconfController; |
| 27 | import org.onosproject.netconf.NetconfDevice; |
| 28 | import org.slf4j.Logger; |
| 29 | |
| 30 | import java.io.ByteArrayInputStream; |
Andrea Campanella | 1cd641b | 2015-12-07 17:28:34 -0800 | [diff] [blame] | 31 | import java.io.IOException; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 32 | import java.nio.charset.StandardCharsets; |
| 33 | import java.util.ArrayList; |
| 34 | import java.util.List; |
| 35 | |
| 36 | import static org.slf4j.LoggerFactory.getLogger; |
| 37 | |
| 38 | /** |
| 39 | * Implementation of controller config which allows to get and set controllers |
| 40 | * through the Netconf protocol. |
| 41 | */ |
| 42 | public class NetconfControllerConfig extends AbstractHandlerBehaviour |
| 43 | implements ControllerConfig { |
| 44 | |
| 45 | private final Logger log = getLogger(NetconfControllerConfig.class); |
| 46 | |
| 47 | @Override |
| 48 | public List<ControllerInfo> getControllers() { |
| 49 | DriverHandler handler = handler(); |
| 50 | NetconfController controller = handler.get(NetconfController.class); |
| 51 | DeviceId ofDeviceId = handler.data().deviceId(); |
| 52 | Preconditions.checkNotNull(controller, "Netconf controller is null"); |
| 53 | List<ControllerInfo> controllers = new ArrayList<>(); |
Andrea Campanella | 1cd641b | 2015-12-07 17:28:34 -0800 | [diff] [blame] | 54 | try { |
Andrea Campanella | b029b9e | 2016-01-29 11:05:36 -0800 | [diff] [blame] | 55 | String reply = controller.getDevicesMap().get(ofDeviceId).getSession(). |
| 56 | getConfig("running"); |
| 57 | log.debug("Reply XML {}", reply); |
Andrea Campanella | 1cd641b | 2015-12-07 17:28:34 -0800 | [diff] [blame] | 58 | controllers.addAll(XmlConfigParser.parseStreamControllers(XmlConfigParser. |
Andrea Campanella | b029b9e | 2016-01-29 11:05:36 -0800 | [diff] [blame] | 59 | loadXml(new ByteArrayInputStream(reply.getBytes(StandardCharsets.UTF_8))))); |
Andrea Campanella | 1cd641b | 2015-12-07 17:28:34 -0800 | [diff] [blame] | 60 | } catch (IOException e) { |
Marc De Leenheer | c662d32 | 2016-02-18 16:05:10 -0800 | [diff] [blame] | 61 | log.error("Cannot communicate with device {} ", ofDeviceId); |
Andrea Campanella | 1cd641b | 2015-12-07 17:28:34 -0800 | [diff] [blame] | 62 | } |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 63 | return controllers; |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public void setControllers(List<ControllerInfo> controllers) { |
| 68 | DriverHandler handler = handler(); |
| 69 | NetconfController controller = handler.get(NetconfController.class); |
| 70 | DeviceId deviceId = handler.data().deviceId(); |
| 71 | Preconditions.checkNotNull(controller, "Netconf controller is null"); |
| 72 | try { |
| 73 | NetconfDevice device = controller.getNetconfDevice(deviceId); |
Andrea Campanella | 1cd641b | 2015-12-07 17:28:34 -0800 | [diff] [blame] | 74 | String config = null; |
Andrea Campanella | b029b9e | 2016-01-29 11:05:36 -0800 | [diff] [blame] | 75 | |
Andrea Campanella | 1cd641b | 2015-12-07 17:28:34 -0800 | [diff] [blame] | 76 | try { |
Andrea Campanella | b029b9e | 2016-01-29 11:05:36 -0800 | [diff] [blame] | 77 | String reply = device.getSession().getConfig("running"); |
| 78 | log.info("reply XML {}", reply); |
Andrea Campanella | 1cd641b | 2015-12-07 17:28:34 -0800 | [diff] [blame] | 79 | config = XmlConfigParser.createControllersConfig( |
| 80 | XmlConfigParser.loadXml(getClass().getResourceAsStream("controllers.xml")), |
| 81 | XmlConfigParser.loadXml( |
Andrea Campanella | b029b9e | 2016-01-29 11:05:36 -0800 | [diff] [blame] | 82 | new ByteArrayInputStream(reply.getBytes(StandardCharsets.UTF_8))), |
Andrea Campanella | 1cd641b | 2015-12-07 17:28:34 -0800 | [diff] [blame] | 83 | "running", "merge", "create", controllers |
| 84 | ); |
| 85 | } catch (IOException e) { |
| 86 | log.error("Cannot comunicate to device {} , exception {}", deviceId, e.getMessage()); |
| 87 | } |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 88 | device.getSession().editConfig(config.substring(config.indexOf("-->") + 3)); |
| 89 | } catch (NullPointerException e) { |
| 90 | log.warn("No NETCONF device with requested parameters " + e); |
| 91 | throw new NullPointerException("No NETCONF device with requested parameters " + e); |
Andrea Campanella | 1cd641b | 2015-12-07 17:28:34 -0800 | [diff] [blame] | 92 | } catch (IOException e) { |
| 93 | log.error("Cannot comunicate to device {} , exception {}", deviceId, e.getMessage()); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | } |
| 97 | |
| 98 | //TODO maybe put method getNetconfClientService like in ovsdb if we need it |
| 99 | |
| 100 | } |
| 101 | |
| 102 | |