Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
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; |
Szymon Giermakowski | f3578de | 2017-08-29 22:02:48 +0200 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableSet; |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 20 | import org.onlab.packet.IpAddress; |
| 21 | import org.onosproject.net.ConnectPoint; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 22 | import org.onosproject.net.HostId; |
Charles Chan | 61fc0d8 | 2017-04-28 14:13:36 -0700 | [diff] [blame] | 23 | import org.onosproject.net.HostLocation; |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 24 | |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 25 | import java.util.HashSet; |
| 26 | import java.util.Set; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 27 | |
| 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"; |
Charles Chan | 61fc0d8 | 2017-04-28 14:13:36 -0700 | [diff] [blame] | 34 | private static final String LOCATIONS = "locations"; |
Simon Hunt | 10618f6 | 2017-06-15 19:30:52 -0700 | [diff] [blame] | 35 | private static final String DASH = "-"; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 36 | |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 37 | @Override |
| 38 | public boolean isValid() { |
Charles Chan | 60c4528 | 2017-07-11 15:07:59 -0700 | [diff] [blame] | 39 | // locations is mandatory and must have at least one |
| 40 | // ipAddresses can be absent, but if present must be valid |
Charles Chan | 61fc0d8 | 2017-04-28 14:13:36 -0700 | [diff] [blame] | 41 | this.locations(); |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 42 | this.ipAddresses(); |
Thomas Vachuska | 0d93386 | 2018-04-06 00:29:30 -0700 | [diff] [blame] | 43 | return hasOnlyFields(ALLOWED, NAME, LOC_TYPE, LATITUDE, LONGITUDE, ROLES, |
| 44 | GRID_X, GRID_Y, UI_TYPE, RACK_ADDRESS, OWNER, IPS, LOCATIONS); |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 45 | } |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 46 | |
Simon Hunt | 10618f6 | 2017-06-15 19:30:52 -0700 | [diff] [blame] | 47 | @Override |
| 48 | public String name() { |
| 49 | // NOTE: |
| 50 | // We don't want to default to host ID if friendly name is not set; |
| 51 | // (it isn't particularly friendly, e.g. "00:00:00:00:00:01/None"). |
| 52 | // We'd prefer to clear the annotation, but if we pass null, then the |
| 53 | // value won't get set (see BasicElementOperator). So, instead we will |
| 54 | // return a DASH to signify "use the default friendly name". |
| 55 | return get(NAME, DASH); |
| 56 | } |
| 57 | |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 58 | /** |
Simon Hunt | 1e20dae | 2016-10-28 11:26:26 -0700 | [diff] [blame] | 59 | * Returns the location of the host. |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 60 | * |
Szymon Giermakowski | f3578de | 2017-08-29 22:02:48 +0200 | [diff] [blame] | 61 | * @return location of the host or null if none specified |
| 62 | * @throws IllegalArgumentException if locations are set but empty or not |
| 63 | * specified with correct format |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 64 | */ |
Charles Chan | 61fc0d8 | 2017-04-28 14:13:36 -0700 | [diff] [blame] | 65 | public Set<HostLocation> locations() { |
Szymon Giermakowski | f3578de | 2017-08-29 22:02:48 +0200 | [diff] [blame] | 66 | if (!object.has(LOCATIONS)) { |
| 67 | return null; //no locations are specified |
Charles Chan | 61fc0d8 | 2017-04-28 14:13:36 -0700 | [diff] [blame] | 68 | } |
Szymon Giermakowski | f3578de | 2017-08-29 22:02:48 +0200 | [diff] [blame] | 69 | |
| 70 | ImmutableSet.Builder<HostLocation> locationsSetBuilder = ImmutableSet.<HostLocation>builder(); |
| 71 | |
| 72 | ArrayNode locationNodes = (ArrayNode) object.path(LOCATIONS); |
| 73 | locationNodes.forEach(n -> { |
| 74 | ConnectPoint cp = ConnectPoint.deviceConnectPoint((n.asText())); |
| 75 | locationsSetBuilder.add(new HostLocation(cp, 0)); |
| 76 | }); |
| 77 | |
| 78 | |
| 79 | Set<HostLocation> locations = locationsSetBuilder.build(); |
Charles Chan | 60c4528 | 2017-07-11 15:07:59 -0700 | [diff] [blame] | 80 | if (locations.isEmpty()) { |
| 81 | throw new IllegalArgumentException("Host should have at least one location"); |
| 82 | } |
Szymon Giermakowski | f3578de | 2017-08-29 22:02:48 +0200 | [diff] [blame] | 83 | |
Charles Chan | 60c4528 | 2017-07-11 15:07:59 -0700 | [diff] [blame] | 84 | return locations; |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Sets the location of the host. |
| 89 | * |
Charles Chan | 61fc0d8 | 2017-04-28 14:13:36 -0700 | [diff] [blame] | 90 | * @param locations location of the host or null to unset |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 91 | * @return the config of the host |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 92 | */ |
Charles Chan | 61fc0d8 | 2017-04-28 14:13:36 -0700 | [diff] [blame] | 93 | public BasicHostConfig setLocations(Set<HostLocation> locations) { |
| 94 | return (BasicHostConfig) setOrClear(LOCATIONS, locations); |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | /** |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 98 | * Returns IP addresses of the host. |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 99 | * |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 100 | * @return IP addresses of the host or null if not set |
| 101 | * @throws IllegalArgumentException if not specified with correct format |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 102 | */ |
| 103 | public Set<IpAddress> ipAddresses() { |
| 104 | HashSet<IpAddress> ipAddresses = new HashSet<>(); |
| 105 | if (object.has(IPS)) { |
| 106 | ArrayNode ipNodes = (ArrayNode) object.path(IPS); |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 107 | ipNodes.forEach(n -> ipAddresses.add(IpAddress.valueOf(n.asText()))); |
| 108 | return ipAddresses; |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 109 | } |
| 110 | return null; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Sets the IP addresses of the host. |
| 115 | * |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 116 | * @param ipAddresses IP addresses of the host or null to unset |
| 117 | * @return the config of the host |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 118 | */ |
| 119 | public BasicHostConfig setIps(Set<IpAddress> ipAddresses) { |
| 120 | return (BasicHostConfig) setOrClear(IPS, ipAddresses); |
| 121 | } |
Szymon Giermakowski | f3578de | 2017-08-29 22:02:48 +0200 | [diff] [blame] | 122 | } |