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