blob: 2d15fa58d5aa78011044575f6cb2af6b831d70df [file] [log] [blame]
Simon Huntf59d36b2016-10-04 19:05:53 -07001/*
2 * Copyright 2016-present 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 */
16
17package org.onosproject.net.config.basics;
18
Simon Hunt4f3a4072016-10-17 17:52:11 -070019import com.google.common.base.MoreObjects;
Simon Huntf59d36b2016-10-04 19:05:53 -070020import org.onosproject.net.DeviceId;
21import org.onosproject.net.config.Config;
22import org.onosproject.net.region.Region;
23import org.onosproject.net.region.RegionId;
24
25import java.util.List;
Simon Hunt4f3a4072016-10-17 17:52:11 -070026import java.util.Set;
Simon Huntf59d36b2016-10-04 19:05:53 -070027
28/**
29 * Basic configuration for network regions.
30 */
31public final class BasicRegionConfig extends Config<RegionId> {
32
Simon Hunt4f3a4072016-10-17 17:52:11 -070033 private static final String NAME = "name";
Simon Huntf59d36b2016-10-04 19:05:53 -070034 private static final String TYPE = "type";
35 private static final String DEVICES = "devices";
36
37 @Override
38 public boolean isValid() {
Simon Hunt4f3a4072016-10-17 17:52:11 -070039 return hasOnlyFields(NAME, TYPE, DEVICES);
40 }
41
42 @Override
43 public String toString() {
44 return MoreObjects.toStringHelper(this)
45 .add("name", name())
46 .add("type", type())
47 .add("devices", devices())
48 .toString();
49 }
50
51 /**
52 * Returns the region name.
53 *
54 * @return the region name
55 */
56 public String name() {
57 return get(NAME, null);
58 }
59
60 /**
61 * Sets the name of this region.
62 *
63 * @param name name of region, or null to unset
64 * @return the config of the region
65 */
66 public BasicRegionConfig name(String name) {
67 return (BasicRegionConfig) setOrClear(NAME, name);
Simon Huntf59d36b2016-10-04 19:05:53 -070068 }
69
70 /**
71 * Returns the region type.
72 *
73 * @return the region type
74 */
Simon Hunt4f3a4072016-10-17 17:52:11 -070075 public Region.Type type() {
Simon Huntf59d36b2016-10-04 19:05:53 -070076 String t = get(TYPE, null);
77 return t == null ? null : regionTypeFor(t);
78 }
79
80 private Region.Type regionTypeFor(String t) {
81 try {
82 return Region.Type.valueOf(t.toUpperCase());
83 } catch (IllegalArgumentException ignored) {
84 }
85 return null;
86 }
87
88 /**
Simon Hunt4f3a4072016-10-17 17:52:11 -070089 * Sets the region type.
90 *
91 * @param type the region type, or null to unset
92 * @return the config of the region
93 */
94 public BasicRegionConfig type(Region.Type type) {
95 String t = type == null ? null : type.name().toLowerCase();
96 return (BasicRegionConfig) setOrClear(TYPE, t);
97 }
98
99 /**
Simon Huntf59d36b2016-10-04 19:05:53 -0700100 * Returns the identities of the devices in this region.
101 *
102 * @return list of device identifiers
103 */
Simon Hunt4f3a4072016-10-17 17:52:11 -0700104 public List<DeviceId> devices() {
105 return object.has(DEVICES) ? getList(DEVICES, DeviceId::deviceId) : null;
Simon Huntf59d36b2016-10-04 19:05:53 -0700106 }
107
Simon Hunt4f3a4072016-10-17 17:52:11 -0700108 /**
109 * Sets the devices of this region.
110 *
111 * @param devices the device identifiers, or null to unset
112 * @return the config of the region
113 */
114 public BasicRegionConfig devices(Set<DeviceId> devices) {
115 return (BasicRegionConfig) setOrClear(DEVICES, devices);
116 }
Simon Huntf59d36b2016-10-04 19:05:53 -0700117}