blob: 74257e65673413abbe7245c9d4b8d04603b070a3 [file] [log] [blame]
Thomas Vachuska96d55b12015-05-11 08:52:03 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Thomas Vachuska96d55b12015-05-11 08:52:03 -07003 *
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 Vachuska4998caa2015-08-26 13:28:38 -070016package org.onosproject.net.config.basics;
Thomas Vachuska96d55b12015-05-11 08:52:03 -070017
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 */
23public abstract class BasicElementConfig<S> extends AllowedEntityConfig<S> {
24
Thomas Vachuska36008462016-01-07 15:38:20 -080025 protected static final String NAME = "name";
Thomas Vachuska96d55b12015-05-11 08:52:03 -070026
Thomas Vachuska36008462016-01-07 15:38:20 -080027 protected static final String LATITUDE = "latitude";
28 protected static final String LONGITUDE = "longitude";
Thomas Vachuska96d55b12015-05-11 08:52:03 -070029
Thomas Vachuska36008462016-01-07 15:38:20 -080030 protected static final String RACK_ADDRESS = "rackAddress";
31 protected static final String OWNER = "owner";
Thomas Vachuska96d55b12015-05-11 08:52:03 -070032
Simon Huntf4fd2a22016-08-10 15:41:09 -070033 protected static final double ZERO_THRESHOLD = Double.MIN_VALUE * 2.0;
34 private static final double DEFAULT_COORD = 0.0;
Ayaka Koshibeb1ffb002015-08-04 15:10:03 -070035
Thomas Vachuska96d55b12015-05-11 08:52:03 -070036 /**
37 * Returns friendly label for the element.
38 *
Simon Huntf4fd2a22016-08-10 15:41:09 -070039 * @return friendly label or element identifier itself if not set
Thomas Vachuska96d55b12015-05-11 08:52:03 -070040 */
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 Huntf4fd2a22016-08-10 15:41:09 -070055 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 Vachuska96d55b12015-05-11 08:52:03 -070069 /**
70 * Returns element latitude.
71 *
Simon Huntf4fd2a22016-08-10 15:41:09 -070072 * @return element latitude; 0.0 if (possibly) not set
73 * @see #geoCoordsSet()
Thomas Vachuska96d55b12015-05-11 08:52:03 -070074 */
75 public double latitude() {
Ayaka Koshibeb1ffb002015-08-04 15:10:03 -070076 return get(LATITUDE, DEFAULT_COORD);
Thomas Vachuska96d55b12015-05-11 08:52:03 -070077 }
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 Huntf4fd2a22016-08-10 15:41:09 -070090 * Returns element longitude.
Thomas Vachuska96d55b12015-05-11 08:52:03 -070091 *
Simon Huntf4fd2a22016-08-10 15:41:09 -070092 * @return element longitude; 0 if (possibly) not set
93 * @see #geoCoordsSet()
Thomas Vachuska96d55b12015-05-11 08:52:03 -070094 */
95 public double longitude() {
Ayaka Koshibeb1ffb002015-08-04 15:10:03 -070096 return get(LONGITUDE, DEFAULT_COORD);
Thomas Vachuska96d55b12015-05-11 08:52:03 -070097 }
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}