blob: c029c2b403347a7eae2e8104fccf350fc6808936 [file] [log] [blame]
Simon Hunt5f6dbf82016-03-30 08:53:33 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
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 Huntd5b96732016-07-08 13:22:27 -070020import org.onosproject.cluster.NodeId;
Simon Hunt23fb1352016-04-11 12:15:19 -070021import org.onosproject.net.Device;
Simon Hunt642bc452016-05-04 19:34:45 -070022import org.onosproject.net.DeviceId;
Simon Huntc0f20c12016-05-09 09:30:20 -070023import org.onosproject.net.region.RegionId;
Simon Hunt23fb1352016-04-11 12:15:19 -070024
Simon Hunt5f6dbf82016-03-30 08:53:33 -070025/**
26 * Represents a device.
27 */
28public class UiDevice extends UiNode {
Simon Hunt23fb1352016-04-11 12:15:19 -070029
Simon Huntc0f20c12016-05-09 09:30:20 -070030 private final UiTopology topology;
31 private final Device device;
32
33 private RegionId regionId;
Simon Huntd5b96732016-07-08 13:22:27 -070034 private NodeId masterId;
35 private boolean online;
Simon Huntc0f20c12016-05-09 09:30:20 -070036
37 /**
38 * Creates a new UI device.
39 *
40 * @param topology parent topology
41 * @param device backing device
42 */
43 public UiDevice(UiTopology topology, Device device) {
44 this.topology = topology;
45 this.device = device;
46 }
47
48 /**
49 * Sets the ID of the region to which this device belongs.
50 *
51 * @param regionId region identifier
52 */
53 public void setRegionId(RegionId regionId) {
54 this.regionId = regionId;
55 }
Simon Hunt23fb1352016-04-11 12:15:19 -070056
Simon Huntd5b96732016-07-08 13:22:27 -070057 /**
58 * Sets the ID of the controller node that holds mastership for this device.
59 *
60 * @param masterId master identifier
61 */
62 public void setMasterId(NodeId masterId) {
63 this.masterId = masterId;
64 }
65
66 /**
67 * Sets a flag indicating whether the backing device is online.
68 *
69 * @param online boolen flag
70 */
71 public void setOnline(boolean online) {
72 this.online = online;
73 }
74
Simon Hunt23fb1352016-04-11 12:15:19 -070075 @Override
Simon Huntc0f20c12016-05-09 09:30:20 -070076 public String toString() {
77 return MoreObjects.toStringHelper(this)
78 .add("id", id())
79 .add("region", regionId)
80 .toString();
Simon Hunt23fb1352016-04-11 12:15:19 -070081 }
Simon Hunt642bc452016-05-04 19:34:45 -070082
Simon Huntc0f20c12016-05-09 09:30:20 -070083 // @Override
84// protected void destroy() {
85// }
86
Simon Hunt642bc452016-05-04 19:34:45 -070087 /**
88 * Returns the identity of the device.
89 *
90 * @return device ID
91 */
92 public DeviceId id() {
93 return device.id();
94 }
95
96 @Override
97 public String idAsString() {
98 return id().toString();
99 }
Simon Huntc0f20c12016-05-09 09:30:20 -0700100
101 /**
102 * Returns the device instance backing this UI device.
103 *
104 * @return the backing device instance
105 */
106 public Device backingDevice() {
107 return device;
108 }
109
110 /**
Simon Hunt58a0dd02016-05-17 11:54:23 -0700111 * Returns the identifier of the region to which this device belongs.
112 * This will be null if the device does not belong to any region.
113 *
114 * @return region identity
115 */
116 public RegionId regionId() {
117 return regionId;
118 }
119
120 /**
Simon Huntc0f20c12016-05-09 09:30:20 -0700121 * Returns the UI region to which this device belongs.
122 *
123 * @return the UI region
124 */
125 public UiRegion uiRegion() {
126 return topology.findRegion(regionId);
127 }
Simon Huntd5b96732016-07-08 13:22:27 -0700128
129 /**
130 * Returns a string representation of the type of the backing device.
131 *
132 * @return the device type
133 */
134 public String type() {
135 return device.type().toString().toLowerCase();
136 }
137
138 /**
139 * Returns a boolean indicating whether the backing device is online.
140 *
141 * @return true if device is online, false otherwise
142 */
143 public boolean isOnline() {
144 return online;
145 }
146
147 /**
148 * Returns the identifier for the cluster member that has
149 * mastership over this device.
150 *
151 * @return master cluster member identifier
152 */
153 public NodeId master() {
154 return masterId;
155 }
Simon Hunt5f6dbf82016-03-30 08:53:33 -0700156}