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