Yixiao Chen | 5ece00f | 2016-09-14 16:23:24 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.provider.te.topology; |
| 17 | |
| 18 | import java.util.Set; |
| 19 | |
| 20 | import org.onlab.packet.IpAddress; |
| 21 | import org.onosproject.core.ApplicationId; |
| 22 | import org.onosproject.incubator.net.config.basics.ConfigException; |
| 23 | import org.onosproject.net.config.Config; |
| 24 | import org.onosproject.protocol.rest.DefaultRestSBDevice; |
| 25 | import org.onosproject.protocol.rest.RestSBDevice; |
| 26 | |
| 27 | import com.fasterxml.jackson.databind.JsonNode; |
| 28 | import com.google.common.collect.Sets; |
| 29 | |
| 30 | /** |
| 31 | * Configuration for Restconf Server. |
| 32 | */ |
| 33 | public class RestconfServerConfig extends Config<ApplicationId> { |
| 34 | private static final String CONFIG_VALUE_ERROR = "Error parsing config value"; |
| 35 | private static final String IP = "ip"; |
| 36 | private static final int DEFAULT_HTTP_PORT = 80; |
| 37 | private static final String PORT = "port"; |
| 38 | private static final String USERNAME = "username"; |
| 39 | private static final String PASSWORD = "password"; |
| 40 | private static final String PROTOCOL = "protocol"; |
| 41 | private static final String URL = "url"; |
| 42 | |
| 43 | /** |
| 44 | * Returns the device addresses from JSON. |
| 45 | * |
| 46 | * @return A set of RESTCONF Server devices |
| 47 | * @throws ConfigException if there is a configuration error |
| 48 | */ |
| 49 | public Set<RestSBDevice> getDevicesAddresses() throws ConfigException { |
| 50 | Set<RestSBDevice> devicesAddresses = Sets.newHashSet(); |
| 51 | |
| 52 | try { |
| 53 | for (JsonNode node : array) { |
| 54 | String ip = node.path(IP).asText(); |
| 55 | IpAddress ipAddr = ip.isEmpty() ? null : IpAddress.valueOf(ip); |
| 56 | int port = node.path(PORT).asInt(DEFAULT_HTTP_PORT); |
| 57 | String username = node.path(USERNAME).asText(); |
| 58 | String password = node.path(PASSWORD).asText(); |
| 59 | String protocol = node.path(PROTOCOL).asText(); |
| 60 | String url = node.path(URL).asText(); |
| 61 | devicesAddresses.add(new DefaultRestSBDevice(ipAddr, port, username, |
| 62 | password, protocol, |
| 63 | url, false)); |
| 64 | |
| 65 | } |
| 66 | } catch (IllegalArgumentException e) { |
| 67 | throw new ConfigException(CONFIG_VALUE_ERROR, e); |
| 68 | } |
| 69 | |
| 70 | return devicesAddresses; |
| 71 | } |
| 72 | |
| 73 | } |