blob: 1a6f31e13f1984d955e9ecf8d4c1029bfa17c131 [file] [log] [blame]
andreaeb70a942015-10-16 21:34:46 -07001/*
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 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;
andreaeb70a942015-10-16 21:34:46 -070021import org.onosproject.net.DeviceId;
22import org.onosproject.net.behaviour.ControllerConfig;
23import org.onosproject.net.behaviour.ControllerInfo;
24import org.onosproject.net.driver.AbstractHandlerBehaviour;
25import org.onosproject.net.driver.DriverHandler;
26import org.onosproject.netconf.NetconfController;
27import org.onosproject.netconf.NetconfDevice;
28import org.slf4j.Logger;
29
30import java.io.ByteArrayInputStream;
Andrea Campanella1cd641b2015-12-07 17:28:34 -080031import java.io.IOException;
andreaeb70a942015-10-16 21:34:46 -070032import java.nio.charset.StandardCharsets;
33import java.util.ArrayList;
34import java.util.List;
35
36import static org.slf4j.LoggerFactory.getLogger;
37
38/**
39 * Implementation of controller config which allows to get and set controllers
40 * through the Netconf protocol.
41 */
42public class NetconfControllerConfig extends AbstractHandlerBehaviour
43 implements ControllerConfig {
44
45 private final Logger log = getLogger(NetconfControllerConfig.class);
46
47 @Override
48 public List<ControllerInfo> getControllers() {
49 DriverHandler handler = handler();
50 NetconfController controller = handler.get(NetconfController.class);
51 DeviceId ofDeviceId = handler.data().deviceId();
52 Preconditions.checkNotNull(controller, "Netconf controller is null");
53 List<ControllerInfo> controllers = new ArrayList<>();
Andrea Campanella1cd641b2015-12-07 17:28:34 -080054 try {
Andrea Campanellab029b9e2016-01-29 11:05:36 -080055 String reply = controller.getDevicesMap().get(ofDeviceId).getSession().
56 getConfig("running");
57 log.debug("Reply XML {}", reply);
Andrea Campanella1cd641b2015-12-07 17:28:34 -080058 controllers.addAll(XmlConfigParser.parseStreamControllers(XmlConfigParser.
Andrea Campanellab029b9e2016-01-29 11:05:36 -080059 loadXml(new ByteArrayInputStream(reply.getBytes(StandardCharsets.UTF_8)))));
Andrea Campanella1cd641b2015-12-07 17:28:34 -080060 } catch (IOException e) {
61 log.error("Cannot comunicate to device {} ", ofDeviceId);
62 }
andreaeb70a942015-10-16 21:34:46 -070063 return controllers;
64 }
65
66 @Override
67 public void setControllers(List<ControllerInfo> controllers) {
68 DriverHandler handler = handler();
69 NetconfController controller = handler.get(NetconfController.class);
70 DeviceId deviceId = handler.data().deviceId();
71 Preconditions.checkNotNull(controller, "Netconf controller is null");
72 try {
73 NetconfDevice device = controller.getNetconfDevice(deviceId);
Andrea Campanella1cd641b2015-12-07 17:28:34 -080074 String config = null;
Andrea Campanellab029b9e2016-01-29 11:05:36 -080075
Andrea Campanella1cd641b2015-12-07 17:28:34 -080076 try {
Andrea Campanellab029b9e2016-01-29 11:05:36 -080077 String reply = device.getSession().getConfig("running");
78 log.info("reply XML {}", reply);
Andrea Campanella1cd641b2015-12-07 17:28:34 -080079 config = XmlConfigParser.createControllersConfig(
80 XmlConfigParser.loadXml(getClass().getResourceAsStream("controllers.xml")),
81 XmlConfigParser.loadXml(
Andrea Campanellab029b9e2016-01-29 11:05:36 -080082 new ByteArrayInputStream(reply.getBytes(StandardCharsets.UTF_8))),
Andrea Campanella1cd641b2015-12-07 17:28:34 -080083 "running", "merge", "create", controllers
84 );
85 } catch (IOException e) {
86 log.error("Cannot comunicate to device {} , exception {}", deviceId, e.getMessage());
87 }
andreaeb70a942015-10-16 21:34:46 -070088 device.getSession().editConfig(config.substring(config.indexOf("-->") + 3));
89 } catch (NullPointerException e) {
90 log.warn("No NETCONF device with requested parameters " + e);
91 throw new NullPointerException("No NETCONF device with requested parameters " + e);
Andrea Campanella1cd641b2015-12-07 17:28:34 -080092 } catch (IOException e) {
93 log.error("Cannot comunicate to device {} , exception {}", deviceId, e.getMessage());
andreaeb70a942015-10-16 21:34:46 -070094 }
95
96 }
97
98 //TODO maybe put method getNetconfClientService like in ovsdb if we need it
99
100}
101
102