blob: 1805f53fe2fb0d5bd3d109cffe7c45002fe07a4c [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
17package org.onosproject.driver.netconf;
18
19import com.google.common.base.Preconditions;
20import org.onosproject.net.DeviceId;
21import org.onosproject.net.behaviour.ControllerConfig;
22import org.onosproject.net.behaviour.ControllerInfo;
23import org.onosproject.net.driver.AbstractHandlerBehaviour;
24import org.onosproject.net.driver.DriverHandler;
25import org.onosproject.netconf.NetconfController;
26import org.onosproject.netconf.NetconfDevice;
27import org.slf4j.Logger;
28
29import java.io.ByteArrayInputStream;
Andrea Campanella1cd641b2015-12-07 17:28:34 -080030import java.io.IOException;
andreaeb70a942015-10-16 21:34:46 -070031import java.nio.charset.StandardCharsets;
32import java.util.ArrayList;
33import java.util.List;
34
35import static org.slf4j.LoggerFactory.getLogger;
36
37/**
38 * Implementation of controller config which allows to get and set controllers
39 * through the Netconf protocol.
40 */
41public class NetconfControllerConfig extends AbstractHandlerBehaviour
42 implements ControllerConfig {
43
44 private final Logger log = getLogger(NetconfControllerConfig.class);
45
46 @Override
47 public List<ControllerInfo> getControllers() {
48 DriverHandler handler = handler();
49 NetconfController controller = handler.get(NetconfController.class);
50 DeviceId ofDeviceId = handler.data().deviceId();
51 Preconditions.checkNotNull(controller, "Netconf controller is null");
52 List<ControllerInfo> controllers = new ArrayList<>();
Andrea Campanella1cd641b2015-12-07 17:28:34 -080053 try {
54 controllers.addAll(XmlConfigParser.parseStreamControllers(XmlConfigParser.
55 loadXml(new ByteArrayInputStream(controller.
56 getDevicesMap().get(ofDeviceId).getSession().
57 getConfig("running").getBytes(StandardCharsets.UTF_8)))));
58 } catch (IOException e) {
59 log.error("Cannot comunicate to device {} ", ofDeviceId);
60 }
andreaeb70a942015-10-16 21:34:46 -070061 return controllers;
62 }
63
64 @Override
65 public void setControllers(List<ControllerInfo> controllers) {
66 DriverHandler handler = handler();
67 NetconfController controller = handler.get(NetconfController.class);
68 DeviceId deviceId = handler.data().deviceId();
69 Preconditions.checkNotNull(controller, "Netconf controller is null");
70 try {
71 NetconfDevice device = controller.getNetconfDevice(deviceId);
72 log.warn("provider map {}", controller.getDevicesMap());
Andrea Campanella1cd641b2015-12-07 17:28:34 -080073 String config = null;
74 try {
75 config = XmlConfigParser.createControllersConfig(
76 XmlConfigParser.loadXml(getClass().getResourceAsStream("controllers.xml")),
77 XmlConfigParser.loadXml(
78 new ByteArrayInputStream(device.getSession()
79 .getConfig("running")
80 .getBytes(
81 StandardCharsets.UTF_8))),
82 "running", "merge", "create", controllers
83 );
84 } catch (IOException e) {
85 log.error("Cannot comunicate to device {} , exception {}", deviceId, e.getMessage());
86 }
andreaeb70a942015-10-16 21:34:46 -070087 device.getSession().editConfig(config.substring(config.indexOf("-->") + 3));
88 } catch (NullPointerException e) {
89 log.warn("No NETCONF device with requested parameters " + e);
90 throw new NullPointerException("No NETCONF device with requested parameters " + e);
Andrea Campanella1cd641b2015-12-07 17:28:34 -080091 } catch (IOException e) {
92 log.error("Cannot comunicate to device {} , exception {}", deviceId, e.getMessage());
andreaeb70a942015-10-16 21:34:46 -070093 }
94
95 }
96
97 //TODO maybe put method getNetconfClientService like in ovsdb if we need it
98
99}
100
101