Andreas Papazois
Committed by Gerrit Code Review

[GEANT] Adding driver/behavior for setting configuration on NETCONF devices.

Change-Id: Iced3ba42c5b1d43d64c06fd19234017dc8a21278
1 +/*
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 +package org.onosproject.cli.net;
17 +
18 +import org.apache.karaf.shell.commands.Argument;
19 +import org.apache.karaf.shell.commands.Command;
20 +import org.onosproject.cli.AbstractShellCommand;
21 +import org.onosproject.net.DeviceId;
22 +import org.onosproject.net.behaviour.ConfigSetter;
23 +import org.onosproject.net.driver.DriverHandler;
24 +import org.onosproject.net.driver.DriverService;
25 +
26 +import static com.google.common.base.Preconditions.checkNotNull;
27 +
28 +/**
29 + * Command that sets the configuration included in the specified file to the
30 + * specified device. It prints the response of the device.
31 + *
32 + * This is a temporary development tool for use until yang integration is complete.
33 + * This uses a not properly specified behavior. DO NOT USE AS AN EXAMPLE.
34 + */
35 +//Temporary Developer tool, NOT TO BE USED in production or as example for
36 +// future commands.
37 +//FIXME this should eventually be removed.
38 +
39 +@Command(scope = "onos", name = "device-setconfiguration",
40 + description = "Sets the configuration of the specified file to the " +
41 + "specified device.")
42 +public class DeviceConfigSetterCommand extends AbstractShellCommand {
43 +
44 + @Argument(index = 0, name = "uri", description = "Device ID",
45 + required = true, multiValued = false)
46 + private String uri = null;
47 + @Argument(index = 1, name = "cfgFile", description = "Configuration file",
48 + required = true, multiValued = false)
49 + private String cfgFile = null;
50 + private DeviceId deviceId;
51 +
52 + @Override
53 + protected void execute() {
54 + DriverService service = get(DriverService.class);
55 + deviceId = DeviceId.deviceId(uri);
56 + DriverHandler h = service.createHandler(deviceId);
57 + ConfigSetter config = h.behaviour(ConfigSetter.class);
58 + checkNotNull(cfgFile, "Configuration file cannot be null");
59 + print(config.setConfiguration(cfgFile));
60 + }
61 +
62 +}
...@@ -127,6 +127,12 @@ ...@@ -127,6 +127,12 @@
127 </completers> 127 </completers>
128 </command> 128 </command>
129 <command> 129 <command>
130 + <action class="org.onosproject.cli.net.DeviceConfigSetterCommand"/>
131 + <completers>
132 + <ref component-id="deviceIdCompleter"/>
133 + </completers>
134 + </command>
135 + <command>
130 <action class="org.onosproject.cli.net.DevicePortGetterCommand"/> 136 <action class="org.onosproject.cli.net.DevicePortGetterCommand"/>
131 <completers> 137 <completers>
132 <ref component-id="deviceIdCompleter"/> 138 <ref component-id="deviceIdCompleter"/>
......
1 +/*
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 +
17 +package org.onosproject.net.behaviour;
18 +
19 +import org.onosproject.net.driver.HandlerBehaviour;
20 +
21 +/**
22 + * Behaviour that sets the configuration to a device.
23 + *
24 + */
25 +//Temporary Developer tool, NOT TO BE USED in production or as example for
26 +// future drivers/behaviors.
27 +//FIXME this should eventually be removed.
28 +public interface ConfigSetter extends HandlerBehaviour {
29 +
30 + /**
31 + * Sets the configuration contained in the file at the file path, returns
32 + * the response of the device.
33 + * @param filePath the path to the configuration file.
34 + * @return string response received from the device.
35 + */
36 + String setConfiguration(String filePath);
37 +}
1 +/*
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 +
17 +package org.onosproject.drivers.netconf;
18 +
19 +import org.onosproject.net.behaviour.ConfigSetter;
20 +import org.onosproject.net.driver.AbstractHandlerBehaviour;
21 +import com.google.common.base.Preconditions;
22 +import org.onosproject.net.DeviceId;
23 +import org.onosproject.net.driver.DriverHandler;
24 +import org.onosproject.netconf.NetconfController;
25 +import org.slf4j.Logger;
26 +
27 +import java.io.IOException;
28 +import java.nio.file.Files;
29 +import java.nio.file.Paths;
30 +
31 +import 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 +
41 +public 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 +}
...@@ -22,10 +22,14 @@ ...@@ -22,10 +22,14 @@
22 impl="org.onosproject.drivers.netconf.NetconfControllerConfig"/> 22 impl="org.onosproject.drivers.netconf.NetconfControllerConfig"/>
23 <behaviour api="org.onosproject.net.behaviour.ConfigGetter" 23 <behaviour api="org.onosproject.net.behaviour.ConfigGetter"
24 impl="org.onosproject.drivers.netconf.NetconfConfigGetter"/> 24 impl="org.onosproject.drivers.netconf.NetconfConfigGetter"/>
25 + <behaviour api="org.onosproject.net.behaviour.ConfigSetter"
26 + impl="org.onosproject.drivers.netconf.NetconfConfigSetter"/>
25 </driver> 27 </driver>
26 <driver name="netconf" extends="default"> 28 <driver name="netconf" extends="default">
27 <behaviour api="org.onosproject.net.behaviour.ConfigGetter" 29 <behaviour api="org.onosproject.net.behaviour.ConfigGetter"
28 impl="org.onosproject.drivers.netconf.NetconfConfigGetter"/> 30 impl="org.onosproject.drivers.netconf.NetconfConfigGetter"/>
31 + <behaviour api="org.onosproject.net.behaviour.ConfigSetter"
32 + impl="org.onosproject.drivers.netconf.NetconfConfigSetter"/>
29 </driver> 33 </driver>
30 </drivers> 34 </drivers>
31 35
......