blob: f094ba1f627f4937798bb1cd7a6f271ad40853c0 [file] [log] [blame]
andreaeb70a942015-10-16 21:34:46 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
andreaeb70a942015-10-16 21:34:46 -07003 *
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 Campanella238d96e2016-01-20 11:52:02 -080017package org.onosproject.drivers.netconf;
andreaeb70a942015-10-16 21:34:46 -070018
19import com.google.common.base.Preconditions;
Andrea Campanella238d96e2016-01-20 11:52:02 -080020import org.onosproject.drivers.utilities.XmlConfigParser;
Andrea Campanella7e6200a2016-03-21 09:48:40 -070021import org.onosproject.mastership.MastershipService;
andreaeb70a942015-10-16 21:34:46 -070022import org.onosproject.net.DeviceId;
23import org.onosproject.net.behaviour.ControllerConfig;
24import org.onosproject.net.behaviour.ControllerInfo;
25import org.onosproject.net.driver.AbstractHandlerBehaviour;
26import org.onosproject.net.driver.DriverHandler;
27import org.onosproject.netconf.NetconfController;
28import org.onosproject.netconf.NetconfDevice;
Andrei Mihaescuac542ca2017-03-26 21:36:25 +030029
andreaeb70a942015-10-16 21:34:46 -070030import org.slf4j.Logger;
31
32import java.io.ByteArrayInputStream;
Andrea Campanella1cd641b2015-12-07 17:28:34 -080033import java.io.IOException;
andreaeb70a942015-10-16 21:34:46 -070034import java.nio.charset.StandardCharsets;
35import java.util.ArrayList;
36import java.util.List;
37
Andrei Mihaescuac542ca2017-03-26 21:36:25 +030038import static org.onosproject.netconf.TargetConfig.RUNNING;
andreaeb70a942015-10-16 21:34:46 -070039import 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 */
45public class NetconfControllerConfig extends AbstractHandlerBehaviour
46 implements ControllerConfig {
47
48 private final Logger log = getLogger(NetconfControllerConfig.class);
49
Andrea Campanella7e6200a2016-03-21 09:48:40 -070050
andreaeb70a942015-10-16 21:34:46 -070051 @Override
52 public List<ControllerInfo> getControllers() {
53 DriverHandler handler = handler();
54 NetconfController controller = handler.get(NetconfController.class);
Andrea Campanella7e6200a2016-03-21 09:48:40 -070055 MastershipService mastershipService = handler.get(MastershipService.class);
56 DeviceId deviceId = handler.data().deviceId();
andreaeb70a942015-10-16 21:34:46 -070057 Preconditions.checkNotNull(controller, "Netconf controller is null");
58 List<ControllerInfo> controllers = new ArrayList<>();
Andrea Campanella7e6200a2016-03-21 09:48:40 -070059 if (mastershipService.isLocalMaster(deviceId)) {
60 try {
61 String reply = controller.getNetconfDevice(deviceId).getSession().
Andrei Mihaescuac542ca2017-03-26 21:36:25 +030062 getConfig(RUNNING);
Andrea Campanella7e6200a2016-03-21 09:48:40 -070063 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 Campanella1cd641b2015-12-07 17:28:34 -080073 }
andreaeb70a942015-10-16 21:34:46 -070074 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 Campanella7e6200a2016-03-21 09:48:40 -070083 MastershipService mastershipService = handler.get(MastershipService.class);
84 if (mastershipService.isLocalMaster(deviceId)) {
Andrea Campanella1cd641b2015-12-07 17:28:34 -080085 try {
Andrea Campanella7e6200a2016-03-21 09:48:40 -070086 NetconfDevice device = controller.getNetconfDevice(deviceId);
87 String config = null;
88
89 try {
Andrei Mihaescuac542ca2017-03-26 21:36:25 +030090 String reply = device.getSession().getConfig(RUNNING);
Andrea Campanella7e6200a2016-03-21 09:48:40 -070091 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 Milkeyfd4f8d32018-01-17 15:24:52 -0800100 return;
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700101 }
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 Campanella1cd641b2015-12-07 17:28:34 -0800106 } catch (IOException e) {
107 log.error("Cannot comunicate to device {} , exception {}", deviceId, e.getMessage());
108 }
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700109 } else {
110 log.warn("I'm not master for {} please use master, {} to execute command",
111 deviceId,
112 mastershipService.getMasterFor(deviceId));
andreaeb70a942015-10-16 21:34:46 -0700113 }
andreaeb70a942015-10-16 21:34:46 -0700114 }
115
116 //TODO maybe put method getNetconfClientService like in ovsdb if we need it
117
118}
119
120