blob: 92946312ecd5faebe300fa402c568a49d474729e [file] [log] [blame]
Thomas Vachuska96d55b12015-05-11 08:52:03 -07001/*
2 * Copyright 2015 Open Networking Laboratory
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 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 Chand6d581a2015-11-18 16:51:08 -080022import java.util.HashSet;
23import java.util.Set;
Thomas Vachuska96d55b12015-05-11 08:52:03 -070024
25/**
26 * Basic configuration for network end-station hosts.
27 */
28public class BasicHostConfig extends BasicElementConfig<HostId> {
Charles Chand6d581a2015-11-18 16:51:08 -080029 private static final String IPS = "ips";
30 private static final String LOCATION = "location";
Thomas Vachuska96d55b12015-05-11 08:52:03 -070031
Charles Chand6d581a2015-11-18 16:51:08 -080032 @Override
33 public boolean isValid() {
34 return hasOnlyFields(IPS, LOCATION) &&
35 this.location() != null &&
36 this.ipAddresses() != null;
37 }
Thomas Vachuska96d55b12015-05-11 08:52:03 -070038
Charles Chand6d581a2015-11-18 16:51:08 -080039 /**
40 * Gets location of the host.
41 *
42 * @return location of the host. Or null if not specified with correct format.
43 */
44 public ConnectPoint location() {
45 String location = get(LOCATION, null);
46
47 if (location != null) {
48 try {
49 return ConnectPoint.deviceConnectPoint(location);
50 } catch (Exception e) {
51 return null;
52 }
53 }
54 return null;
55 }
56
57 /**
58 * Sets the location of the host.
59 *
60 * @param location location of the host.
61 * @return the config of the host.
62 */
63 public BasicHostConfig setLocation(String location) {
64 return (BasicHostConfig) setOrClear(LOCATION, location);
65 }
66
67 /**
68 * Gets IP addresses of the host.
69 *
70 * @return IP addresses of the host. Or null if not specified with correct format.
71 */
72 public Set<IpAddress> ipAddresses() {
73 HashSet<IpAddress> ipAddresses = new HashSet<>();
74 if (object.has(IPS)) {
75 ArrayNode ipNodes = (ArrayNode) object.path(IPS);
76 try {
77 ipNodes.forEach(ipNode -> {
78 ipAddresses.add(IpAddress.valueOf(ipNode.asText()));
79 });
80 return ipAddresses;
81 } catch (Exception e) {
82 return null;
83 }
84 }
85 return null;
86 }
87
88 /**
89 * Sets the IP addresses of the host.
90 *
91 * @param ipAddresses IP addresses of the host.
92 * @return the config of the host.
93 */
94 public BasicHostConfig setIps(Set<IpAddress> ipAddresses) {
95 return (BasicHostConfig) setOrClear(IPS, ipAddresses);
96 }
Thomas Vachuska96d55b12015-05-11 08:52:03 -070097}