Committed by
Gerrit Code Review
ONOS-3692 Southbound Rest provider and protocol
Change-Id: I74a5752d4fce1df88828fa6c531979ab7c30a26a t
Showing
23 changed files
with
1255 additions
and
0 deletions
... | @@ -128,6 +128,8 @@ | ... | @@ -128,6 +128,8 @@ |
128 | <behaviour api="org.onosproject.net.behaviour.Pipeliner" | 128 | <behaviour api="org.onosproject.net.behaviour.Pipeliner" |
129 | impl="org.onosproject.driver.pipeline.OltPipeline"/> | 129 | impl="org.onosproject.driver.pipeline.OltPipeline"/> |
130 | </driver> | 130 | </driver> |
131 | + <driver name="rest" manufacturer="" hwVersion="" swVersion=""> | ||
132 | + </driver> | ||
131 | <!-- The SoftRouter driver is meant to be used by any software/NPU based | 133 | <!-- The SoftRouter driver is meant to be used by any software/NPU based |
132 | ~ switch that wishes to implement a simple 2-table router. To use this | 134 | ~ switch that wishes to implement a simple 2-table router. To use this |
133 | ~ driver, configure ONOS with the dpid of the device, or extend the | 135 | ~ driver, configure ONOS with the dpid of the device, or extend the | ... | ... |
... | @@ -239,6 +239,11 @@ | ... | @@ -239,6 +239,11 @@ |
239 | <scope>test</scope> | 239 | <scope>test</scope> |
240 | </dependency> | 240 | </dependency> |
241 | <dependency> | 241 | <dependency> |
242 | + <groupId>com.sun.jersey</groupId> | ||
243 | + <artifactId>jersey-client</artifactId> | ||
244 | + <version>${jersey.version}</version> | ||
245 | + </dependency> | ||
246 | + <dependency> | ||
242 | <groupId>com.fasterxml.jackson.core</groupId> | 247 | <groupId>com.fasterxml.jackson.core</groupId> |
243 | <artifactId>jackson-databind</artifactId> | 248 | <artifactId>jackson-databind</artifactId> |
244 | <version>${fasterxml.jackson.version}</version> | 249 | <version>${fasterxml.jackson.version}</version> | ... | ... |
protocols/rest/api/pom.xml
0 → 100644
1 | +<?xml version="1.0" encoding="UTF-8"?> | ||
2 | +<!-- | ||
3 | + ~ Copyright 2016 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" | ||
19 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
20 | + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
21 | + <modelVersion>4.0.0</modelVersion> | ||
22 | + <parent> | ||
23 | + <artifactId>onos-restsb</artifactId> | ||
24 | + <groupId>org.onosproject</groupId> | ||
25 | + <version>1.5.0-SNAPSHOT</version> | ||
26 | + <relativePath>../pom.xml</relativePath> | ||
27 | + </parent> | ||
28 | + | ||
29 | + <artifactId>onos-restsb-api</artifactId> | ||
30 | + <packaging>bundle</packaging> | ||
31 | + | ||
32 | + <description>ONOS Rest southbound plugin API</description> | ||
33 | + | ||
34 | + | ||
35 | + | ||
36 | +</project> | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
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.protocol.rest; | ||
18 | + | ||
19 | +import com.google.common.base.Preconditions; | ||
20 | +import org.onlab.packet.IpAddress; | ||
21 | +import org.onosproject.net.DeviceId; | ||
22 | + | ||
23 | +import java.util.Objects; | ||
24 | + | ||
25 | +/** | ||
26 | + * Default implementation for Rest devices. | ||
27 | + */ | ||
28 | +public class DefaultRestSBDevice implements RestSBDevice { | ||
29 | + private static final String REST = "rest"; | ||
30 | + private static final String COLON = ":"; | ||
31 | + private final IpAddress ip; | ||
32 | + private final int port; | ||
33 | + private final String name; | ||
34 | + private final String password; | ||
35 | + private boolean isActive; | ||
36 | + private String protocol; | ||
37 | + | ||
38 | + public DefaultRestSBDevice(IpAddress ip, int port, String name, String password, | ||
39 | + String protocol, boolean isActive) { | ||
40 | + Preconditions.checkNotNull(ip, "IP address cannot be null"); | ||
41 | + Preconditions.checkArgument(port > 0, "Port address cannot be negative"); | ||
42 | + Preconditions.checkNotNull(protocol, "protocol address cannot be null"); | ||
43 | + this.ip = ip; | ||
44 | + this.port = port; | ||
45 | + this.name = name; | ||
46 | + this.password = password; | ||
47 | + this.isActive = isActive; | ||
48 | + this.protocol = protocol; | ||
49 | + } | ||
50 | + | ||
51 | + @Override | ||
52 | + public IpAddress ip() { | ||
53 | + return ip; | ||
54 | + } | ||
55 | + | ||
56 | + @Override | ||
57 | + public int port() { | ||
58 | + return port; | ||
59 | + } | ||
60 | + | ||
61 | + @Override | ||
62 | + public String name() { | ||
63 | + return name; | ||
64 | + } | ||
65 | + | ||
66 | + @Override | ||
67 | + public String password() { | ||
68 | + return password; | ||
69 | + } | ||
70 | + | ||
71 | + @Override | ||
72 | + public DeviceId deviceId() { | ||
73 | + return DeviceId.deviceId(REST + COLON + ip + COLON + port); | ||
74 | + } | ||
75 | + | ||
76 | + @Override | ||
77 | + public void setActive(boolean active) { | ||
78 | + isActive = active; | ||
79 | + } | ||
80 | + | ||
81 | + @Override | ||
82 | + public boolean isActive() { | ||
83 | + return isActive; | ||
84 | + } | ||
85 | + | ||
86 | + @Override | ||
87 | + public String protocol() { | ||
88 | + return protocol; | ||
89 | + } | ||
90 | + | ||
91 | + @Override | ||
92 | + public boolean equals(Object obj) { | ||
93 | + if (obj == this) { | ||
94 | + return true; | ||
95 | + } | ||
96 | + if (!(obj instanceof RestSBDevice)) { | ||
97 | + return false; | ||
98 | + } | ||
99 | + RestSBDevice device = (RestSBDevice) obj; | ||
100 | + return this.name.equals(device.name()) && this.ip.equals(device.ip()) && | ||
101 | + this.port == device.port(); | ||
102 | + | ||
103 | + } | ||
104 | + | ||
105 | + @Override | ||
106 | + public int hashCode() { | ||
107 | + return Objects.hash(ip, port); | ||
108 | + } | ||
109 | + | ||
110 | +} |
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.protocol.rest; | ||
18 | + | ||
19 | +import org.onlab.packet.IpAddress; | ||
20 | +import org.onosproject.net.DeviceId; | ||
21 | + | ||
22 | +import java.io.InputStream; | ||
23 | +import java.util.Map; | ||
24 | + | ||
25 | +/** | ||
26 | + * Abstraction of an REST controller. Serves as a one stop shop for obtaining | ||
27 | + * Rest southbound devices and (un)register listeners. | ||
28 | + */ | ||
29 | +public interface RestSBController { | ||
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 device to be removed | ||
66 | + */ | ||
67 | + void removeDevice(RestSBDevice device); | ||
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 PUT request with specified parameters to the device. | ||
82 | + * | ||
83 | + * @param device device to make the request to | ||
84 | + * @param request resource path of the request | ||
85 | + * @param payload payload of the request as an InputStream | ||
86 | + * @param mediaType type of content in the payload i.e. application/json | ||
87 | + * @return true if operation returned 200, 201, 202, false otherwise | ||
88 | + */ | ||
89 | + boolean put(DeviceId device, String request, InputStream payload, String mediaType); | ||
90 | + | ||
91 | + /** | ||
92 | + * Does a REST GET request with specified parameters to the device. | ||
93 | + * | ||
94 | + * @param device device to make the request to | ||
95 | + * @param request url of the request | ||
96 | + * @param mediaType format to retrieve the content in | ||
97 | + * @return an inputstream of data from the reply. | ||
98 | + */ | ||
99 | + InputStream get(DeviceId device, String request, String mediaType); | ||
100 | + | ||
101 | + /** | ||
102 | + * Does a REST DELETE request with specified parameters to the device. | ||
103 | + * | ||
104 | + * @param device device to make the request to | ||
105 | + * @param request url of the request | ||
106 | + * @param payload payload of the request as an InputStream | ||
107 | + * @param mediaType type of content in the payload i.e. application/json | ||
108 | + * @return true if operation returned 200 false otherwise | ||
109 | + */ | ||
110 | + boolean delete(DeviceId device, String request, InputStream payload, String mediaType); | ||
111 | + | ||
112 | +} |
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.protocol.rest; | ||
18 | + | ||
19 | +import org.onlab.packet.IpAddress; | ||
20 | +import org.onosproject.net.DeviceId; | ||
21 | + | ||
22 | +/** | ||
23 | + * Represents an abstraction of a Rest Device in ONOS. | ||
24 | + */ | ||
25 | +public interface RestSBDevice { | ||
26 | + /** | ||
27 | + * Returns the ip of this device. | ||
28 | + * | ||
29 | + * @return ip | ||
30 | + */ | ||
31 | + IpAddress ip(); | ||
32 | + | ||
33 | + /** | ||
34 | + * Returns the password of this device. | ||
35 | + * | ||
36 | + * @return port | ||
37 | + */ | ||
38 | + int port(); | ||
39 | + | ||
40 | + /** | ||
41 | + * Returns the name of this device. | ||
42 | + * | ||
43 | + * @return name | ||
44 | + */ | ||
45 | + String name(); | ||
46 | + | ||
47 | + /** | ||
48 | + * Returns the password of this device. | ||
49 | + * | ||
50 | + * @return password | ||
51 | + */ | ||
52 | + String password(); | ||
53 | + | ||
54 | + /** | ||
55 | + * Returns the ONOS deviceID for this device. | ||
56 | + * | ||
57 | + * @return DeviceId | ||
58 | + */ | ||
59 | + DeviceId deviceId(); | ||
60 | + | ||
61 | + /** | ||
62 | + * Sets or unsets the state of the device. | ||
63 | + * | ||
64 | + * @param active boolean | ||
65 | + */ | ||
66 | + void setActive(boolean active); | ||
67 | + | ||
68 | + /** | ||
69 | + * Returns the state of this device. | ||
70 | + * | ||
71 | + * @return state | ||
72 | + */ | ||
73 | + boolean isActive(); | ||
74 | + | ||
75 | + /** | ||
76 | + * Returns the protocol for the REST request, usually HTTP o HTTPS. | ||
77 | + * | ||
78 | + * @return protocol | ||
79 | + */ | ||
80 | + String protocol(); | ||
81 | + | ||
82 | +} |
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 | +/** | ||
18 | + * REST southbound protocols libraries. | ||
19 | + */ | ||
20 | +package org.onosproject.protocol.rest; |
protocols/rest/ctl/pom.xml
0 → 100644
1 | +<?xml version="1.0" encoding="UTF-8"?> | ||
2 | +<!-- | ||
3 | + ~ Copyright 2016 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" | ||
19 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
20 | + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
21 | + <parent> | ||
22 | + <artifactId>onos-restsb</artifactId> | ||
23 | + <groupId>org.onosproject</groupId> | ||
24 | + <version>1.5.0-SNAPSHOT</version> | ||
25 | + <relativePath>../pom.xml</relativePath> | ||
26 | + </parent> | ||
27 | + <modelVersion>4.0.0</modelVersion> | ||
28 | + | ||
29 | + <artifactId>onos-restsb-ctl</artifactId> | ||
30 | + <packaging>bundle</packaging> | ||
31 | + | ||
32 | + <dependencies> | ||
33 | + <dependency> | ||
34 | + <groupId>org.apache.felix</groupId> | ||
35 | + <artifactId>org.apache.felix.scr.annotations</artifactId> | ||
36 | + </dependency> | ||
37 | + <dependency> | ||
38 | + <groupId>org.osgi</groupId> | ||
39 | + <artifactId>org.osgi.compendium</artifactId> | ||
40 | + </dependency> | ||
41 | + <dependency> | ||
42 | + <groupId>org.onosproject</groupId> | ||
43 | + <artifactId>onos-restsb-api</artifactId> | ||
44 | + <version>${project.version}</version> | ||
45 | + </dependency> | ||
46 | + <dependency> | ||
47 | + <groupId>com.sun.jersey</groupId> | ||
48 | + <artifactId>jersey-client</artifactId> | ||
49 | + </dependency> | ||
50 | + <dependency> | ||
51 | + <groupId>commons-io</groupId> | ||
52 | + <artifactId>commons-io</artifactId> | ||
53 | + <version>2.4</version> | ||
54 | + </dependency> | ||
55 | + </dependencies> | ||
56 | + | ||
57 | + <build> | ||
58 | + <plugins> | ||
59 | + <plugin> | ||
60 | + <groupId>org.apache.felix</groupId> | ||
61 | + <artifactId>maven-scr-plugin</artifactId> | ||
62 | + </plugin> | ||
63 | + <plugin> | ||
64 | + <groupId>org.apache.felix</groupId> | ||
65 | + <artifactId>maven-bundle-plugin</artifactId> | ||
66 | + </plugin> | ||
67 | + </plugins> | ||
68 | + </build> | ||
69 | + | ||
70 | + | ||
71 | +</project> | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
protocols/rest/ctl/src/main/java/org/onosproject/protocol/rest/ctl/RestSBControllerImpl.java
0 → 100644
1 | +/* | ||
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 | + | ||
17 | +package org.onosproject.protocol.rest.ctl; | ||
18 | + | ||
19 | +import com.sun.jersey.api.client.Client; | ||
20 | +import com.sun.jersey.api.client.ClientResponse; | ||
21 | +import com.sun.jersey.api.client.WebResource; | ||
22 | +import org.apache.commons.io.IOUtils; | ||
23 | +import org.apache.felix.scr.annotations.Activate; | ||
24 | +import org.apache.felix.scr.annotations.Component; | ||
25 | +import org.apache.felix.scr.annotations.Deactivate; | ||
26 | +import org.apache.felix.scr.annotations.Service; | ||
27 | +import org.onlab.packet.IpAddress; | ||
28 | +import org.onosproject.net.DeviceId; | ||
29 | +import org.onosproject.protocol.rest.RestSBController; | ||
30 | +import org.onosproject.protocol.rest.RestSBDevice; | ||
31 | +import org.osgi.service.component.ComponentContext; | ||
32 | +import org.slf4j.Logger; | ||
33 | +import org.slf4j.LoggerFactory; | ||
34 | + | ||
35 | +import javax.ws.rs.core.MediaType; | ||
36 | +import javax.ws.rs.core.Response; | ||
37 | +import java.io.ByteArrayInputStream; | ||
38 | +import java.io.IOException; | ||
39 | +import java.io.InputStream; | ||
40 | +import java.nio.charset.StandardCharsets; | ||
41 | +import java.util.Map; | ||
42 | +import java.util.concurrent.ConcurrentHashMap; | ||
43 | + | ||
44 | +/** | ||
45 | + * The implementation of RestSBController. | ||
46 | + */ | ||
47 | +@Component(immediate = true) | ||
48 | +@Service | ||
49 | +public class RestSBControllerImpl implements RestSBController { | ||
50 | + | ||
51 | + private static final Logger log = | ||
52 | + LoggerFactory.getLogger(RestSBControllerImpl.class); | ||
53 | + private static final String APPLICATION = "application/"; | ||
54 | + private static final String XML = "xml"; | ||
55 | + private static final String JSON = "json"; | ||
56 | + private static final String DOUBLESLASH = "//"; | ||
57 | + private static final String COLON = ":"; | ||
58 | + private static final int STATUS_OK = Response.Status.OK.getStatusCode(); | ||
59 | + private static final int STATUS_CREATED = Response.Status.CREATED.getStatusCode(); | ||
60 | + private static final int STATUS_ACCEPTED = Response.Status.ACCEPTED.getStatusCode(); | ||
61 | + private static final String SLASH = "/"; | ||
62 | + | ||
63 | + private final Map<DeviceId, RestSBDevice> deviceMap = new ConcurrentHashMap<>(); | ||
64 | + Client client; | ||
65 | + | ||
66 | + @Activate | ||
67 | + public void activate(ComponentContext context) { | ||
68 | + client = Client.create(); | ||
69 | + log.info("Started"); | ||
70 | + } | ||
71 | + | ||
72 | + @Deactivate | ||
73 | + public void deactivate() { | ||
74 | + deviceMap.clear(); | ||
75 | + log.info("Stopped"); | ||
76 | + } | ||
77 | + | ||
78 | + @Override | ||
79 | + public Map<DeviceId, RestSBDevice> getDevices() { | ||
80 | + return deviceMap; | ||
81 | + } | ||
82 | + | ||
83 | + @Override | ||
84 | + public RestSBDevice getDevice(DeviceId deviceInfo) { | ||
85 | + return deviceMap.get(deviceInfo); | ||
86 | + } | ||
87 | + | ||
88 | + @Override | ||
89 | + public RestSBDevice getDevice(IpAddress ip, int port) { | ||
90 | + for (DeviceId info : deviceMap.keySet()) { | ||
91 | + if (IpAddress.valueOf(info.uri().getHost()).equals(ip) && | ||
92 | + info.uri().getPort() == port) { | ||
93 | + return deviceMap.get(info); | ||
94 | + } | ||
95 | + } | ||
96 | + return null; | ||
97 | + } | ||
98 | + | ||
99 | + @Override | ||
100 | + public void addDevice(RestSBDevice device) { | ||
101 | + deviceMap.put(device.deviceId(), device); | ||
102 | + } | ||
103 | + | ||
104 | + @Override | ||
105 | + public void removeDevice(RestSBDevice device) { | ||
106 | + deviceMap.remove(device.deviceId()); | ||
107 | + } | ||
108 | + | ||
109 | + @Override | ||
110 | + public boolean post(DeviceId device, String request, InputStream payload, String mediaType) { | ||
111 | + WebResource webResource = getWebResource(device, request); | ||
112 | + | ||
113 | + ClientResponse response = null; | ||
114 | + if (payload != null) { | ||
115 | + try { | ||
116 | + response = webResource.accept(mediaType) | ||
117 | + .post(ClientResponse.class, IOUtils.toString(payload, StandardCharsets.UTF_8)); | ||
118 | + } catch (IOException e) { | ||
119 | + log.error("Cannot do POST {} request on device {} because can't read payload", | ||
120 | + request, device); | ||
121 | + } | ||
122 | + } else { | ||
123 | + response = webResource.accept(mediaType) | ||
124 | + .post(ClientResponse.class); | ||
125 | + } | ||
126 | + return checkReply(response); | ||
127 | + } | ||
128 | + | ||
129 | + @Override | ||
130 | + public boolean put(DeviceId device, String request, InputStream payload, String mediaType) { | ||
131 | + WebResource webResource = getWebResource(device, request); | ||
132 | + ClientResponse response = null; | ||
133 | + if (payload != null) { | ||
134 | + try { | ||
135 | + response = webResource.accept(mediaType) | ||
136 | + .put(ClientResponse.class, IOUtils.toString(payload, StandardCharsets.UTF_8)); | ||
137 | + } catch (IOException e) { | ||
138 | + log.error("Cannot do PUT {} request on device {} because can't read payload", | ||
139 | + request, device); | ||
140 | + } | ||
141 | + } else { | ||
142 | + response = webResource.accept(mediaType) | ||
143 | + .put(ClientResponse.class); | ||
144 | + } | ||
145 | + return checkReply(response); | ||
146 | + } | ||
147 | + | ||
148 | + @Override | ||
149 | + public InputStream get(DeviceId device, String request, String mediaType) { | ||
150 | + WebResource webResource = getWebResource(device, request); | ||
151 | + String type; | ||
152 | + switch (mediaType) { | ||
153 | + case XML: | ||
154 | + type = MediaType.APPLICATION_XML; | ||
155 | + break; | ||
156 | + case JSON: | ||
157 | + type = MediaType.APPLICATION_JSON; | ||
158 | + break; | ||
159 | + default: | ||
160 | + throw new IllegalArgumentException("Unsupported media type " + mediaType); | ||
161 | + | ||
162 | + } | ||
163 | + return new ByteArrayInputStream(webResource.accept(type).get(ClientResponse.class) | ||
164 | + .getEntity(String.class) | ||
165 | + .getBytes(StandardCharsets.UTF_8)); | ||
166 | + } | ||
167 | + | ||
168 | + @Override | ||
169 | + public boolean delete(DeviceId device, String request, InputStream payload, String mediaType) { | ||
170 | + WebResource webResource = getWebResource(device, request); | ||
171 | + ClientResponse response = null; | ||
172 | + if (payload != null) { | ||
173 | + try { | ||
174 | + response = webResource.accept(mediaType) | ||
175 | + .delete(ClientResponse.class, IOUtils.toString(payload, StandardCharsets.UTF_8)); | ||
176 | + } catch (IOException e) { | ||
177 | + log.error("Cannot do PUT {} request on device {} because can't read payload", | ||
178 | + request, device); | ||
179 | + } | ||
180 | + } else { | ||
181 | + response = webResource.accept(mediaType) | ||
182 | + .delete(ClientResponse.class); | ||
183 | + } | ||
184 | + return checkReply(response); | ||
185 | + } | ||
186 | + | ||
187 | + private WebResource getWebResource(DeviceId device, String request) { | ||
188 | + return Client.create().resource(deviceMap.get(device).protocol() + COLON + | ||
189 | + DOUBLESLASH + | ||
190 | + deviceMap.get(device).ip().toString() + | ||
191 | + COLON + deviceMap.get(device).port() + | ||
192 | + SLASH + request); | ||
193 | + } | ||
194 | + | ||
195 | + private boolean checkReply(ClientResponse response) { | ||
196 | + if (response != null) { | ||
197 | + if (response.getStatus() == STATUS_OK || | ||
198 | + response.getStatus() == STATUS_CREATED || | ||
199 | + response.getStatus() == STATUS_ACCEPTED) { | ||
200 | + return true; | ||
201 | + } else { | ||
202 | + log.error("Failed request: HTTP error code : " | ||
203 | + + response.getStatus()); | ||
204 | + return false; | ||
205 | + } | ||
206 | + } | ||
207 | + log.error("Null reply from device"); | ||
208 | + return false; | ||
209 | + } | ||
210 | +} |
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 | +/** | ||
18 | + * NETCONF libraries. | ||
19 | + */ | ||
20 | +package org.onosproject.protocol.rest.ctl; |
protocols/rest/pom.xml
0 → 100644
1 | +<?xml version="1.0" encoding="UTF-8"?> | ||
2 | +<!-- | ||
3 | + ~ Copyright 2016 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" | ||
19 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
20 | + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
21 | + <parent> | ||
22 | + <artifactId>onos-protocols</artifactId> | ||
23 | + <groupId>org.onosproject</groupId> | ||
24 | + <version>1.5.0-SNAPSHOT</version> | ||
25 | + </parent> | ||
26 | + <modelVersion>4.0.0</modelVersion> | ||
27 | + | ||
28 | + <artifactId>onos-restsb</artifactId> | ||
29 | + <packaging>pom</packaging> | ||
30 | + <modules> | ||
31 | + <module>api</module> | ||
32 | + <module>ctl</module> | ||
33 | + </modules> | ||
34 | + <dependencies> | ||
35 | + <dependency> | ||
36 | + <groupId>org.onosproject</groupId> | ||
37 | + <artifactId>onos-api</artifactId> | ||
38 | + <version>${project.version}</version> | ||
39 | + </dependency> | ||
40 | + </dependencies> | ||
41 | + | ||
42 | + | ||
43 | +</project> | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
... | @@ -38,6 +38,7 @@ | ... | @@ -38,6 +38,7 @@ |
38 | <version>${project.version}</version> | 38 | <version>${project.version}</version> |
39 | </dependency> | 39 | </dependency> |
40 | <!-- Add other dependencies here as more bundles are added to the app --> | 40 | <!-- Add other dependencies here as more bundles are added to the app --> |
41 | + | ||
41 | </dependencies> | 42 | </dependencies> |
42 | 43 | ||
43 | </project> | 44 | </project> | ... | ... |
providers/rest/app/app.xml
0 → 100644
1 | +<?xml version="1.0" encoding="UTF-8"?> | ||
2 | +<!-- | ||
3 | + ~ Copyright 2016 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 | +<app name="org.onosproject.restsb" origin="ON.Lab" version="${project.version}" | ||
18 | + featuresRepo="mvn:${project.groupId}/${project.artifactId}/${project.version}/xml/features" | ||
19 | + features="${project.artifactId}"> | ||
20 | + <description>${project.description}</description> | ||
21 | + | ||
22 | + <artifact>mvn:${project.groupId}/onos-restsb-api/${project.version}</artifact> | ||
23 | + <artifact>mvn:${project.groupId}/onos-restsb-ctl/${project.version}</artifact> | ||
24 | + <artifact>mvn:${project.groupId}/onos-drivers/${project.version}</artifact> | ||
25 | + | ||
26 | + <artifact>mvn:${project.groupId}/onos-restsb-provider-device/${project.version}</artifact> | ||
27 | + | ||
28 | +</app> |
providers/rest/app/features.xml
0 → 100644
1 | +<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | ||
2 | +<!-- | ||
3 | + ~ Copyright 2016 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 | +<features xmlns="http://karaf.apache.org/xmlns/features/v1.2.0" name="${project.artifactId}-${project.version}"> | ||
18 | + <feature name="${project.artifactId}" version="${project.version}" | ||
19 | + description="${project.description}"> | ||
20 | + <feature>onos-api</feature> | ||
21 | + <bundle>mvn:${project.groupId}/onos-restsb-api/${project.version}</bundle> | ||
22 | + <bundle>mvn:${project.groupId}/onos-restsb-ctl/${project.version}</bundle> | ||
23 | + | ||
24 | + <bundle>mvn:${project.groupId}/onos-restsb-provider-device/${project.version}</bundle> | ||
25 | + | ||
26 | + <bundle>mvn:com.sun.jersey/jersey-client/1.19</bundle> | ||
27 | + <bundle>mvn:commons-io/commons-io/2.4</bundle> | ||
28 | + </feature> | ||
29 | +</features> | ||
30 | + |
providers/rest/app/pom.xml
0 → 100644
1 | +<?xml version="1.0" encoding="UTF-8"?> | ||
2 | +<!-- | ||
3 | + ~ Copyright 2016 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" | ||
19 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
20 | + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
21 | + <parent> | ||
22 | + <groupId>org.onosproject</groupId> | ||
23 | + <artifactId>onos-restsb-providers</artifactId> | ||
24 | + <version>1.5.0-SNAPSHOT</version> | ||
25 | + <relativePath>../pom.xml</relativePath> | ||
26 | + </parent> | ||
27 | + <modelVersion>4.0.0</modelVersion> | ||
28 | + | ||
29 | + <artifactId>onos-restsb-app</artifactId> | ||
30 | + | ||
31 | + <packaging>pom</packaging> | ||
32 | + | ||
33 | + <description>REST protocol southbound providers</description> | ||
34 | + | ||
35 | + <dependencies> | ||
36 | + <dependency> | ||
37 | + <groupId>org.onosproject</groupId> | ||
38 | + <artifactId>onos-restsb-provider-device</artifactId> | ||
39 | + <version>${project.version}</version> | ||
40 | + </dependency> | ||
41 | + <!-- Add other dependencies here as more bundles are added to the app --> | ||
42 | + </dependencies> | ||
43 | +</project> | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
providers/rest/device/pom.xml
0 → 100644
1 | +<?xml version="1.0" encoding="UTF-8"?> | ||
2 | +<!-- | ||
3 | + ~ Copyright 2016 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" | ||
19 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
20 | + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
21 | + <parent> | ||
22 | + <artifactId>onos-restsb-providers</artifactId> | ||
23 | + <groupId>org.onosproject</groupId> | ||
24 | + <version>1.5.0-SNAPSHOT</version> | ||
25 | + <relativePath>../pom.xml</relativePath> | ||
26 | + </parent> | ||
27 | + <modelVersion>4.0.0</modelVersion> | ||
28 | + | ||
29 | + <artifactId>onos-restsb-provider-device</artifactId> | ||
30 | + <packaging>bundle</packaging> | ||
31 | + <dependencies> | ||
32 | + <dependency> | ||
33 | + <groupId>org.onosproject</groupId> | ||
34 | + <artifactId>onos-restsb-api</artifactId> | ||
35 | + <version>${project.version}</version> | ||
36 | + </dependency> | ||
37 | + <dependency> | ||
38 | + <groupId>org.onosproject</groupId> | ||
39 | + <artifactId>onos-restsb-ctl</artifactId> | ||
40 | + <version>${project.version}</version> | ||
41 | + </dependency> | ||
42 | + </dependencies> | ||
43 | + <build> | ||
44 | + <plugins> | ||
45 | + <plugin> | ||
46 | + <groupId>org.apache.felix</groupId> | ||
47 | + <artifactId>maven-scr-plugin</artifactId> | ||
48 | + </plugin> | ||
49 | + <plugin> | ||
50 | + <groupId>org.onosproject</groupId> | ||
51 | + <artifactId>onos-maven-plugin</artifactId> | ||
52 | + </plugin> | ||
53 | + </plugins> | ||
54 | + </build> | ||
55 | + | ||
56 | + | ||
57 | +</project> | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
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.provider.rest.device.impl; | ||
18 | + | ||
19 | +import com.google.common.base.Preconditions; | ||
20 | +import org.apache.felix.scr.annotations.Activate; | ||
21 | +import org.apache.felix.scr.annotations.Component; | ||
22 | +import org.apache.felix.scr.annotations.Deactivate; | ||
23 | +import org.apache.felix.scr.annotations.Reference; | ||
24 | +import org.apache.felix.scr.annotations.ReferenceCardinality; | ||
25 | +import org.onlab.packet.ChassisId; | ||
26 | +import org.onosproject.core.ApplicationId; | ||
27 | +import org.onosproject.core.CoreService; | ||
28 | +import org.onosproject.incubator.net.config.basics.ConfigException; | ||
29 | +import org.onosproject.net.DefaultAnnotations; | ||
30 | +import org.onosproject.net.Device; | ||
31 | +import org.onosproject.net.DeviceId; | ||
32 | +import org.onosproject.net.MastershipRole; | ||
33 | +import org.onosproject.net.SparseAnnotations; | ||
34 | +import org.onosproject.net.config.ConfigFactory; | ||
35 | +import org.onosproject.net.config.NetworkConfigEvent; | ||
36 | +import org.onosproject.net.config.NetworkConfigListener; | ||
37 | +import org.onosproject.net.config.NetworkConfigRegistry; | ||
38 | +import org.onosproject.net.device.DefaultDeviceDescription; | ||
39 | +import org.onosproject.net.device.DeviceDescription; | ||
40 | +import org.onosproject.net.device.DeviceProvider; | ||
41 | +import org.onosproject.net.device.DeviceProviderRegistry; | ||
42 | +import org.onosproject.net.device.DeviceProviderService; | ||
43 | +import org.onosproject.net.device.PortDescription; | ||
44 | +import org.onosproject.net.provider.AbstractProvider; | ||
45 | +import org.onosproject.net.provider.ProviderId; | ||
46 | +import org.onosproject.protocol.rest.RestSBController; | ||
47 | +import org.onosproject.protocol.rest.RestSBDevice; | ||
48 | +import org.slf4j.Logger; | ||
49 | + | ||
50 | +import java.io.IOException; | ||
51 | +import java.net.HttpURLConnection; | ||
52 | +import java.net.URL; | ||
53 | +import java.util.HashSet; | ||
54 | +import java.util.List; | ||
55 | +import java.util.Set; | ||
56 | + | ||
57 | +import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_ADDED; | ||
58 | +import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_UPDATED; | ||
59 | +import static org.onosproject.net.config.basics.SubjectFactories.APP_SUBJECT_FACTORY; | ||
60 | +import static org.slf4j.LoggerFactory.getLogger; | ||
61 | + | ||
62 | +/** | ||
63 | + * Provider for devices that use REST as means of configuration communication. | ||
64 | + */ | ||
65 | +@Component(immediate = true) | ||
66 | +public class RestDeviceProvider extends AbstractProvider | ||
67 | + implements DeviceProvider { | ||
68 | + private static final String APP_NAME = "org.onosproject.restsb"; | ||
69 | + private static final String REST = "rest"; | ||
70 | + private static final String PROVIDER = "org.onosproject.provider.rest.device"; | ||
71 | + private static final String IPADDRESS = "ipaddress"; | ||
72 | + private static final int TEST_CONNECT_TIMEOUT = 1000; | ||
73 | + private final Logger log = getLogger(getClass()); | ||
74 | + | ||
75 | + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | ||
76 | + protected DeviceProviderRegistry providerRegistry; | ||
77 | + | ||
78 | + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | ||
79 | + protected RestSBController controller; | ||
80 | + | ||
81 | + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | ||
82 | + protected NetworkConfigRegistry cfgService; | ||
83 | + | ||
84 | + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | ||
85 | + protected CoreService coreService; | ||
86 | + | ||
87 | + | ||
88 | + private DeviceProviderService providerService; | ||
89 | + protected static final String ISNOTNULL = "Rest device is not null"; | ||
90 | + private static final String UNKNOWN = "unknown"; | ||
91 | + | ||
92 | + private final ConfigFactory factory = | ||
93 | + new ConfigFactory<ApplicationId, RestProviderConfig>(APP_SUBJECT_FACTORY, | ||
94 | + RestProviderConfig.class, | ||
95 | + "restDevices", | ||
96 | + true) { | ||
97 | + @Override | ||
98 | + public RestProviderConfig createConfig() { | ||
99 | + return new RestProviderConfig(); | ||
100 | + } | ||
101 | + }; | ||
102 | + private final NetworkConfigListener cfgLister = new InternalNetworkConfigListener(); | ||
103 | + private ApplicationId appId; | ||
104 | + | ||
105 | + | ||
106 | + @Activate | ||
107 | + public void activate() { | ||
108 | + appId = coreService.registerApplication(APP_NAME); | ||
109 | + providerService = providerRegistry.register(this); | ||
110 | + cfgService.registerConfigFactory(factory); | ||
111 | + cfgService.addListener(cfgLister); | ||
112 | + connectDevices(); | ||
113 | + log.info("Started"); | ||
114 | + } | ||
115 | + | ||
116 | + | ||
117 | + @Deactivate | ||
118 | + public void deactivate() { | ||
119 | + providerRegistry.unregister(this); | ||
120 | + providerService = null; | ||
121 | + cfgService.unregisterConfigFactory(factory); | ||
122 | + cfgService.removeListener(cfgLister); | ||
123 | + log.info("Stopped"); | ||
124 | + } | ||
125 | + | ||
126 | + public RestDeviceProvider() { | ||
127 | + super(new ProviderId(REST, PROVIDER)); | ||
128 | + } | ||
129 | + | ||
130 | + @Override | ||
131 | + public void triggerProbe(DeviceId deviceId) { | ||
132 | + // TODO: This will be implemented later. | ||
133 | + log.info("Triggering probe on device {}", deviceId); | ||
134 | + } | ||
135 | + | ||
136 | + @Override | ||
137 | + public void roleChanged(DeviceId deviceId, MastershipRole newRole) { | ||
138 | + // TODO: This will be implemented later. | ||
139 | + } | ||
140 | + | ||
141 | + | ||
142 | + @Override | ||
143 | + public boolean isReachable(DeviceId deviceId) { | ||
144 | + RestSBDevice restDevice = controller.getDevice(deviceId); | ||
145 | + if (restDevice == null) { | ||
146 | + log.warn("BAD REQUEST: the requested device id: " + | ||
147 | + deviceId.toString() + | ||
148 | + " is not associated to any REST Device"); | ||
149 | + return false; | ||
150 | + } | ||
151 | + return restDevice.isActive(); | ||
152 | + } | ||
153 | + | ||
154 | + private void deviceAdded(RestSBDevice nodeId) { | ||
155 | + Preconditions.checkNotNull(nodeId, ISNOTNULL); | ||
156 | + DeviceId deviceId = nodeId.deviceId(); | ||
157 | + ChassisId cid = new ChassisId(); | ||
158 | + String ipAddress = nodeId.ip().toString(); | ||
159 | + SparseAnnotations annotations = DefaultAnnotations.builder() | ||
160 | + .set(IPADDRESS, ipAddress).build(); | ||
161 | + DeviceDescription deviceDescription = new DefaultDeviceDescription( | ||
162 | + deviceId.uri(), | ||
163 | + Device.Type.SWITCH, | ||
164 | + UNKNOWN, UNKNOWN, | ||
165 | + UNKNOWN, UNKNOWN, | ||
166 | + cid, | ||
167 | + annotations); | ||
168 | + providerService.deviceConnected(deviceId, deviceDescription); | ||
169 | + nodeId.setActive(true); | ||
170 | + controller.addDevice(nodeId); | ||
171 | + } | ||
172 | + | ||
173 | + private void updatePorts(DeviceId deviceId, List<PortDescription> portDescriptions) { | ||
174 | + // TODO get driver and call behavior to get ports | ||
175 | + //signal the ports to onos | ||
176 | + } | ||
177 | + | ||
178 | + //when do I call it ? | ||
179 | + public void deviceRemoved(RestSBDevice nodeId) { | ||
180 | + Preconditions.checkNotNull(nodeId, ISNOTNULL); | ||
181 | + DeviceId deviceId = nodeId.deviceId(); | ||
182 | + providerService.deviceDisconnected(deviceId); | ||
183 | + controller.removeDevice(nodeId); | ||
184 | + } | ||
185 | + | ||
186 | + private void connectDevices() { | ||
187 | + RestProviderConfig cfg = cfgService.getConfig(appId, RestProviderConfig.class); | ||
188 | + try { | ||
189 | + if (cfg != null && cfg.getDevicesAddresses() != null) { | ||
190 | + //Precomputing the devices to be removed | ||
191 | + Set<RestSBDevice> toBeRemoved = new HashSet<>(controller.getDevices().values()); | ||
192 | + toBeRemoved.removeAll(cfg.getDevicesAddresses()); | ||
193 | + //Adding new devices | ||
194 | + cfg.getDevicesAddresses().stream() | ||
195 | + .filter(device -> testDeviceConnection(device)) | ||
196 | + .forEach(device -> { | ||
197 | + deviceAdded(device); | ||
198 | + }); | ||
199 | + //Removing devices not wanted anymore | ||
200 | + toBeRemoved.stream().forEach(device -> deviceRemoved(device)); | ||
201 | + | ||
202 | + } | ||
203 | + } catch (ConfigException e) { | ||
204 | + log.error("Configuration error {}", e); | ||
205 | + } | ||
206 | + log.info("REST Devices {}", controller.getDevices()); | ||
207 | + //TODO ask for ports then call update ports. | ||
208 | + | ||
209 | + } | ||
210 | + | ||
211 | + private boolean testDeviceConnection(RestSBDevice device) { | ||
212 | + try { | ||
213 | + URL url = new URL(device.protocol(), device.ip().toString(), device.port(), "/"); | ||
214 | + HttpURLConnection urlConn = (HttpURLConnection) url.openConnection(); | ||
215 | + urlConn.setConnectTimeout(TEST_CONNECT_TIMEOUT); | ||
216 | + boolean open = urlConn.getResponseCode() == (HttpURLConnection.HTTP_OK); | ||
217 | + urlConn.disconnect(); | ||
218 | + return open; | ||
219 | + } catch (IOException e) { | ||
220 | + log.error("Device {} not reachable, error creating HTTP connection", device, e); | ||
221 | + } | ||
222 | + return false; | ||
223 | + } | ||
224 | + | ||
225 | + private class InternalNetworkConfigListener implements NetworkConfigListener { | ||
226 | + | ||
227 | + | ||
228 | + @Override | ||
229 | + public void event(NetworkConfigEvent event) { | ||
230 | + connectDevices(); | ||
231 | + } | ||
232 | + | ||
233 | + @Override | ||
234 | + public boolean isRelevant(NetworkConfigEvent event) { | ||
235 | + //TODO refactor | ||
236 | + return event.configClass().equals(RestProviderConfig.class) && | ||
237 | + (event.type() == CONFIG_ADDED || | ||
238 | + event.type() == CONFIG_UPDATED); | ||
239 | + } | ||
240 | + } | ||
241 | +} |
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.provider.rest.device.impl; | ||
18 | + | ||
19 | +import com.fasterxml.jackson.databind.JsonNode; | ||
20 | +import com.google.common.annotations.Beta; | ||
21 | +import com.google.common.collect.Sets; | ||
22 | +import org.onlab.packet.IpAddress; | ||
23 | +import org.onosproject.core.ApplicationId; | ||
24 | +import org.onosproject.incubator.net.config.basics.ConfigException; | ||
25 | +import org.onosproject.net.config.Config; | ||
26 | +import org.onosproject.protocol.rest.DefaultRestSBDevice; | ||
27 | +import org.onosproject.protocol.rest.RestSBDevice; | ||
28 | + | ||
29 | + | ||
30 | +import java.util.Set; | ||
31 | + | ||
32 | +/** | ||
33 | + * Configuration for RestSB provider. | ||
34 | + */ | ||
35 | +@Beta | ||
36 | +public class RestProviderConfig extends Config<ApplicationId> { | ||
37 | + | ||
38 | + public static final String CONFIG_VALUE_ERROR = "Error parsing config value"; | ||
39 | + private static final String IP = "ip"; | ||
40 | + private static final int DEFAULT_HTTP_PORT = 80; | ||
41 | + private static final String PORT = "port"; | ||
42 | + private static final String NAME = "name"; | ||
43 | + private static final String PASSWORD = "password"; | ||
44 | + private static final String PROTOCOL = "protocol"; | ||
45 | + | ||
46 | + public Set<RestSBDevice> getDevicesAddresses() throws ConfigException { | ||
47 | + Set<RestSBDevice> devicesAddresses = Sets.newHashSet(); | ||
48 | + | ||
49 | + try { | ||
50 | + for (JsonNode node : array) { | ||
51 | + String ip = node.path(IP).asText(); | ||
52 | + IpAddress ipAddr = ip.isEmpty() ? null : IpAddress.valueOf(ip); | ||
53 | + int port = node.path(PORT).asInt(DEFAULT_HTTP_PORT); | ||
54 | + String name = node.path(NAME).asText(); | ||
55 | + String password = node.path(PASSWORD).asText(); | ||
56 | + String protocol = node.path(PROTOCOL).asText(); | ||
57 | + devicesAddresses.add(new DefaultRestSBDevice(ipAddr, port, name, | ||
58 | + password, protocol, false)); | ||
59 | + | ||
60 | + } | ||
61 | + } catch (IllegalArgumentException e) { | ||
62 | + throw new ConfigException(CONFIG_VALUE_ERROR, e); | ||
63 | + } | ||
64 | + | ||
65 | + return devicesAddresses; | ||
66 | + } | ||
67 | + | ||
68 | +} |
providers/rest/device/src/main/java/org/onosproject/provider/rest/device/impl/package-info.java
0 → 100644
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 | +/** | ||
18 | + * Provider that uses REST calls request as a means of device discovery and setup. | ||
19 | + */ | ||
20 | +package org.onosproject.provider.rest.device.impl; | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
providers/rest/pom.xml
0 → 100644
1 | +<?xml version="1.0" encoding="UTF-8"?> | ||
2 | +<!-- | ||
3 | + ~ Copyright 2016 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" | ||
19 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
20 | + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
21 | + <parent> | ||
22 | + <artifactId>onos-providers</artifactId> | ||
23 | + <groupId>org.onosproject</groupId> | ||
24 | + <version>1.5.0-SNAPSHOT</version> | ||
25 | + </parent> | ||
26 | + <modelVersion>4.0.0</modelVersion> | ||
27 | + | ||
28 | + <artifactId>onos-restsb-providers</artifactId> | ||
29 | + <packaging>pom</packaging> | ||
30 | + <modules> | ||
31 | + <module>app</module> | ||
32 | + <module>device</module> | ||
33 | + </modules> | ||
34 | +</project> | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
tools/test/configs/restSB-cfg.json
0 → 100644
1 | +{ | ||
2 | + "devices": { | ||
3 | + "rest:127.0.0.1:8080": { | ||
4 | + "basic": { | ||
5 | + "driver": "rest" | ||
6 | + } | ||
7 | + } | ||
8 | + }, | ||
9 | + "apps": { | ||
10 | + "org.onosproject.restsb": { | ||
11 | + "restDevices": [{ | ||
12 | + "name": "local", | ||
13 | + "password": "local", | ||
14 | + "ip": "127.0.0.1", | ||
15 | + "port": 8080, | ||
16 | + "protocol": "http" | ||
17 | + }] | ||
18 | + } | ||
19 | + } | ||
20 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
-
Please register or login to post a comment