Hesam Rahimi

ONOS-4919: Implement RESTCONF client

Adding code to support RESTCONF protocol as one of
the supported SBI protocols of ONOS. This RESTCONF SBI extends
the current REST SBI protocl and adds some new APIs/functinalities
so that a provider can subscribe/register to an external restconf
server to receive notification stream.

Change-Id: I21bf0d0f0394cf788e066d743b3ade04735fe07e
Showing 23 changed files with 876 additions and 144 deletions
1 COMPILE_DEPS = [ 1 COMPILE_DEPS = [
2 - '//lib:CORE_DEPS',
3 '//incubator/api:onos-incubator-api', 2 '//incubator/api:onos-incubator-api',
4 '//utils/rest:onlab-rest', 3 '//utils/rest:onlab-rest',
4 + '//lib:CORE_DEPS',
5 + '//lib:jersey-client',
6 + '//lib:jersey-common',
7 + '//lib:httpclient-osgi',
8 + '//lib:httpcore-osgi',
9 + '//lib:javax.ws.rs-api',
10 + '//lib:hk2-api',
11 + '//lib:jersey-guava',
12 + '//lib:aopalliance-repackaged',
13 + '//lib:javax.inject',
5 ] 14 ]
6 15
7 osgi_jar_with_tests ( 16 osgi_jar_with_tests (
......
...@@ -28,6 +28,29 @@ ...@@ -28,6 +28,29 @@
28 <artifactId>onos-restsb-api</artifactId> 28 <artifactId>onos-restsb-api</artifactId>
29 <packaging>bundle</packaging> 29 <packaging>bundle</packaging>
30 30
31 + <dependencies>
32 + <dependency>
33 + <groupId>org.glassfish.jersey.core</groupId>
34 + <artifactId>jersey-client</artifactId>
35 + </dependency>
36 + <dependency>
37 + <groupId>org.apache.httpcomponents</groupId>
38 + <artifactId>httpclient-osgi</artifactId>
39 + <version>4.5.1</version>
40 + </dependency>
41 + <dependency>
42 + <groupId>commons-io</groupId>
43 + <artifactId>commons-io</artifactId>
44 + <version>2.4</version>
45 + </dependency>
46 + <dependency>
47 + <groupId>junit</groupId>
48 + <artifactId>junit</artifactId>
49 + <version>3.8.1</version>
50 + <scope>test</scope>
51 + </dependency>
52 + </dependencies>
53 +
31 <description>ONOS Rest southbound plugin API</description> 54 <description>ONOS Rest southbound plugin API</description>
32 55
33 56
......
1 +/*
2 + * Copyright 2016-present 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.protocol.http;
18 +
19 +import org.onlab.packet.IpAddress;
20 +import org.onosproject.net.DeviceId;
21 +import org.onosproject.protocol.rest.RestSBDevice;
22 +
23 +import java.io.InputStream;
24 +import java.util.Map;
25 +
26 +/**
27 + * Abstraction of an HTTP controller. Serves as a one stop shop for obtaining
28 + * HTTP southbound devices and (un)register listeners.
29 + */
30 +public interface HttpSBController {
31 +
32 + /**
33 + * Returns all the devices known to this controller.
34 + *
35 + * @return map of devices
36 + */
37 + Map<DeviceId, RestSBDevice> getDevices();
38 +
39 + /**
40 + * Returns a device by node identifier.
41 + *
42 + * @param deviceInfo node identifier
43 + * @return RestSBDevice rest device
44 + */
45 + RestSBDevice getDevice(DeviceId deviceInfo);
46 +
47 + /**
48 + * Returns a device by Ip and Port.
49 + *
50 + * @param ip device ip
51 + * @param port device port
52 + * @return RestSBDevice rest device
53 + */
54 + RestSBDevice getDevice(IpAddress ip, int port);
55 +
56 + /**
57 + * Adds a device to the device map.
58 + *
59 + * @param device to be added
60 + */
61 + void addDevice(RestSBDevice device);
62 +
63 + /**
64 + * Removes the device from the devices map.
65 + *
66 + * @param deviceId to be removed
67 + */
68 + void removeDevice(DeviceId deviceId);
69 +
70 + /**
71 + * Does a HTTP POST request with specified parameters to the device.
72 + *
73 + * @param device device to make the request to
74 + * @param request url of the request
75 + * @param payload payload of the request as an InputStream
76 + * @param mediaType type of content in the payload i.e. application/json
77 + * @return true if operation returned 200, 201, 202, false otherwise
78 + */
79 + boolean post(DeviceId device, String request, InputStream payload, String mediaType);
80 +
81 + /**
82 + * Does a HTTP POST request with specified parameters to the device.
83 + *
84 + * @param <T> post return type
85 + * @param device device to make the request to
86 + * @param request url of the request
87 + * @param payload payload of the request as an InputStream
88 + * @param mediaType type of content in the payload i.e. application/json
89 + * @param responseClass the type of response object we are interested in,
90 + * such as String, InputStream.
91 + * @return Object of type requested via responseClass.
92 + */
93 + <T> T post(DeviceId device, String request, InputStream payload,
94 + String mediaType, Class<T> responseClass);
95 +
96 + /**
97 + * Does a HTTP PUT request with specified parameters to the device.
98 + *
99 + * @param device device to make the request to
100 + * @param request resource path of the request
101 + * @param payload payload of the request as an InputStream
102 + * @param mediaType type of content in the payload i.e. application/json
103 + * @return true if operation returned 200, 201, 202, false otherwise
104 + */
105 + boolean put(DeviceId device, String request, InputStream payload, String mediaType);
106 +
107 + /**
108 + * Does a HTTP GET request with specified parameters to the device.
109 + *
110 + * @param device device to make the request to
111 + * @param request url of the request
112 + * @param mediaType format to retrieve the content in
113 + * @return an inputstream of data from the reply.
114 + */
115 + InputStream get(DeviceId device, String request, String mediaType);
116 +
117 + /**
118 + * Does a HTTP PATCH request with specified parameters to the device.
119 + *
120 + * @param device device to make the request to
121 + * @param request url of the request
122 + * @param payload payload of the request as an InputStream
123 + * @param mediaType format to retrieve the content in
124 + * @return true if operation returned 200, 201, 202, false otherwise
125 + */
126 + boolean patch(DeviceId device, String request, InputStream payload, String mediaType);
127 +
128 + /**
129 + * Does a HTTP DELETE request with specified parameters to the device.
130 + *
131 + * @param device device to make the request to
132 + * @param request url of the request
133 + * @param payload payload of the request as an InputStream
134 + * @param mediaType type of content in the payload i.e. application/json
135 + * @return true if operation returned 200 false otherwise
136 + */
137 + boolean delete(DeviceId device, String request, InputStream payload, String mediaType);
138 +
139 +}
1 +/**
2 + * Copyright 2016-present 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 + * @author onos
18 + *
19 + */
20 +package org.onosproject.protocol.http.ctl;
1 +/**
2 + * Copyright 2016-present 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 + * @author onos
18 + *
19 + */
20 +package org.onosproject.protocol.http;
...@@ -16,15 +16,16 @@ ...@@ -16,15 +16,16 @@
16 16
17 package org.onosproject.protocol.rest; 17 package org.onosproject.protocol.rest;
18 18
19 -import com.google.common.base.MoreObjects; 19 +import java.net.URI;
20 -import com.google.common.base.Preconditions; 20 +import java.net.URISyntaxException;
21 +import java.util.Objects;
22 +
21 import org.apache.commons.lang3.StringUtils; 23 import org.apache.commons.lang3.StringUtils;
22 import org.onlab.packet.IpAddress; 24 import org.onlab.packet.IpAddress;
23 import org.onosproject.net.DeviceId; 25 import org.onosproject.net.DeviceId;
24 26
25 -import java.net.URI; 27 +import com.google.common.base.MoreObjects;
26 -import java.net.URISyntaxException; 28 +import com.google.common.base.Preconditions;
27 -import java.util.Objects;
28 29
29 /** 30 /**
30 * Default implementation for Rest devices. 31 * Default implementation for Rest devices.
......
...@@ -16,123 +16,11 @@ ...@@ -16,123 +16,11 @@
16 16
17 package org.onosproject.protocol.rest; 17 package org.onosproject.protocol.rest;
18 18
19 -import org.onlab.packet.IpAddress; 19 +import org.onosproject.protocol.http.HttpSBController;
20 -import org.onosproject.net.DeviceId;
21 -
22 -import java.io.InputStream;
23 -import java.util.Map;
24 20
25 /** 21 /**
26 * Abstraction of an REST controller. Serves as a one stop shop for obtaining 22 * Abstraction of an REST controller. Serves as a one stop shop for obtaining
27 * Rest southbound devices and (un)register listeners. 23 * Rest southbound devices and (un)register listeners.
28 */ 24 */
29 -public interface RestSBController { 25 +public interface RestSBController extends HttpSBController {
30 -
31 - /**
32 - * Returns all the devices known to this controller.
33 - *
34 - * @return map of devices
35 - */
36 - Map<DeviceId, RestSBDevice> getDevices();
37 -
38 - /**
39 - * Returns a device by node identifier.
40 - *
41 - * @param deviceInfo node identifier
42 - * @return RestSBDevice rest device
43 - */
44 - RestSBDevice getDevice(DeviceId deviceInfo);
45 -
46 - /**
47 - * Returns a device by Ip and Port.
48 - *
49 - * @param ip device ip
50 - * @param port device port
51 - * @return RestSBDevice rest device
52 - */
53 - RestSBDevice getDevice(IpAddress ip, int port);
54 -
55 - /**
56 - * Adds a device to the device map.
57 - *
58 - * @param device to be added
59 - */
60 - void addDevice(RestSBDevice device);
61 -
62 - /**
63 - * Removes the device from the devices map.
64 - *
65 - * @param deviceId to be removed
66 - */
67 - void removeDevice(DeviceId deviceId);
68 -
69 - /**
70 - * Does a REST POST request with specified parameters to the device.
71 - *
72 - * @param device device to make the request to
73 - * @param request url of the request
74 - * @param payload payload of the request as an InputStream
75 - * @param mediaType type of content in the payload i.e. application/json
76 - * @return true if operation returned 200, 201, 202, false otherwise
77 - */
78 - boolean post(DeviceId device, String request, InputStream payload, String mediaType);
79 -
80 - /**
81 - * Does a REST POST request with specified parameters to the device.
82 - *
83 - * @param <T> post return type
84 - * @param device device to make the request to
85 - * @param request url of the request
86 - * @param payload payload of the request as an InputStream
87 - * @param mediaType type of content in the payload i.e. application/json
88 - * @param responseClass the type of response object we are interested in,
89 - * such as String, InputStream.
90 - * @return Object of type requested via responseClass.
91 - */
92 - <T> T post(DeviceId device, String request, InputStream payload,
93 - String mediaType, Class<T> responseClass);
94 -
95 - /**
96 - * Does a REST PUT request with specified parameters to the device.
97 - *
98 - * @param device device to make the request to
99 - * @param request resource path of the request
100 - * @param payload payload of the request as an InputStream
101 - * @param mediaType type of content in the payload i.e. application/json
102 - * @return true if operation returned 200, 201, 202, false otherwise
103 - */
104 - boolean put(DeviceId device, String request, InputStream payload, String mediaType);
105 -
106 - /**
107 - * Does a REST GET request with specified parameters to the device.
108 - *
109 - * @param device device to make the request to
110 - * @param request url of the request
111 - * @param mediaType format to retrieve the content in
112 - * @return an inputstream of data from the reply.
113 - */
114 - InputStream get(DeviceId device, String request, String mediaType);
115 -
116 - /**
117 - * Does a REST PATCH request with specified parameters to the device.
118 - *
119 - * @param device device to make the request to
120 - * @param request url of the request
121 - * @param payload payload of the request as an InputStream
122 - * @param mediaType format to retrieve the content in
123 - * @return true if operation returned 200, 201, 202, false otherwise
124 - */
125 - boolean patch(DeviceId device, String request, InputStream payload, String mediaType);
126 -
127 - /**
128 - * Does a REST DELETE request with specified parameters to the device.
129 - *
130 - * @param device device to make the request to
131 - * @param request url of the request
132 - * @param payload payload of the request as an InputStream
133 - * @param mediaType type of content in the payload i.e. application/json
134 - * @return true if operation returned 200 false otherwise
135 - */
136 - boolean delete(DeviceId device, String request, InputStream payload, String mediaType);
137 -
138 } 26 }
......
...@@ -34,32 +34,10 @@ ...@@ -34,32 +34,10 @@
34 <artifactId>org.apache.felix.scr.annotations</artifactId> 34 <artifactId>org.apache.felix.scr.annotations</artifactId>
35 </dependency> 35 </dependency>
36 <dependency> 36 <dependency>
37 - <groupId>org.osgi</groupId>
38 - <artifactId>org.osgi.compendium</artifactId>
39 - </dependency>
40 - <dependency>
41 <groupId>org.onosproject</groupId> 37 <groupId>org.onosproject</groupId>
42 <artifactId>onos-restsb-api</artifactId> 38 <artifactId>onos-restsb-api</artifactId>
43 <version>${project.version}</version> 39 <version>${project.version}</version>
44 - </dependency> 40 + <type>bundle</type>
45 - <dependency>
46 - <groupId>org.glassfish.jersey.core</groupId>
47 - <artifactId>jersey-client</artifactId>
48 - </dependency>
49 - <dependency>
50 - <groupId>org.apache.httpcomponents</groupId>
51 - <artifactId>httpclient-osgi</artifactId>
52 - <version>4.5.1</version>
53 - </dependency>
54 - <dependency>
55 - <groupId>org.apache.httpcomponents</groupId>
56 - <artifactId>httpcore-osgi</artifactId>
57 - <version>4.4.4</version>
58 - </dependency>
59 - <dependency>
60 - <groupId>commons-io</groupId>
61 - <artifactId>commons-io</artifactId>
62 - <version>2.4</version>
63 </dependency> 41 </dependency>
64 </dependencies> 42 </dependencies>
65 43
......
...@@ -39,4 +39,4 @@ ...@@ -39,4 +39,4 @@
39 </dependencies> 39 </dependencies>
40 40
41 41
42 -</project>
...\ No newline at end of file ...\ No newline at end of file
42 +</project>
......
1 +COMPILE_DEPS = [
2 + '//lib:CORE_DEPS',
3 + '//incubator/api:onos-incubator-api',
4 + '//utils/rest:onlab-rest',
5 + '//protocols/rest/api:onos-protocols-rest-api',
6 +]
7 +
8 +osgi_jar_with_tests (
9 + deps = COMPILE_DEPS,
10 +)
1 +<?xml version="1.0" encoding="UTF-8"?>
2 +<!--
3 + ~ Copyright 2016-present Open Networking Laboratory
4 + ~
5 + ~ Licensed under the Apache License, Version 2.0 (the "License");
6 + ~ you may not use this file except in compliance with the License.
7 + ~ You may obtain a copy of the License at
8 + ~
9 + ~ http://www.apache.org/licenses/LICENSE-2.0
10 + ~
11 + ~ Unless required by applicable law or agreed to in writing, software
12 + ~ distributed under the License is distributed on an "AS IS" BASIS,
13 + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 + ~ See the License for the specific language governing permissions and
15 + ~ limitations under the License.
16 + -->
17 +
18 +<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
19 + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
20 + <modelVersion>4.0.0</modelVersion>
21 + <parent>
22 + <groupId>org.onosproject</groupId>
23 + <artifactId>onos-restconf-client</artifactId>
24 + <version>1.8.0-SNAPSHOT</version>
25 + <relativePath>../pom.xml</relativePath>
26 + </parent>
27 + <artifactId>onos-restconf-client-api</artifactId>
28 + <packaging>bundle</packaging>
29 + <description>ONOS RESTCONF southbound plugin API</description>
30 + <dependencies>
31 + <dependency>
32 + <groupId>org.onosproject</groupId>
33 + <artifactId>onos-restsb-api</artifactId>
34 + <version>${project.version}</version>
35 + <type>bundle</type>
36 + </dependency>
37 + </dependencies>
38 +</project>
1 +/*
2 + * Copyright 2016-present 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.protocol.restconf;
17 +
18 +import org.onosproject.net.DeviceId;
19 +
20 +/**
21 + * Notifies providers about incoming RESTCONF notification events.
22 + */
23 +public interface RestConfNotificationEventListener {
24 +
25 + /**
26 + * Handles the notification event.
27 + *
28 + * @param <T>
29 + *
30 + * @param deviceId of the restconf device
31 + * @param eventJsonString the json string representation of the event
32 + */
33 + <T> void handleNotificationEvent(DeviceId deviceId, T eventJsonString);
34 +
35 +}
1 +/*
2 + * Copyright 2016-present 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.protocol.restconf;
17 +
18 +import org.onosproject.net.DeviceId;
19 +import org.onosproject.protocol.http.HttpSBController;
20 +
21 +/**
22 + * Abstraction of a RESTCONF controller. Serves as a one stop shop for obtaining
23 + * RESTCONF southbound devices and (un)register listeners.
24 + */
25 +public interface RestConfSBController extends HttpSBController {
26 +
27 + /**
28 + * This method is to be called by whoever is interested to receive
29 + * Notifications from a specific device. It does a REST GET request
30 + * with specified parameters to the device, and calls the provided
31 + * callBackListener upon receiving notifications to notify the requester
32 + * about notifications.
33 + *
34 + *
35 + * @param device device to make the request to
36 + * @param request url of the request
37 + * @param mediaType format to retrieve the content in
38 + * @param callBackListener method to call when notifications arrives
39 + */
40 + void enableNotifications(DeviceId device, String request, String mediaType,
41 + RestConfNotificationEventListener callBackListener);
42 +
43 + /**
44 + * Register a listener for notification events that occur to restconf
45 + * devices.
46 + *
47 + * @param deviceId the deviceId
48 + * @param listener the listener to notify
49 + */
50 + void addNotificationListener(DeviceId deviceId,
51 + RestConfNotificationEventListener listener);
52 +
53 + /**
54 + * Unregister the listener for the device.
55 + *
56 + * @param deviceId the deviceId
57 + */
58 + void removeNotificationListener(DeviceId deviceId);
59 +}
1 +/*
2 + * Copyright 2016-present 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 +/**
18 + * RESTCONF southbound protocols libraries.
19 + */
20 +package org.onosproject.protocol.restconf;
1 +COMPILE_DEPS = [
2 + '//lib:CORE_DEPS',
3 + '//lib:jersey-client',
4 + '//lib:jersey-common',
5 + '//lib:httpclient-osgi',
6 + '//lib:httpcore-osgi',
7 + '//lib:javax.ws.rs-api',
8 + '//lib:hk2-api',
9 + '//lib:jersey-guava',
10 + '//lib:aopalliance-repackaged',
11 + '//lib:javax.inject',
12 + '//protocols/restconf/client/api:onos-protocols-restconf-client-api',
13 + '//protocols/rest/api:onos-protocols-rest-api',
14 +]
15 +
16 +osgi_jar_with_tests (
17 + deps = COMPILE_DEPS,
18 +)
19 +
1 +<?xml version="1.0" encoding="UTF-8"?>
2 +<!--
3 + ~ Copyright 2016-present Open Networking Laboratory
4 + ~
5 + ~ Licensed under the Apache License, Version 2.0 (the "License");
6 + ~ you may not use this file except in compliance with the License.
7 + ~ You may obtain a copy of the License at
8 + ~
9 + ~ http://www.apache.org/licenses/LICENSE-2.0
10 + ~
11 + ~ Unless required by applicable law or agreed to in writing, software
12 + ~ distributed under the License is distributed on an "AS IS" BASIS,
13 + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 + ~ See the License for the specific language governing permissions and
15 + ~ limitations under the License.
16 + -->
17 +
18 +<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
19 + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
20 + <modelVersion>4.0.0</modelVersion>
21 + <parent>
22 + <groupId>org.onosproject</groupId>
23 + <artifactId>onos-restconf-client</artifactId>
24 + <version>1.8.0-SNAPSHOT</version>
25 + <relativePath>../pom.xml</relativePath>
26 + </parent>
27 + <artifactId>onos-restconf-client-ctl</artifactId>
28 +
29 + <packaging>bundle</packaging>
30 +
31 + <dependencies>
32 + <dependency>
33 + <groupId>org.apache.felix</groupId>
34 + <artifactId>org.apache.felix.scr.annotations</artifactId>
35 + </dependency>
36 + <dependency>
37 + <groupId>org.osgi</groupId>
38 + <artifactId>org.osgi.compendium</artifactId>
39 + </dependency>
40 + <dependency>
41 + <groupId>org.onosproject</groupId>
42 + <artifactId>onos-restconf-client-api</artifactId>
43 + <version>${project.version}</version>
44 + </dependency>
45 + <dependency>
46 + <groupId>org.glassfish.jersey.core</groupId>
47 + <artifactId>jersey-client</artifactId>
48 + </dependency>
49 + <dependency>
50 + <groupId>org.apache.httpcomponents</groupId>
51 + <artifactId>httpclient-osgi</artifactId>
52 + <version>4.5.1</version>
53 + </dependency>
54 + <dependency>
55 + <groupId>org.apache.httpcomponents</groupId>
56 + <artifactId>httpcore-osgi</artifactId>
57 + <version>4.4.4</version>
58 + </dependency>
59 + <dependency>
60 + <groupId>commons-io</groupId>
61 + <artifactId>commons-io</artifactId>
62 + <version>2.4</version>
63 + </dependency>
64 + <dependency>
65 + <groupId>org.onosproject</groupId>
66 + <artifactId>onos-restsb-api</artifactId>
67 + <version>${project.version}</version>
68 + <type>bundle</type>
69 + </dependency>
70 + </dependencies>
71 +
72 + <build>
73 + <plugins>
74 + <plugin>
75 + <groupId>org.apache.felix</groupId>
76 + <artifactId>maven-scr-plugin</artifactId>
77 + </plugin>
78 + <plugin>
79 + <groupId>org.apache.felix</groupId>
80 + <artifactId>maven-bundle-plugin</artifactId>
81 + </plugin>
82 + </plugins>
83 + </build>
84 +
85 +</project>
1 +/*
2 + * Copyright 2016-present 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.protocol.restconf.ctl;
17 +
18 +import java.io.InputStream;
19 +import java.util.Map;
20 +import java.util.concurrent.ConcurrentHashMap;
21 +import java.util.concurrent.ExecutorService;
22 +import java.util.concurrent.Executors;
23 +
24 +import javax.ws.rs.client.WebTarget;
25 +import javax.ws.rs.core.GenericType;
26 +import javax.ws.rs.core.Response;
27 +
28 +import org.apache.felix.scr.annotations.Activate;
29 +import org.apache.felix.scr.annotations.Component;
30 +import org.apache.felix.scr.annotations.Deactivate;
31 +import org.apache.felix.scr.annotations.Service;
32 +import org.glassfish.jersey.client.ChunkedInput;
33 +import org.onlab.packet.IpAddress;
34 +import org.onosproject.net.DeviceId;
35 +import org.onosproject.protocol.http.ctl.HttpSBControllerImpl;
36 +import org.onosproject.protocol.rest.RestSBDevice;
37 +import org.onosproject.protocol.restconf.RestConfNotificationEventListener;
38 +import org.onosproject.protocol.restconf.RestConfSBController;
39 +import org.slf4j.Logger;
40 +import org.slf4j.LoggerFactory;
41 +
42 +/**
43 + * The implementation of RestConfSBController.
44 + */
45 +@Component(immediate = true)
46 +@Service
47 +public class RestConfSBControllerImpl extends HttpSBControllerImpl
48 + implements RestConfSBController {
49 +
50 + private static final Logger log = LoggerFactory
51 + .getLogger(RestConfSBControllerImpl.class);
52 +
53 + // TODO: for the Ibis release when both RESTCONF server and RESTCONF client
54 + // fully support root resource discovery, ROOT_RESOURCE constant will be
55 + // removed and rather the value would get discovered dynamically.
56 + private static final String ROOT_RESOURCE = "/onos/restconf";
57 +
58 + private static final String RESOURCE_PATH_PREFIX = "/data/";
59 + private static final String NOTIFICATION_PATH_PREFIX = "/data/";
60 +
61 + private Map<DeviceId, RestConfNotificationEventListener>
62 + restconfNotificationListenerMap = new ConcurrentHashMap<>();
63 + private Map<DeviceId, GetChunksRunnable> runnableTable = new ConcurrentHashMap<>();
64 +
65 + ExecutorService executor = Executors.newCachedThreadPool();
66 +
67 + @Activate
68 + public void activate() {
69 + log.info("RESTCONF SBI Started");
70 + }
71 +
72 + @Deactivate
73 + public void deactivate() {
74 + log.info("RESTCONF SBI Stopped");
75 + executor.shutdown();
76 + this.getClientMap().clear();
77 + this.getDeviceMap().clear();
78 + }
79 +
80 + @Override
81 + public Map<DeviceId, RestSBDevice> getDevices() {
82 + log.trace("RESTCONF SBI::getDevices");
83 + return super.getDevices();
84 + }
85 +
86 + @Override
87 + public RestSBDevice getDevice(DeviceId deviceInfo) {
88 + log.trace("RESTCONF SBI::getDevice with deviceId");
89 + return super.getDevice(deviceInfo);
90 + }
91 +
92 + @Override
93 + public RestSBDevice getDevice(IpAddress ip, int port) {
94 + log.trace("RESTCONF SBI::getDevice with ip and port");
95 + return super.getDevice(ip, port);
96 + }
97 +
98 + @Override
99 + public void addDevice(RestSBDevice device) {
100 + log.trace("RESTCONF SBI::addDevice");
101 + super.addDevice(device);
102 + }
103 +
104 + @Override
105 + public void removeDevice(DeviceId deviceId) {
106 + log.trace("RESTCONF SBI::removeDevice");
107 + super.removeDevice(deviceId);
108 + }
109 +
110 + @Override
111 + public boolean post(DeviceId device, String request, InputStream payload,
112 + String mediaType) {
113 + request = discoverRootResource(device) + RESOURCE_PATH_PREFIX
114 + + request;
115 + return super.post(device, request, payload, mediaType);
116 + }
117 +
118 + @Override
119 + public <T> T post(DeviceId device, String request, InputStream payload,
120 + String mediaType, Class<T> responseClass) {
121 + request = discoverRootResource(device) + RESOURCE_PATH_PREFIX
122 + + request;
123 + return super.post(device, request, payload, mediaType, responseClass);
124 + }
125 +
126 + @Override
127 + public boolean put(DeviceId device, String request, InputStream payload,
128 + String mediaType) {
129 + request = discoverRootResource(device) + RESOURCE_PATH_PREFIX
130 + + request;
131 + return super.put(device, request, payload, mediaType);
132 + }
133 +
134 + @Override
135 + public InputStream get(DeviceId device, String request, String mediaType) {
136 + request = discoverRootResource(device) + RESOURCE_PATH_PREFIX
137 + + request;
138 + return super.get(device, request, mediaType);
139 + }
140 +
141 + @Override
142 + public boolean patch(DeviceId device, String request, InputStream payload,
143 + String mediaType) {
144 + request = discoverRootResource(device) + RESOURCE_PATH_PREFIX
145 + + request;
146 + return super.patch(device, request, payload, mediaType);
147 + }
148 +
149 + @Override
150 + public boolean delete(DeviceId device, String request, InputStream payload,
151 + String mediaType) {
152 + request = discoverRootResource(device) + RESOURCE_PATH_PREFIX
153 + + request;
154 + return super.delete(device, request, payload, mediaType);
155 + }
156 +
157 + @Override
158 + public void enableNotifications(DeviceId device, String request,
159 + String mediaType,
160 + RestConfNotificationEventListener listener) {
161 +
162 + request = discoverRootResource(device) + NOTIFICATION_PATH_PREFIX
163 + + request;
164 +
165 + addNotificationListener(device, listener);
166 +
167 + GetChunksRunnable runnable = new GetChunksRunnable(request, mediaType,
168 + device);
169 + runnableTable.put(device, runnable);
170 + executor.execute(runnable);
171 + }
172 +
173 + public void stopNotifications(DeviceId device) {
174 +
175 + runnableTable.get(device).terminate();
176 + runnableTable.remove(device);
177 + removeNotificationListener(device);
178 + log.debug("Stop sending notifications for device URI: " + device.uri().toString());
179 +
180 + }
181 +
182 + public class GetChunksRunnable implements Runnable {
183 + private String request;
184 + private String mediaType;
185 + private DeviceId device;
186 +
187 + private volatile boolean running = true;
188 +
189 + public void terminate() {
190 + running = false;
191 + }
192 +
193 + /**
194 + * @param request
195 + * @param mediaType
196 + * @param device
197 + */
198 + public GetChunksRunnable(String request, String mediaType,
199 + DeviceId device) {
200 + this.request = request;
201 + this.mediaType = mediaType;
202 + this.device = device;
203 + }
204 +
205 + @Override
206 + public void run() {
207 + WebTarget wt = getWebTarget(device, request);
208 + Response clientResp = wt.request(mediaType).get();
209 + RestConfNotificationEventListener listener = restconfNotificationListenerMap
210 + .get(device);
211 + final ChunkedInput<String> chunkedInput = (ChunkedInput<String>) clientResp
212 + .readEntity(new GenericType<ChunkedInput<String>>() {
213 + });
214 +
215 + String chunk;
216 + // Note that the read() is a blocking operation and the invoking
217 + // thread is blocked until a new chunk comes. Jersey implementation
218 + // of this IO operation is in a way that it does not respond to
219 + // interrupts.
220 + while (running) {
221 + chunk = chunkedInput.read();
222 + if (chunk != null) {
223 + if (running) {
224 + listener.handleNotificationEvent(device, chunk);
225 + } else {
226 + log.trace("the requesting client is no more interested "
227 + + "to receive such notifications.");
228 + }
229 + } else {
230 + log.trace("The received notification chunk is null. do not continue any more.");
231 + break;
232 + }
233 + }
234 + log.trace("out of while loop -- end of run");
235 + }
236 + }
237 +
238 + public String discoverRootResource(DeviceId device) {
239 + // FIXME: send a GET command to the device to discover the root resource.
240 + // The plan to fix this is for the Ibis release when the RESTCONF server and
241 + // the RESTCONF client both support root resource discovery.
242 + return ROOT_RESOURCE;
243 + }
244 +
245 + @Override
246 + public void addNotificationListener(DeviceId deviceId,
247 + RestConfNotificationEventListener listener) {
248 + if (!restconfNotificationListenerMap.containsKey(deviceId)) {
249 + this.restconfNotificationListenerMap.put(deviceId, listener);
250 + }
251 + }
252 +
253 + @Override
254 + public void removeNotificationListener(DeviceId deviceId) {
255 + this.restconfNotificationListenerMap.remove(deviceId);
256 + }
257 +
258 +}
1 +/*
2 + * Copyright 2016-present 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 +/**
18 + * RESTCONF southbound protocol implementation.
19 + */
20 +package org.onosproject.protocol.restconf.ctl;
1 +/*
2 + * Copyright 2016-present 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.protocol.restconf.ctl;
18 +
19 +import static org.junit.Assert.assertEquals;
20 +import static org.junit.Assert.assertFalse;
21 +import static org.junit.Assert.assertTrue;
22 +
23 +import java.util.concurrent.ExecutorService;
24 +import java.util.concurrent.Executors;
25 +
26 +import org.junit.Before;
27 +import org.junit.Test;
28 +import org.onlab.packet.IpAddress;
29 +import org.onosproject.protocol.rest.DefaultRestSBDevice;
30 +import org.onosproject.protocol.rest.RestSBDevice;
31 +
32 +/**
33 + * Basic testing for RestSBController.
34 + */
35 +public class RestConfSBControllerImplTest {
36 +
37 + RestConfSBControllerImpl restConfController;
38 +
39 + RestSBDevice device3;
40 +
41 + ExecutorService executor = Executors.newSingleThreadExecutor();
42 +
43 + @Before
44 + public void setUp() {
45 + restConfController = new RestConfSBControllerImpl();
46 + restConfController.activate();
47 + device3 = new DefaultRestSBDevice(IpAddress.valueOf("127.0.0.1"), 8181,
48 + "", "", "http", null, true);
49 + restConfController.addDevice(device3);
50 +
51 + }
52 +
53 + @Test
54 + public void basics() {
55 + assertTrue("Device3 non added",
56 + restConfController.getDevices().containsValue(device3));
57 + assertEquals("Device3 added but with wrong key",
58 + restConfController.getDevices().get(device3.deviceId()),
59 + device3);
60 + assertEquals("Incorrect Get Device by ID",
61 + restConfController.getDevice(device3.deviceId()), device3);
62 + assertEquals("Incorrect Get Device by IP, Port",
63 + restConfController.getDevice(device3.ip(), device3.port()),
64 + device3);
65 + restConfController.removeDevice(device3.deviceId());
66 + assertFalse("Device3 not removed",
67 + restConfController.getDevices().containsValue(device3));
68 + }
69 +}
1 +<?xml version="1.0" encoding="UTF-8"?>
2 +<!--
3 + ~ Copyright 2016-present Open Networking Laboratory
4 + ~
5 + ~ Licensed under the Apache License, Version 2.0 (the "License");
6 + ~ you may not use this file except in compliance with the License.
7 + ~ You may obtain a copy of the License at
8 + ~
9 + ~ http://www.apache.org/licenses/LICENSE-2.0
10 + ~
11 + ~ Unless required by applicable law or agreed to in writing, software
12 + ~ distributed under the License is distributed on an "AS IS" BASIS,
13 + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 + ~ See the License for the specific language governing permissions and
15 + ~ limitations under the License.
16 + -->
17 +
18 +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
19 + <modelVersion>4.0.0</modelVersion>
20 + <parent>
21 + <groupId>org.onosproject</groupId>
22 + <artifactId>onos-restconf</artifactId>
23 + <version>1.8.0-SNAPSHOT</version>
24 + <relativePath>../pom.xml</relativePath>
25 + </parent>
26 + <artifactId>onos-restconf-client</artifactId>
27 + <packaging>pom</packaging>
28 + <dependencies>
29 + <dependency>
30 + <groupId>org.onosproject</groupId>
31 + <artifactId>onos-api</artifactId>
32 + <version>${project.version}</version>
33 + </dependency>
34 + </dependencies>
35 + <modules>
36 + <module>api</module>
37 + <module>ctl</module>
38 + </modules>
39 + <description>RESTCONF Client Module</description>
40 +</project>
...@@ -28,6 +28,7 @@ ...@@ -28,6 +28,7 @@
28 28
29 <modules> 29 <modules>
30 <module>server</module> 30 <module>server</module>
31 + <module>client</module>
31 </modules> 32 </modules>
32 33
33 </project> 34 </project>
......