blob: bcaed6895842ed3da3545f65612dc3ba2f3d7685 [file] [log] [blame]
Thomas Vachuska96d55b12015-05-11 08:52:03 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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
Charles Chand6d581a2015-11-18 16:51:08 -080018import com.fasterxml.jackson.databind.node.ArrayNode;
19import org.onlab.packet.IpAddress;
20import org.onosproject.net.ConnectPoint;
Thomas Vachuska96d55b12015-05-11 08:52:03 -070021import org.onosproject.net.HostId;
Charles Chan61fc0d82017-04-28 14:13:36 -070022import org.onosproject.net.HostLocation;
Thomas Vachuska36008462016-01-07 15:38:20 -080023
Charles Chand6d581a2015-11-18 16:51:08 -080024import java.util.HashSet;
25import java.util.Set;
Thomas Vachuska96d55b12015-05-11 08:52:03 -070026
27/**
28 * Basic configuration for network end-station hosts.
29 */
Thomas Vachuska36008462016-01-07 15:38:20 -080030public final class BasicHostConfig extends BasicElementConfig<HostId> {
31
Charles Chand6d581a2015-11-18 16:51:08 -080032 private static final String IPS = "ips";
Charles Chan61fc0d82017-04-28 14:13:36 -070033 private static final String LOCATIONS = "locations";
Simon Hunt10618f62017-06-15 19:30:52 -070034 private static final String DASH = "-";
Thomas Vachuska96d55b12015-05-11 08:52:03 -070035
Charles Chand6d581a2015-11-18 16:51:08 -080036 @Override
37 public boolean isValid() {
Charles Chan60c45282017-07-11 15:07:59 -070038 // locations is mandatory and must have at least one
39 // ipAddresses can be absent, but if present must be valid
Charles Chan61fc0d82017-04-28 14:13:36 -070040 this.locations();
Thomas Vachuska36008462016-01-07 15:38:20 -080041 this.ipAddresses();
Simon Huntbc30e682017-02-15 18:39:23 -080042 return hasOnlyFields(ALLOWED, NAME, LOC_TYPE, LATITUDE, LONGITUDE,
Charles Chan61fc0d82017-04-28 14:13:36 -070043 GRID_Y, GRID_Y, UI_TYPE, RACK_ADDRESS, OWNER, IPS, LOCATIONS);
Charles Chand6d581a2015-11-18 16:51:08 -080044 }
Thomas Vachuska96d55b12015-05-11 08:52:03 -070045
Simon Hunt10618f62017-06-15 19:30:52 -070046 @Override
47 public String name() {
48 // NOTE:
49 // We don't want to default to host ID if friendly name is not set;
50 // (it isn't particularly friendly, e.g. "00:00:00:00:00:01/None").
51 // We'd prefer to clear the annotation, but if we pass null, then the
52 // value won't get set (see BasicElementOperator). So, instead we will
53 // return a DASH to signify "use the default friendly name".
54 return get(NAME, DASH);
55 }
56
Charles Chand6d581a2015-11-18 16:51:08 -080057 /**
Simon Hunt1e20dae2016-10-28 11:26:26 -070058 * Returns the location of the host.
Charles Chand6d581a2015-11-18 16:51:08 -080059 *
Charles Chan60c45282017-07-11 15:07:59 -070060 * @return location of the host
Thomas Vachuska36008462016-01-07 15:38:20 -080061 * @throws IllegalArgumentException if not specified with correct format
Charles Chand6d581a2015-11-18 16:51:08 -080062 */
Charles Chan61fc0d82017-04-28 14:13:36 -070063 public Set<HostLocation> locations() {
64 HashSet<HostLocation> locations = new HashSet<>();
65 if (object.has(LOCATIONS)) {
66 ArrayNode locationNodes = (ArrayNode) object.path(LOCATIONS);
67 locationNodes.forEach(n -> {
68 ConnectPoint cp = ConnectPoint.deviceConnectPoint((n.asText()));
69 locations.add(new HostLocation(cp, 0));
70 });
Charles Chan61fc0d82017-04-28 14:13:36 -070071 }
Charles Chan60c45282017-07-11 15:07:59 -070072 if (locations.isEmpty()) {
73 throw new IllegalArgumentException("Host should have at least one location");
74 }
75 return locations;
Charles Chand6d581a2015-11-18 16:51:08 -080076 }
77
78 /**
79 * Sets the location of the host.
80 *
Charles Chan61fc0d82017-04-28 14:13:36 -070081 * @param locations location of the host or null to unset
Thomas Vachuska36008462016-01-07 15:38:20 -080082 * @return the config of the host
Charles Chand6d581a2015-11-18 16:51:08 -080083 */
Charles Chan61fc0d82017-04-28 14:13:36 -070084 public BasicHostConfig setLocations(Set<HostLocation> locations) {
85 return (BasicHostConfig) setOrClear(LOCATIONS, locations);
Charles Chand6d581a2015-11-18 16:51:08 -080086 }
87
88 /**
Thomas Vachuska36008462016-01-07 15:38:20 -080089 * Returns IP addresses of the host.
Charles Chand6d581a2015-11-18 16:51:08 -080090 *
Thomas Vachuska36008462016-01-07 15:38:20 -080091 * @return IP addresses of the host or null if not set
92 * @throws IllegalArgumentException if not specified with correct format
Charles Chand6d581a2015-11-18 16:51:08 -080093 */
94 public Set<IpAddress> ipAddresses() {
95 HashSet<IpAddress> ipAddresses = new HashSet<>();
96 if (object.has(IPS)) {
97 ArrayNode ipNodes = (ArrayNode) object.path(IPS);
Thomas Vachuska36008462016-01-07 15:38:20 -080098 ipNodes.forEach(n -> ipAddresses.add(IpAddress.valueOf(n.asText())));
99 return ipAddresses;
Charles Chand6d581a2015-11-18 16:51:08 -0800100 }
101 return null;
102 }
103
104 /**
105 * Sets the IP addresses of the host.
106 *
Thomas Vachuska36008462016-01-07 15:38:20 -0800107 * @param ipAddresses IP addresses of the host or null to unset
108 * @return the config of the host
Charles Chand6d581a2015-11-18 16:51:08 -0800109 */
110 public BasicHostConfig setIps(Set<IpAddress> ipAddresses) {
111 return (BasicHostConfig) setOrClear(IPS, ipAddresses);
112 }
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700113}