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