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