blob: 84043b5b44d4e8bf711cd44fe55a2e86f7b59103 [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;
30import java.nio.charset.StandardCharsets;
31import java.util.ArrayList;
32import java.util.List;
33
34import static org.slf4j.LoggerFactory.getLogger;
35
36/**
37 * Implementation of controller config which allows to get and set controllers
38 * through the Netconf protocol.
39 */
40public class NetconfControllerConfig extends AbstractHandlerBehaviour
41 implements ControllerConfig {
42
43 private final Logger log = getLogger(NetconfControllerConfig.class);
44
45 @Override
46 public List<ControllerInfo> getControllers() {
47 DriverHandler handler = handler();
48 NetconfController controller = handler.get(NetconfController.class);
49 DeviceId ofDeviceId = handler.data().deviceId();
50 Preconditions.checkNotNull(controller, "Netconf controller is null");
51 List<ControllerInfo> controllers = new ArrayList<>();
52 controllers.addAll(XmlConfigParser.parseStreamControllers(XmlConfigParser.
53 loadXml(new ByteArrayInputStream(controller.
54 getDevicesMap().get(ofDeviceId).getSession().
55 getConfig("running").getBytes(StandardCharsets.UTF_8)))));
56 return controllers;
57 }
58
59 @Override
60 public void setControllers(List<ControllerInfo> controllers) {
61 DriverHandler handler = handler();
62 NetconfController controller = handler.get(NetconfController.class);
63 DeviceId deviceId = handler.data().deviceId();
64 Preconditions.checkNotNull(controller, "Netconf controller is null");
65 try {
66 NetconfDevice device = controller.getNetconfDevice(deviceId);
67 log.warn("provider map {}", controller.getDevicesMap());
68 String config = XmlConfigParser.createControllersConfig(
69 XmlConfigParser.loadXml(getClass().getResourceAsStream("controllers.xml")),
70 XmlConfigParser.loadXml(
71 new ByteArrayInputStream(device.getSession()
72 .getConfig("running")
73 .getBytes(
74 StandardCharsets.UTF_8))),
75 "running", "merge", "create", controllers
76 );
77 device.getSession().editConfig(config.substring(config.indexOf("-->") + 3));
78 } catch (NullPointerException e) {
79 log.warn("No NETCONF device with requested parameters " + e);
80 throw new NullPointerException("No NETCONF device with requested parameters " + e);
81 }
82
83 }
84
85 //TODO maybe put method getNetconfClientService like in ovsdb if we need it
86
87}
88
89