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 | |
| 18 | /** |
| 19 | * Basic configuration for network elements, e.g. devices, hosts. Such elements |
| 20 | * can have a friendly name, geo-coordinates, logical rack coordinates and |
| 21 | * an owner entity. |
| 22 | */ |
| 23 | public abstract class BasicElementConfig<S> extends AllowedEntityConfig<S> { |
| 24 | |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 25 | protected static final String NAME = "name"; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 26 | |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 27 | protected static final String LATITUDE = "latitude"; |
| 28 | protected static final String LONGITUDE = "longitude"; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 29 | |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 30 | protected static final String RACK_ADDRESS = "rackAddress"; |
| 31 | protected static final String OWNER = "owner"; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 32 | |
Simon Hunt | f4fd2a2 | 2016-08-10 15:41:09 -0700 | [diff] [blame] | 33 | protected static final double ZERO_THRESHOLD = Double.MIN_VALUE * 2.0; |
| 34 | private static final double DEFAULT_COORD = 0.0; |
Ayaka Koshibe | b1ffb00 | 2015-08-04 15:10:03 -0700 | [diff] [blame] | 35 | |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 36 | /** |
| 37 | * Returns friendly label for the element. |
| 38 | * |
Simon Hunt | f4fd2a2 | 2016-08-10 15:41:09 -0700 | [diff] [blame] | 39 | * @return friendly label or element identifier itself if not set |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 40 | */ |
| 41 | public String name() { |
| 42 | return get(NAME, subject.toString()); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Sets friendly label for the element. |
| 47 | * |
| 48 | * @param name new friendly label; null to clear |
| 49 | * @return self |
| 50 | */ |
| 51 | public BasicElementConfig name(String name) { |
| 52 | return (BasicElementConfig) setOrClear(NAME, name); |
| 53 | } |
| 54 | |
Simon Hunt | f4fd2a2 | 2016-08-10 15:41:09 -0700 | [diff] [blame] | 55 | private static boolean doubleIsZero(double value) { |
| 56 | return value >= -ZERO_THRESHOLD && value <= ZERO_THRESHOLD; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Returns true if the geographical coordinates (latitude and longitude) |
| 61 | * are set on this element. |
| 62 | * |
| 63 | * @return true if geo-coordinates are set |
| 64 | */ |
| 65 | public boolean geoCoordsSet() { |
| 66 | return !doubleIsZero(latitude()) || !doubleIsZero(longitude()); |
| 67 | } |
| 68 | |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 69 | /** |
| 70 | * Returns element latitude. |
| 71 | * |
Simon Hunt | f4fd2a2 | 2016-08-10 15:41:09 -0700 | [diff] [blame] | 72 | * @return element latitude; 0.0 if (possibly) not set |
| 73 | * @see #geoCoordsSet() |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 74 | */ |
| 75 | public double latitude() { |
Ayaka Koshibe | b1ffb00 | 2015-08-04 15:10:03 -0700 | [diff] [blame] | 76 | return get(LATITUDE, DEFAULT_COORD); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Sets the element latitude. |
| 81 | * |
| 82 | * @param latitude new latitude; null to clear |
| 83 | * @return self |
| 84 | */ |
| 85 | public BasicElementConfig latitude(Double latitude) { |
| 86 | return (BasicElementConfig) setOrClear(LATITUDE, latitude); |
| 87 | } |
| 88 | |
| 89 | /** |
Simon Hunt | f4fd2a2 | 2016-08-10 15:41:09 -0700 | [diff] [blame] | 90 | * Returns element longitude. |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 91 | * |
Simon Hunt | f4fd2a2 | 2016-08-10 15:41:09 -0700 | [diff] [blame] | 92 | * @return element longitude; 0 if (possibly) not set |
| 93 | * @see #geoCoordsSet() |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 94 | */ |
| 95 | public double longitude() { |
Ayaka Koshibe | b1ffb00 | 2015-08-04 15:10:03 -0700 | [diff] [blame] | 96 | return get(LONGITUDE, DEFAULT_COORD); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Sets the element longitude. |
| 101 | * |
| 102 | * @param longitude new longitude; null to clear |
| 103 | * @return self |
| 104 | */ |
| 105 | public BasicElementConfig longitude(Double longitude) { |
| 106 | return (BasicElementConfig) setOrClear(LONGITUDE, longitude); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Returns the element rack address. |
| 111 | * |
| 112 | * @return rack address; null if not set |
| 113 | */ |
| 114 | public String rackAddress() { |
| 115 | return get(RACK_ADDRESS, null); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Sets element rack address. |
| 120 | * |
| 121 | * @param address new rack address; null to clear |
| 122 | * @return self |
| 123 | */ |
| 124 | public BasicElementConfig rackAddress(String address) { |
| 125 | return (BasicElementConfig) setOrClear(RACK_ADDRESS, address); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Returns owner of the element. |
| 130 | * |
| 131 | * @return owner or null if not set |
| 132 | */ |
| 133 | public String owner() { |
| 134 | return get(OWNER, null); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Sets the owner of the element. |
| 139 | * |
| 140 | * @param owner new owner; null to clear |
| 141 | * @return self |
| 142 | */ |
| 143 | public BasicElementConfig owner(String owner) { |
| 144 | return (BasicElementConfig) setOrClear(OWNER, owner); |
| 145 | } |
| 146 | |
| 147 | } |