blob: 2056dcea291a93bbfab224115e8bf7b0d1435505 [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";
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}