blob: 3758c4eb583884b465daad9b992b8a5dd5c3ff54 [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;
Thomas Vachuska36008462016-01-07 15:38:20 -080022
Charles Chand6d581a2015-11-18 16:51:08 -080023import java.util.HashSet;
24import java.util.Set;
Thomas Vachuska96d55b12015-05-11 08:52:03 -070025
26/**
27 * Basic configuration for network end-station hosts.
28 */
Thomas Vachuska36008462016-01-07 15:38:20 -080029public final class BasicHostConfig extends BasicElementConfig<HostId> {
30
Charles Chand6d581a2015-11-18 16:51:08 -080031 private static final String IPS = "ips";
32 private static final String LOCATION = "location";
Thomas Vachuska96d55b12015-05-11 08:52:03 -070033
Charles Chand6d581a2015-11-18 16:51:08 -080034 @Override
35 public boolean isValid() {
Thomas Vachuska36008462016-01-07 15:38:20 -080036 // Location and IP addresses can be absent, but if present must be valid.
37 this.location();
38 this.ipAddresses();
Simon Hunt1e20dae2016-10-28 11:26:26 -070039 return hasOnlyFields(ALLOWED, NAME, LATITUDE, LONGITUDE, UI_TYPE,
40 RACK_ADDRESS, OWNER, IPS, LOCATION);
Charles Chand6d581a2015-11-18 16:51:08 -080041 }
Thomas Vachuska96d55b12015-05-11 08:52:03 -070042
Charles Chand6d581a2015-11-18 16:51:08 -080043 /**
Simon Hunt1e20dae2016-10-28 11:26:26 -070044 * Returns the location of the host.
Charles Chand6d581a2015-11-18 16:51:08 -080045 *
Thomas Vachuska36008462016-01-07 15:38:20 -080046 * @return location of the host or null if not set
47 * @throws IllegalArgumentException if not specified with correct format
Charles Chand6d581a2015-11-18 16:51:08 -080048 */
49 public ConnectPoint location() {
50 String location = get(LOCATION, null);
Thomas Vachuska36008462016-01-07 15:38:20 -080051 return location != null ? ConnectPoint.deviceConnectPoint(location) : null;
Charles Chand6d581a2015-11-18 16:51:08 -080052 }
53
54 /**
55 * Sets the location of the host.
56 *
Thomas Vachuska36008462016-01-07 15:38:20 -080057 * @param location location of the host or null to unset
58 * @return the config of the host
Charles Chand6d581a2015-11-18 16:51:08 -080059 */
60 public BasicHostConfig setLocation(String location) {
61 return (BasicHostConfig) setOrClear(LOCATION, location);
62 }
63
64 /**
Thomas Vachuska36008462016-01-07 15:38:20 -080065 * Returns IP addresses of the host.
Charles Chand6d581a2015-11-18 16:51:08 -080066 *
Thomas Vachuska36008462016-01-07 15:38:20 -080067 * @return IP addresses of the host or null if not set
68 * @throws IllegalArgumentException if not specified with correct format
Charles Chand6d581a2015-11-18 16:51:08 -080069 */
70 public Set<IpAddress> ipAddresses() {
71 HashSet<IpAddress> ipAddresses = new HashSet<>();
72 if (object.has(IPS)) {
73 ArrayNode ipNodes = (ArrayNode) object.path(IPS);
Thomas Vachuska36008462016-01-07 15:38:20 -080074 ipNodes.forEach(n -> ipAddresses.add(IpAddress.valueOf(n.asText())));
75 return ipAddresses;
Charles Chand6d581a2015-11-18 16:51:08 -080076 }
77 return null;
78 }
79
80 /**
81 * Sets the IP addresses of the host.
82 *
Thomas Vachuska36008462016-01-07 15:38:20 -080083 * @param ipAddresses IP addresses of the host or null to unset
84 * @return the config of the host
Charles Chand6d581a2015-11-18 16:51:08 -080085 */
86 public BasicHostConfig setIps(Set<IpAddress> ipAddresses) {
87 return (BasicHostConfig) setOrClear(IPS, ipAddresses);
88 }
Thomas Vachuska96d55b12015-05-11 08:52:03 -070089}