Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 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 | |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 29 | import java.util.Set; |
| 30 | |
| 31 | /** |
| 32 | * Configuration for RestSB provider. |
| 33 | */ |
| 34 | @Beta |
| 35 | public class RestProviderConfig extends Config<ApplicationId> { |
| 36 | |
| 37 | public static final String CONFIG_VALUE_ERROR = "Error parsing config value"; |
| 38 | private static final String IP = "ip"; |
| 39 | private static final int DEFAULT_HTTP_PORT = 80; |
| 40 | private static final String PORT = "port"; |
Andrea Campanella | 784ee0f | 2016-02-17 15:50:59 -0800 | [diff] [blame] | 41 | private static final String USERNAME = "username"; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 42 | private static final String PASSWORD = "password"; |
| 43 | private static final String PROTOCOL = "protocol"; |
Andrea Campanella | 2947e62 | 2016-01-27 09:23:46 -0800 | [diff] [blame] | 44 | private static final String URL = "url"; |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 45 | private static final String TESTURL = "testUrl"; |
| 46 | private static final String MANUFACTURER = "manufacturer"; |
| 47 | private static final String HWVERSION = "hwVersion"; |
| 48 | private static final String SWVERSION = "swVersion"; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 49 | |
| 50 | public Set<RestSBDevice> getDevicesAddresses() throws ConfigException { |
| 51 | Set<RestSBDevice> devicesAddresses = Sets.newHashSet(); |
| 52 | |
| 53 | try { |
| 54 | for (JsonNode node : array) { |
| 55 | String ip = node.path(IP).asText(); |
| 56 | IpAddress ipAddr = ip.isEmpty() ? null : IpAddress.valueOf(ip); |
| 57 | int port = node.path(PORT).asInt(DEFAULT_HTTP_PORT); |
Andrea Campanella | 784ee0f | 2016-02-17 15:50:59 -0800 | [diff] [blame] | 58 | String username = node.path(USERNAME).asText(); |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 59 | String password = node.path(PASSWORD).asText(); |
| 60 | String protocol = node.path(PROTOCOL).asText(); |
Andrea Campanella | 2947e62 | 2016-01-27 09:23:46 -0800 | [diff] [blame] | 61 | String url = node.path(URL).asText(); |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 62 | String testUrl = node.path(TESTURL).asText(); |
| 63 | String manufacturer = node.path(MANUFACTURER).asText(); |
| 64 | String hwVersion = node.path(HWVERSION).asText(); |
| 65 | String swVersion = node.path(SWVERSION).asText(); |
| 66 | |
Andrea Campanella | 784ee0f | 2016-02-17 15:50:59 -0800 | [diff] [blame] | 67 | devicesAddresses.add(new DefaultRestSBDevice(ipAddr, port, username, |
Andrea Campanella | 2947e62 | 2016-01-27 09:23:46 -0800 | [diff] [blame] | 68 | password, protocol, |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 69 | url, false, testUrl, manufacturer, |
| 70 | hwVersion, swVersion)); |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 71 | } |
| 72 | } catch (IllegalArgumentException e) { |
| 73 | throw new ConfigException(CONFIG_VALUE_ERROR, e); |
| 74 | } |
| 75 | |
| 76 | return devicesAddresses; |
| 77 | } |
| 78 | |
| 79 | } |