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