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; |
| 30 | import java.nio.charset.StandardCharsets; |
| 31 | import java.util.ArrayList; |
| 32 | import java.util.List; |
| 33 | |
| 34 | import static org.slf4j.LoggerFactory.getLogger; |
| 35 | |
| 36 | /** |
| 37 | * Implementation of controller config which allows to get and set controllers |
| 38 | * through the Netconf protocol. |
| 39 | */ |
| 40 | public class NetconfControllerConfig extends AbstractHandlerBehaviour |
| 41 | implements ControllerConfig { |
| 42 | |
| 43 | private final Logger log = getLogger(NetconfControllerConfig.class); |
| 44 | |
| 45 | @Override |
| 46 | public List<ControllerInfo> getControllers() { |
| 47 | DriverHandler handler = handler(); |
| 48 | NetconfController controller = handler.get(NetconfController.class); |
| 49 | DeviceId ofDeviceId = handler.data().deviceId(); |
| 50 | Preconditions.checkNotNull(controller, "Netconf controller is null"); |
| 51 | List<ControllerInfo> controllers = new ArrayList<>(); |
| 52 | controllers.addAll(XmlConfigParser.parseStreamControllers(XmlConfigParser. |
| 53 | loadXml(new ByteArrayInputStream(controller. |
| 54 | getDevicesMap().get(ofDeviceId).getSession(). |
| 55 | getConfig("running").getBytes(StandardCharsets.UTF_8))))); |
| 56 | return controllers; |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public void setControllers(List<ControllerInfo> controllers) { |
| 61 | DriverHandler handler = handler(); |
| 62 | NetconfController controller = handler.get(NetconfController.class); |
| 63 | DeviceId deviceId = handler.data().deviceId(); |
| 64 | Preconditions.checkNotNull(controller, "Netconf controller is null"); |
| 65 | try { |
| 66 | NetconfDevice device = controller.getNetconfDevice(deviceId); |
| 67 | log.warn("provider map {}", controller.getDevicesMap()); |
| 68 | String config = XmlConfigParser.createControllersConfig( |
| 69 | XmlConfigParser.loadXml(getClass().getResourceAsStream("controllers.xml")), |
| 70 | XmlConfigParser.loadXml( |
| 71 | new ByteArrayInputStream(device.getSession() |
| 72 | .getConfig("running") |
| 73 | .getBytes( |
| 74 | StandardCharsets.UTF_8))), |
| 75 | "running", "merge", "create", controllers |
| 76 | ); |
| 77 | device.getSession().editConfig(config.substring(config.indexOf("-->") + 3)); |
| 78 | } catch (NullPointerException e) { |
| 79 | log.warn("No NETCONF device with requested parameters " + e); |
| 80 | throw new NullPointerException("No NETCONF device with requested parameters " + e); |
| 81 | } |
| 82 | |
| 83 | } |
| 84 | |
| 85 | //TODO maybe put method getNetconfClientService like in ovsdb if we need it |
| 86 | |
| 87 | } |
| 88 | |
| 89 | |