Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | */ |
Thomas Vachuska | 4998caa | 2015-08-26 13:28:38 -0700 | [diff] [blame] | 16 | package org.onosproject.net.config.basics; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 17 | |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 19 | import org.onlab.packet.IpAddress; |
| 20 | import org.onosproject.net.ConnectPoint; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 21 | import org.onosproject.net.HostId; |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 22 | import java.util.HashSet; |
| 23 | import java.util.Set; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 24 | |
| 25 | /** |
| 26 | * Basic configuration for network end-station hosts. |
| 27 | */ |
| 28 | public class BasicHostConfig extends BasicElementConfig<HostId> { |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 29 | private static final String IPS = "ips"; |
| 30 | private static final String LOCATION = "location"; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 31 | |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 32 | @Override |
| 33 | public boolean isValid() { |
| 34 | return hasOnlyFields(IPS, LOCATION) && |
| 35 | this.location() != null && |
| 36 | this.ipAddresses() != null; |
| 37 | } |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 38 | |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 39 | /** |
| 40 | * Gets location of the host. |
| 41 | * |
| 42 | * @return location of the host. Or null if not specified with correct format. |
| 43 | */ |
| 44 | public ConnectPoint location() { |
| 45 | String location = get(LOCATION, null); |
| 46 | |
| 47 | if (location != null) { |
| 48 | try { |
| 49 | return ConnectPoint.deviceConnectPoint(location); |
| 50 | } catch (Exception e) { |
| 51 | return null; |
| 52 | } |
| 53 | } |
| 54 | return null; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Sets the location of the host. |
| 59 | * |
| 60 | * @param location location of the host. |
| 61 | * @return the config of the host. |
| 62 | */ |
| 63 | public BasicHostConfig setLocation(String location) { |
| 64 | return (BasicHostConfig) setOrClear(LOCATION, location); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Gets IP addresses of the host. |
| 69 | * |
| 70 | * @return IP addresses of the host. Or null if not specified with correct format. |
| 71 | */ |
| 72 | public Set<IpAddress> ipAddresses() { |
| 73 | HashSet<IpAddress> ipAddresses = new HashSet<>(); |
| 74 | if (object.has(IPS)) { |
| 75 | ArrayNode ipNodes = (ArrayNode) object.path(IPS); |
| 76 | try { |
| 77 | ipNodes.forEach(ipNode -> { |
| 78 | ipAddresses.add(IpAddress.valueOf(ipNode.asText())); |
| 79 | }); |
| 80 | return ipAddresses; |
| 81 | } catch (Exception e) { |
| 82 | return null; |
| 83 | } |
| 84 | } |
| 85 | return null; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Sets the IP addresses of the host. |
| 90 | * |
| 91 | * @param ipAddresses IP addresses of the host. |
| 92 | * @return the config of the host. |
| 93 | */ |
| 94 | public BasicHostConfig setIps(Set<IpAddress> ipAddresses) { |
| 95 | return (BasicHostConfig) setOrClear(IPS, ipAddresses); |
| 96 | } |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 97 | } |