blob: 4b8106976ea0e69232e5384503ecd556437253b7 [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;
Andrea Campanellad8d92db2016-01-14 16:24:41 -080020import org.onosproject.driver.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 {
55 controllers.addAll(XmlConfigParser.parseStreamControllers(XmlConfigParser.
56 loadXml(new ByteArrayInputStream(controller.
57 getDevicesMap().get(ofDeviceId).getSession().
58 getConfig("running").getBytes(StandardCharsets.UTF_8)))));
59 } catch (IOException e) {
60 log.error("Cannot comunicate to device {} ", ofDeviceId);
61 }
andreaeb70a942015-10-16 21:34:46 -070062 return controllers;
63 }
64
65 @Override
66 public void setControllers(List<ControllerInfo> controllers) {
67 DriverHandler handler = handler();
68 NetconfController controller = handler.get(NetconfController.class);
69 DeviceId deviceId = handler.data().deviceId();
70 Preconditions.checkNotNull(controller, "Netconf controller is null");
71 try {
72 NetconfDevice device = controller.getNetconfDevice(deviceId);
73 log.warn("provider map {}", controller.getDevicesMap());
Andrea Campanella1cd641b2015-12-07 17:28:34 -080074 String config = null;
75 try {
76 config = XmlConfigParser.createControllersConfig(
77 XmlConfigParser.loadXml(getClass().getResourceAsStream("controllers.xml")),
78 XmlConfigParser.loadXml(
79 new ByteArrayInputStream(device.getSession()
80 .getConfig("running")
81 .getBytes(
82 StandardCharsets.UTF_8))),
83 "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