Carmelo Cascone | 3bb71c1 | 2016-04-06 21:30:44 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014-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.bmv2.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 | |
| 27 | import java.util.Set; |
| 28 | |
| 29 | import static com.google.common.base.Preconditions.checkNotNull; |
| 30 | |
| 31 | /** |
| 32 | * Configuration decoder for Bmv2 provider. |
| 33 | */ |
| 34 | @Beta |
| 35 | public class Bmv2ProviderConfig extends Config<ApplicationId> { |
| 36 | public static final String CONFIG_VALUE_ERROR = "Error parsing config value"; |
| 37 | private static final String IP = "ip"; |
| 38 | private static final int DEFAULT_THRIFT_PORT = 9090; |
| 39 | private static final String PORT = "port"; |
| 40 | |
| 41 | /** |
| 42 | * Retrieves a set of Bmv2DeviceInfo containing all the device |
| 43 | * configuration pertaining to the Bmv2 device provider. |
| 44 | * |
| 45 | * @return set of device configurations. |
| 46 | * @throws ConfigException if configuration can't be read |
| 47 | */ |
| 48 | public Set<Bmv2DeviceInfo> getDevicesInfo() throws ConfigException { |
| 49 | Set<Bmv2DeviceInfo> deviceInfos = Sets.newHashSet(); |
| 50 | |
| 51 | try { |
| 52 | for (JsonNode node : array) { |
| 53 | String ip = node.path(IP).asText(); |
| 54 | IpAddress ipAddr = ip.isEmpty() ? null : IpAddress.valueOf(ip); |
| 55 | int port = node.path(PORT).asInt(DEFAULT_THRIFT_PORT); |
| 56 | deviceInfos.add(new Bmv2DeviceInfo(ipAddr, port)); |
| 57 | |
| 58 | } |
| 59 | } catch (IllegalArgumentException e) { |
| 60 | throw new ConfigException(CONFIG_VALUE_ERROR, e); |
| 61 | } |
| 62 | |
| 63 | return deviceInfos; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Contains information about a Bmv2 device retrieved from the net-cfg |
| 68 | * subsystem. |
| 69 | */ |
| 70 | public static class Bmv2DeviceInfo { |
| 71 | private final IpAddress ip; |
| 72 | private final int port; |
| 73 | |
| 74 | /** |
| 75 | * Build an information object containing the given device specifics. |
| 76 | * |
| 77 | * @param ip ip |
| 78 | * @param port port |
| 79 | */ |
| 80 | public Bmv2DeviceInfo(IpAddress ip, int port) { |
| 81 | // TODO use generalized host string instead of IP address |
| 82 | this.ip = checkNotNull(ip, "ip cannot be null"); |
| 83 | this.port = checkNotNull(port, "port cannot be null"); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Returns IpAddress of the device. |
| 88 | * |
| 89 | * @return ip |
| 90 | */ |
| 91 | public IpAddress ip() { |
| 92 | return ip; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Returns port of the device. |
| 97 | * |
| 98 | * @return port |
| 99 | */ |
| 100 | public int port() { |
| 101 | return port; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | } |