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