Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 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; |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 22 | |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 23 | import java.util.HashSet; |
| 24 | import java.util.Set; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 25 | |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 26 | import static org.onosproject.net.config.basics.AllowedEntityConfig.ALLOWED; |
| 27 | |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 28 | /** |
| 29 | * Basic configuration for network end-station hosts. |
| 30 | */ |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 31 | public final class BasicHostConfig extends BasicElementConfig<HostId> { |
| 32 | |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 33 | private static final String IPS = "ips"; |
| 34 | private static final String LOCATION = "location"; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 35 | |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 36 | @Override |
| 37 | public boolean isValid() { |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 38 | // Location and IP addresses can be absent, but if present must be valid. |
| 39 | this.location(); |
| 40 | this.ipAddresses(); |
| 41 | return hasOnlyFields(ALLOWED, NAME, LATITUDE, LONGITUDE, RACK_ADDRESS, OWNER, |
| 42 | IPS, LOCATION); |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 43 | } |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 44 | |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 45 | /** |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 46 | * Returns location of the host. |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 47 | * |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 48 | * @return location of the host or null if not set |
| 49 | * @throws IllegalArgumentException if not specified with correct format |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 50 | */ |
| 51 | public ConnectPoint location() { |
| 52 | String location = get(LOCATION, null); |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 53 | return location != null ? ConnectPoint.deviceConnectPoint(location) : null; |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Sets the location of the host. |
| 58 | * |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 59 | * @param location location of the host or null to unset |
| 60 | * @return the config of the host |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 61 | */ |
| 62 | public BasicHostConfig setLocation(String location) { |
| 63 | return (BasicHostConfig) setOrClear(LOCATION, location); |
| 64 | } |
| 65 | |
| 66 | /** |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 67 | * Returns IP addresses of the host. |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 68 | * |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 69 | * @return IP addresses of the host or null if not set |
| 70 | * @throws IllegalArgumentException if not specified with correct format |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 71 | */ |
| 72 | public Set<IpAddress> ipAddresses() { |
| 73 | HashSet<IpAddress> ipAddresses = new HashSet<>(); |
| 74 | if (object.has(IPS)) { |
| 75 | ArrayNode ipNodes = (ArrayNode) object.path(IPS); |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 76 | ipNodes.forEach(n -> ipAddresses.add(IpAddress.valueOf(n.asText()))); |
| 77 | return ipAddresses; |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 78 | } |
| 79 | return null; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Sets the IP addresses of the host. |
| 84 | * |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 85 | * @param ipAddresses IP addresses of the host or null to unset |
| 86 | * @return the config of the host |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 87 | */ |
| 88 | public BasicHostConfig setIps(Set<IpAddress> ipAddresses) { |
| 89 | return (BasicHostConfig) setOrClear(IPS, ipAddresses); |
| 90 | } |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 91 | } |