blob: 15a0519db02a29427587eaa7f4ccbcf8ecae7324 [file] [log] [blame]
Simon Hunt5f6dbf82016-03-30 08:53:33 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Simon Hunt5f6dbf82016-03-30 08:53:33 -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 */
16
17package org.onosproject.ui.model.topo;
18
Simon Huntc0f20c12016-05-09 09:30:20 -070019import com.google.common.base.MoreObjects;
Simon Hunt23fb1352016-04-11 12:15:19 -070020import org.onosproject.net.Device;
Simon Hunt642bc452016-05-04 19:34:45 -070021import org.onosproject.net.DeviceId;
Simon Huntc0f20c12016-05-09 09:30:20 -070022import org.onosproject.net.region.RegionId;
Simon Hunt23fb1352016-04-11 12:15:19 -070023
Thomas Vachuskab877a6f2017-04-14 11:43:30 -070024import static com.google.common.base.Preconditions.checkNotNull;
25
Simon Hunt5f6dbf82016-03-30 08:53:33 -070026/**
27 * Represents a device.
28 */
29public class UiDevice extends UiNode {
Simon Hunt23fb1352016-04-11 12:15:19 -070030
Thomas Vachuskab877a6f2017-04-14 11:43:30 -070031 private static final String DEVICE_CANNOT_BE_NULL = "Device cannot be null";
32
Simon Huntc0f20c12016-05-09 09:30:20 -070033 private final UiTopology topology;
Thomas Vachuskab877a6f2017-04-14 11:43:30 -070034 private final DeviceId deviceId;
Simon Huntc0f20c12016-05-09 09:30:20 -070035
36 private RegionId regionId;
37
38 /**
39 * Creates a new UI device.
40 *
41 * @param topology parent topology
42 * @param device backing device
43 */
44 public UiDevice(UiTopology topology, Device device) {
Thomas Vachuskab877a6f2017-04-14 11:43:30 -070045 checkNotNull(device, DEVICE_CANNOT_BE_NULL);
Simon Huntc0f20c12016-05-09 09:30:20 -070046 this.topology = topology;
Thomas Vachuskab877a6f2017-04-14 11:43:30 -070047 this.deviceId = device.id();
Sean Condon28884332019-03-21 14:07:00 +000048 this.regionId = RegionId.regionId(UiRegion.NULL_NAME);
Simon Huntc0f20c12016-05-09 09:30:20 -070049 }
50
51 /**
52 * Sets the ID of the region to which this device belongs.
53 *
54 * @param regionId region identifier
55 */
56 public void setRegionId(RegionId regionId) {
57 this.regionId = regionId;
58 }
Simon Hunt23fb1352016-04-11 12:15:19 -070059
60 @Override
Simon Huntc0f20c12016-05-09 09:30:20 -070061 public String toString() {
62 return MoreObjects.toStringHelper(this)
63 .add("id", id())
64 .add("region", regionId)
65 .toString();
Simon Hunt23fb1352016-04-11 12:15:19 -070066 }
Simon Hunt642bc452016-05-04 19:34:45 -070067
68 /**
69 * Returns the identity of the device.
70 *
71 * @return device ID
72 */
73 public DeviceId id() {
Thomas Vachuskab877a6f2017-04-14 11:43:30 -070074 return deviceId;
Simon Hunt642bc452016-05-04 19:34:45 -070075 }
76
77 @Override
78 public String idAsString() {
79 return id().toString();
80 }
Simon Huntc0f20c12016-05-09 09:30:20 -070081
82 /**
83 * Returns the device instance backing this UI device.
84 *
85 * @return the backing device instance
86 */
87 public Device backingDevice() {
Thomas Vachuskab877a6f2017-04-14 11:43:30 -070088 return topology.services.device().getDevice(deviceId);
Simon Huntc0f20c12016-05-09 09:30:20 -070089 }
90
91 /**
Simon Hunt58a0dd02016-05-17 11:54:23 -070092 * Returns the identifier of the region to which this device belongs.
93 * This will be null if the device does not belong to any region.
94 *
Simon Huntb0582492016-09-20 18:26:38 -070095 * @return region ID
Simon Hunt58a0dd02016-05-17 11:54:23 -070096 */
97 public RegionId regionId() {
98 return regionId;
99 }
100
101 /**
Simon Huntc0f20c12016-05-09 09:30:20 -0700102 * Returns the UI region to which this device belongs.
103 *
104 * @return the UI region
105 */
106 public UiRegion uiRegion() {
107 return topology.findRegion(regionId);
108 }
Simon Huntd5b96732016-07-08 13:22:27 -0700109
110 /**
111 * Returns a string representation of the type of the backing device.
112 *
113 * @return the device type
114 */
115 public String type() {
dvaddireedeaf4a2017-06-21 00:00:30 +0530116 Device device = backingDevice();
117 return device == null ? null : device.type().toString().toLowerCase();
Simon Huntd5b96732016-07-08 13:22:27 -0700118 }
Simon Hunt5f6dbf82016-03-30 08:53:33 -0700119}