blob: b0622dd533c0b12d8c6d35528c6b579c9747d976 [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();
Simon Huntc0f20c12016-05-09 09:30:20 -070048 }
49
50 /**
51 * Sets the ID of the region to which this device belongs.
52 *
53 * @param regionId region identifier
54 */
55 public void setRegionId(RegionId regionId) {
56 this.regionId = regionId;
57 }
Simon Hunt23fb1352016-04-11 12:15:19 -070058
59 @Override
Simon Huntc0f20c12016-05-09 09:30:20 -070060 public String toString() {
61 return MoreObjects.toStringHelper(this)
62 .add("id", id())
63 .add("region", regionId)
64 .toString();
Simon Hunt23fb1352016-04-11 12:15:19 -070065 }
Simon Hunt642bc452016-05-04 19:34:45 -070066
67 /**
68 * Returns the identity of the device.
69 *
70 * @return device ID
71 */
72 public DeviceId id() {
Thomas Vachuskab877a6f2017-04-14 11:43:30 -070073 return deviceId;
Simon Hunt642bc452016-05-04 19:34:45 -070074 }
75
76 @Override
77 public String idAsString() {
78 return id().toString();
79 }
Simon Huntc0f20c12016-05-09 09:30:20 -070080
81 /**
82 * Returns the device instance backing this UI device.
83 *
84 * @return the backing device instance
85 */
86 public Device backingDevice() {
Thomas Vachuskab877a6f2017-04-14 11:43:30 -070087 return topology.services.device().getDevice(deviceId);
Simon Huntc0f20c12016-05-09 09:30:20 -070088 }
89
90 /**
Simon Hunt58a0dd02016-05-17 11:54:23 -070091 * Returns the identifier of the region to which this device belongs.
92 * This will be null if the device does not belong to any region.
93 *
Simon Huntb0582492016-09-20 18:26:38 -070094 * @return region ID
Simon Hunt58a0dd02016-05-17 11:54:23 -070095 */
96 public RegionId regionId() {
97 return regionId;
98 }
99
100 /**
Simon Huntc0f20c12016-05-09 09:30:20 -0700101 * Returns the UI region to which this device belongs.
102 *
103 * @return the UI region
104 */
105 public UiRegion uiRegion() {
106 return topology.findRegion(regionId);
107 }
Simon Huntd5b96732016-07-08 13:22:27 -0700108
109 /**
110 * Returns a string representation of the type of the backing device.
111 *
112 * @return the device type
113 */
114 public String type() {
dvaddireedeaf4a2017-06-21 00:00:30 +0530115 Device device = backingDevice();
116 return device == null ? null : device.type().toString().toLowerCase();
Simon Huntd5b96732016-07-08 13:22:27 -0700117 }
Simon Hunt5f6dbf82016-03-30 08:53:33 -0700118}