blob: 558a158d8f7509072cde6f92b73fa60f74e84788 [file] [log] [blame]
Andrea Campanella945ded22016-01-07 13:17:43 -08001/*
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
17package org.onosproject.provider.rest.device.impl;
18
19import com.fasterxml.jackson.databind.JsonNode;
20import com.google.common.annotations.Beta;
21import com.google.common.collect.Sets;
22import org.onlab.packet.IpAddress;
23import org.onosproject.core.ApplicationId;
24import org.onosproject.incubator.net.config.basics.ConfigException;
25import org.onosproject.net.config.Config;
26import org.onosproject.protocol.rest.DefaultRestSBDevice;
27import org.onosproject.protocol.rest.RestSBDevice;
28
29
30import java.util.Set;
31
32/**
33 * Configuration for RestSB provider.
34 */
35@Beta
36public 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";
Andrea Campanella784ee0f2016-02-17 15:50:59 -080042 private static final String USERNAME = "username";
Andrea Campanella945ded22016-01-07 13:17:43 -080043 private static final String PASSWORD = "password";
44 private static final String PROTOCOL = "protocol";
Andrea Campanella2947e622016-01-27 09:23:46 -080045 private static final String URL = "url";
Andrea Campanella945ded22016-01-07 13:17:43 -080046
47 public Set<RestSBDevice> getDevicesAddresses() throws ConfigException {
48 Set<RestSBDevice> devicesAddresses = Sets.newHashSet();
49
50 try {
51 for (JsonNode node : array) {
52 String ip = node.path(IP).asText();
53 IpAddress ipAddr = ip.isEmpty() ? null : IpAddress.valueOf(ip);
54 int port = node.path(PORT).asInt(DEFAULT_HTTP_PORT);
Andrea Campanella784ee0f2016-02-17 15:50:59 -080055 String username = node.path(USERNAME).asText();
Andrea Campanella945ded22016-01-07 13:17:43 -080056 String password = node.path(PASSWORD).asText();
57 String protocol = node.path(PROTOCOL).asText();
Andrea Campanella2947e622016-01-27 09:23:46 -080058 String url = node.path(URL).asText();
Andrea Campanella784ee0f2016-02-17 15:50:59 -080059 devicesAddresses.add(new DefaultRestSBDevice(ipAddr, port, username,
Andrea Campanella2947e622016-01-27 09:23:46 -080060 password, protocol,
61 url, false));
Andrea Campanella945ded22016-01-07 13:17:43 -080062
63 }
64 } catch (IllegalArgumentException e) {
65 throw new ConfigException(CONFIG_VALUE_ERROR, e);
66 }
67
68 return devicesAddresses;
69 }
70
71}