blob: e27157ea488ec8578b9fa5606dcee11e597e823b [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;
Simon Huntf59d36b2016-10-04 19:05:53 -070021import org.onosproject.net.region.Region;
22import org.onosproject.net.region.RegionId;
23
24import java.util.List;
Simon Hunt4f3a4072016-10-17 17:52:11 -070025import java.util.Set;
Simon Huntf59d36b2016-10-04 19:05:53 -070026
27/**
28 * Basic configuration for network regions.
29 */
Simon Hunt53612212016-12-04 17:19:52 -080030public final class BasicRegionConfig extends BasicElementConfig<RegionId> {
Simon Huntf59d36b2016-10-04 19:05:53 -070031
32 private static final String TYPE = "type";
33 private static final String DEVICES = "devices";
34
35 @Override
36 public boolean isValid() {
Simon Hunt53612212016-12-04 17:19:52 -080037 return hasOnlyFields(ALLOWED, NAME, LATITUDE, LONGITUDE, UI_TYPE,
38 RACK_ADDRESS, OWNER, TYPE, DEVICES);
Simon Hunt4f3a4072016-10-17 17:52:11 -070039 }
40
41 @Override
42 public String toString() {
43 return MoreObjects.toStringHelper(this)
Simon Hunt53612212016-12-04 17:19:52 -080044 .add(NAME, name())
45 .add(TYPE, type())
46 .add(UI_TYPE, uiType())
47 .add(LATITUDE, latitude())
48 .add(LONGITUDE, longitude())
49 .add(DEVICES, devices())
Simon Hunt4f3a4072016-10-17 17:52:11 -070050 .toString();
51 }
52
53 /**
Simon Huntf59d36b2016-10-04 19:05:53 -070054 * Returns the region type.
55 *
56 * @return the region type
57 */
Simon Hunt4f3a4072016-10-17 17:52:11 -070058 public Region.Type type() {
Simon Huntf59d36b2016-10-04 19:05:53 -070059 String t = get(TYPE, null);
60 return t == null ? null : regionTypeFor(t);
61 }
62
63 private Region.Type regionTypeFor(String t) {
64 try {
65 return Region.Type.valueOf(t.toUpperCase());
66 } catch (IllegalArgumentException ignored) {
67 }
68 return null;
69 }
70
71 /**
Simon Hunt4f3a4072016-10-17 17:52:11 -070072 * Sets the region type.
73 *
74 * @param type the region type, or null to unset
75 * @return the config of the region
76 */
77 public BasicRegionConfig type(Region.Type type) {
78 String t = type == null ? null : type.name().toLowerCase();
79 return (BasicRegionConfig) setOrClear(TYPE, t);
80 }
81
82 /**
Simon Huntf59d36b2016-10-04 19:05:53 -070083 * Returns the identities of the devices in this region.
84 *
85 * @return list of device identifiers
86 */
Simon Hunt4f3a4072016-10-17 17:52:11 -070087 public List<DeviceId> devices() {
88 return object.has(DEVICES) ? getList(DEVICES, DeviceId::deviceId) : null;
Simon Huntf59d36b2016-10-04 19:05:53 -070089 }
90
Simon Hunt4f3a4072016-10-17 17:52:11 -070091 /**
92 * Sets the devices of this region.
93 *
94 * @param devices the device identifiers, or null to unset
95 * @return the config of the region
96 */
97 public BasicRegionConfig devices(Set<DeviceId> devices) {
98 return (BasicRegionConfig) setOrClear(DEVICES, devices);
99 }
Simon Huntf59d36b2016-10-04 19:05:53 -0700100}