blob: 2534ca9adea671e87d03f6a3d614a3c1ed274a77 [file] [log] [blame]
Andreas Papazoisc93117d2016-01-13 15:19:27 +02001/*
2 * Copyright 2016 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.drivers.netconf;
18
19import org.onosproject.net.behaviour.ConfigSetter;
20import org.onosproject.net.driver.AbstractHandlerBehaviour;
21import com.google.common.base.Preconditions;
22import org.onosproject.net.DeviceId;
23import org.onosproject.net.driver.DriverHandler;
24import org.onosproject.netconf.NetconfController;
25import org.slf4j.Logger;
26
27import java.io.IOException;
28import java.nio.file.Files;
29import java.nio.file.Paths;
30
31import static org.slf4j.LoggerFactory.getLogger;
32
33/**
34 * Sets the configuration included in the specified file to the specified
35 * device. It returns the response of the device.
36 * Temporary Developer tool, NOT TO BE USED in production or as example for
37 * future drivers/behaviors.
38 */
39//FIXME this should eventually be removed.
40
41public class NetconfConfigSetter extends AbstractHandlerBehaviour
42 implements ConfigSetter {
43
44 private final Logger log = getLogger(getClass());
45
46 private static final String UNABLE_TO_READ_FILE =
47 "Configuration cannot be retrieved from file";
48 private static final String UNABLE_TO_SET_CONFIG =
49 "Configuration cannot be set";
50
51 @Override
52 public String setConfiguration(String filePath) {
53 DriverHandler handler = handler();
54 NetconfController controller = handler.get(NetconfController.class);
55 DeviceId deviceId = handler.data().deviceId();
56 Preconditions.checkNotNull(controller, "Netconf controller is null");
57
58 String request;
59 try {
60 request = new String(Files.readAllBytes(Paths.get(filePath)));
61 } catch (IOException e) {
62 log.error("Cannot read configuration file", e);
63 return UNABLE_TO_READ_FILE;
64 }
65
66 try {
67 return controller.getDevicesMap()
68 .get(deviceId)
69 .getSession()
70 .requestSync(request);
71 } catch (IOException e) {
72 log.error("Configuration could not be set", e);
73 }
74 return UNABLE_TO_SET_CONFIG;
75 }
76
77}