blob: 7b870b431ba88052d4493d417ed3124d192bdea1 [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
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";
Thomas Vachuska96d55b12015-05-11 08:52:03 -070034
Charles Chand6d581a2015-11-18 16:51:08 -080035 @Override
36 public boolean isValid() {
Thomas Vachuska36008462016-01-07 15:38:20 -080037 // Location and IP addresses can be absent, but if present must be valid.
Charles Chan61fc0d82017-04-28 14:13:36 -070038 this.locations();
Thomas Vachuska36008462016-01-07 15:38:20 -080039 this.ipAddresses();
Simon Huntbc30e682017-02-15 18:39:23 -080040 return hasOnlyFields(ALLOWED, NAME, LOC_TYPE, LATITUDE, LONGITUDE,
Charles Chan61fc0d82017-04-28 14:13:36 -070041 GRID_Y, GRID_Y, UI_TYPE, RACK_ADDRESS, OWNER, IPS, LOCATIONS);
Charles Chand6d581a2015-11-18 16:51:08 -080042 }
Thomas Vachuska96d55b12015-05-11 08:52:03 -070043
Charles Chand6d581a2015-11-18 16:51:08 -080044 /**
Simon Hunt1e20dae2016-10-28 11:26:26 -070045 * Returns the location of the host.
Charles Chand6d581a2015-11-18 16:51:08 -080046 *
Thomas Vachuska36008462016-01-07 15:38:20 -080047 * @return location of the host or null if not set
48 * @throws IllegalArgumentException if not specified with correct format
Charles Chand6d581a2015-11-18 16:51:08 -080049 */
Charles Chan61fc0d82017-04-28 14:13:36 -070050 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 Chand6d581a2015-11-18 16:51:08 -080061 }
62
63 /**
64 * Sets the location of the host.
65 *
Charles Chan61fc0d82017-04-28 14:13:36 -070066 * @param locations location of the host or null to unset
Thomas Vachuska36008462016-01-07 15:38:20 -080067 * @return the config of the host
Charles Chand6d581a2015-11-18 16:51:08 -080068 */
Charles Chan61fc0d82017-04-28 14:13:36 -070069 public BasicHostConfig setLocations(Set<HostLocation> locations) {
70 return (BasicHostConfig) setOrClear(LOCATIONS, locations);
Charles Chand6d581a2015-11-18 16:51:08 -080071 }
72
73 /**
Thomas Vachuska36008462016-01-07 15:38:20 -080074 * Returns IP addresses of the host.
Charles Chand6d581a2015-11-18 16:51:08 -080075 *
Thomas Vachuska36008462016-01-07 15:38:20 -080076 * @return IP addresses of the host or null if not set
77 * @throws IllegalArgumentException if not specified with correct format
Charles Chand6d581a2015-11-18 16:51:08 -080078 */
79 public Set<IpAddress> ipAddresses() {
80 HashSet<IpAddress> ipAddresses = new HashSet<>();
81 if (object.has(IPS)) {
82 ArrayNode ipNodes = (ArrayNode) object.path(IPS);
Thomas Vachuska36008462016-01-07 15:38:20 -080083 ipNodes.forEach(n -> ipAddresses.add(IpAddress.valueOf(n.asText())));
84 return ipAddresses;
Charles Chand6d581a2015-11-18 16:51:08 -080085 }
86 return null;
87 }
88
89 /**
90 * Sets the IP addresses of the host.
91 *
Thomas Vachuska36008462016-01-07 15:38:20 -080092 * @param ipAddresses IP addresses of the host or null to unset
93 * @return the config of the host
Charles Chand6d581a2015-11-18 16:51:08 -080094 */
95 public BasicHostConfig setIps(Set<IpAddress> ipAddresses) {
96 return (BasicHostConfig) setOrClear(IPS, ipAddresses);
97 }
Thomas Vachuska96d55b12015-05-11 08:52:03 -070098}