blob: c6a41349cdbc1be4e8433e96c2d3a87b224303be [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;
Yuta HIGUCHIdd7c3f82017-09-03 14:18:01 -070027import org.onosproject.netconf.DatastoreId;
andreaeb70a942015-10-16 21:34:46 -070028import org.onosproject.netconf.NetconfController;
29import org.onosproject.netconf.NetconfDevice;
Yuta HIGUCHI234eaf32017-09-06 13:45:05 -070030import org.onosproject.netconf.NetconfException;
andreaeb70a942015-10-16 21:34:46 -070031import org.slf4j.Logger;
32
33import java.io.ByteArrayInputStream;
34import java.nio.charset.StandardCharsets;
35import java.util.ArrayList;
36import java.util.List;
37
38import 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 */
44public class NetconfControllerConfig extends AbstractHandlerBehaviour
45 implements ControllerConfig {
46
47 private final Logger log = getLogger(NetconfControllerConfig.class);
48
Andrea Campanella7e6200a2016-03-21 09:48:40 -070049
andreaeb70a942015-10-16 21:34:46 -070050 @Override
51 public List<ControllerInfo> getControllers() {
52 DriverHandler handler = handler();
53 NetconfController controller = handler.get(NetconfController.class);
Andrea Campanella7e6200a2016-03-21 09:48:40 -070054 MastershipService mastershipService = handler.get(MastershipService.class);
55 DeviceId deviceId = handler.data().deviceId();
andreaeb70a942015-10-16 21:34:46 -070056 Preconditions.checkNotNull(controller, "Netconf controller is null");
57 List<ControllerInfo> controllers = new ArrayList<>();
Andrea Campanella7e6200a2016-03-21 09:48:40 -070058 if (mastershipService.isLocalMaster(deviceId)) {
59 try {
60 String reply = controller.getNetconfDevice(deviceId).getSession().
Yuta HIGUCHIdd7c3f82017-09-03 14:18:01 -070061 getConfig(DatastoreId.RUNNING);
Andrea Campanella7e6200a2016-03-21 09:48:40 -070062 log.debug("Reply XML {}", reply);
63 controllers.addAll(XmlConfigParser.parseStreamControllers(XmlConfigParser.
64 loadXml(new ByteArrayInputStream(reply.getBytes(StandardCharsets.UTF_8)))));
Yuta HIGUCHI234eaf32017-09-06 13:45:05 -070065 } catch (NetconfException e) {
Andrea Campanella7e6200a2016-03-21 09:48:40 -070066 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 Campanella1cd641b2015-12-07 17:28:34 -080072 }
andreaeb70a942015-10-16 21:34:46 -070073 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 Campanella7e6200a2016-03-21 09:48:40 -070082 MastershipService mastershipService = handler.get(MastershipService.class);
83 if (mastershipService.isLocalMaster(deviceId)) {
Andrea Campanella1cd641b2015-12-07 17:28:34 -080084 try {
Andrea Campanella7e6200a2016-03-21 09:48:40 -070085 NetconfDevice device = controller.getNetconfDevice(deviceId);
86 String config = null;
87
88 try {
Yuta HIGUCHIdd7c3f82017-09-03 14:18:01 -070089 String reply = device.getSession().getConfig(DatastoreId.RUNNING);
Andrea Campanella7e6200a2016-03-21 09:48:40 -070090 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 HIGUCHI234eaf32017-09-06 13:45:05 -070097 } catch (NetconfException e) {
Andrea Campanella7e6200a2016-03-21 09:48:40 -070098 log.error("Cannot comunicate to device {} , exception {}", deviceId, e.getMessage());
Ray Milkey74e59132018-01-17 15:24:52 -080099 return;
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700100 }
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 HIGUCHI234eaf32017-09-06 13:45:05 -0700105 } catch (NetconfException e) {
Andrea Campanella1cd641b2015-12-07 17:28:34 -0800106 log.error("Cannot comunicate to device {} , exception {}", deviceId, e.getMessage());
107 }
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700108 } else {
109 log.warn("I'm not master for {} please use master, {} to execute command",
110 deviceId,
111 mastershipService.getMasterFor(deviceId));
andreaeb70a942015-10-16 21:34:46 -0700112 }
andreaeb70a942015-10-16 21:34:46 -0700113 }
114
115 //TODO maybe put method getNetconfClientService like in ovsdb if we need it
116
117}
118
119