blob: 420768fa46d26472254ae10a106bcfb9b557383d [file] [log] [blame]
Simon Huntcda9c032016-04-11 10:32:54 -07001/*
2 * Copyright 2016 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.ui.impl.topo.model;
18
Simon Hunt23fb1352016-04-11 12:15:19 -070019import org.onosproject.cluster.ControllerNode;
Simon Hunt642bc452016-05-04 19:34:45 -070020import org.onosproject.cluster.NodeId;
Simon Hunt23fb1352016-04-11 12:15:19 -070021import org.onosproject.cluster.RoleInfo;
Simon Huntcda9c032016-04-11 10:32:54 -070022import org.onosproject.event.EventDispatcher;
23import org.onosproject.net.Device;
Simon Hunt23fb1352016-04-11 12:15:19 -070024import org.onosproject.net.DeviceId;
25import org.onosproject.net.Host;
26import org.onosproject.net.Link;
27import org.onosproject.net.region.Region;
Simon Hunt642bc452016-05-04 19:34:45 -070028import org.onosproject.ui.model.ServiceBundle;
Simon Hunt338a3b42016-04-14 09:43:52 -070029import org.onosproject.ui.model.topo.UiClusterMember;
Simon Huntcda9c032016-04-11 10:32:54 -070030import org.onosproject.ui.model.topo.UiDevice;
Simon Hunt23fb1352016-04-11 12:15:19 -070031import org.onosproject.ui.model.topo.UiTopology;
Simon Hunt642bc452016-05-04 19:34:45 -070032import org.slf4j.Logger;
33import org.slf4j.LoggerFactory;
Simon Hunt23fb1352016-04-11 12:15:19 -070034
Simon Hunt642bc452016-05-04 19:34:45 -070035import static org.onosproject.ui.impl.topo.model.UiModelEvent.Type.CLUSTER_MEMBER_ADDED_OR_UPDATED;
36import static org.onosproject.ui.impl.topo.model.UiModelEvent.Type.CLUSTER_MEMBER_REMOVED;
37import static org.onosproject.ui.impl.topo.model.UiModelEvent.Type.DEVICE_ADDED_OR_UPDATED;
Simon Hunt23fb1352016-04-11 12:15:19 -070038import static org.onosproject.ui.impl.topo.model.UiModelEvent.Type.DEVICE_REMOVED;
Simon Huntcda9c032016-04-11 10:32:54 -070039
40/**
41 * UI Topology Model cache.
42 */
43class ModelCache {
44
Simon Hunt642bc452016-05-04 19:34:45 -070045 private static final Logger log = LoggerFactory.getLogger(ModelCache.class);
46
47 private final ServiceBundle services;
Simon Huntcda9c032016-04-11 10:32:54 -070048 private final EventDispatcher dispatcher;
Simon Hunt23fb1352016-04-11 12:15:19 -070049 private final UiTopology uiTopology = new UiTopology();
Simon Huntcda9c032016-04-11 10:32:54 -070050
Simon Hunt642bc452016-05-04 19:34:45 -070051 ModelCache(ServiceBundle services, EventDispatcher eventDispatcher) {
52 this.services = services;
Simon Huntcda9c032016-04-11 10:32:54 -070053 this.dispatcher = eventDispatcher;
54 }
55
Simon Hunt338a3b42016-04-14 09:43:52 -070056 @Override
57 public String toString() {
58 return "ModelCache{" + uiTopology + "}";
59 }
60
Simon Huntcda9c032016-04-11 10:32:54 -070061 /**
62 * Clear our model.
63 */
64 void clear() {
Simon Hunt23fb1352016-04-11 12:15:19 -070065 uiTopology.clear();
Simon Huntcda9c032016-04-11 10:32:54 -070066 }
67
68 /**
69 * Create our internal model of the global topology.
70 */
71 void load() {
Simon Hunt642bc452016-05-04 19:34:45 -070072 // TODO - implement loading of initial state
Simon Huntcda9c032016-04-11 10:32:54 -070073// loadClusterMembers();
74// loadRegions();
75// loadDevices();
76// loadHosts();
77// loadLinks();
78 }
79
80
Simon Hunt338a3b42016-04-14 09:43:52 -070081 /**
82 * Updates the model (adds a new instance if necessary) with the given
83 * controller node information.
84 *
85 * @param cnode controller node to be added/updated
86 */
87 void addOrUpdateClusterMember(ControllerNode cnode) {
Simon Hunt642bc452016-05-04 19:34:45 -070088 NodeId id = cnode.id();
89 UiClusterMember member = uiTopology.findClusterMember(id);
90 if (member == null) {
Simon Hunt338a3b42016-04-14 09:43:52 -070091 member = new UiClusterMember(cnode);
92 uiTopology.add(member);
93 }
94
Simon Hunt642bc452016-05-04 19:34:45 -070095 // inject computed data about the cluster node, into the model object
96 ControllerNode.State state = services.cluster().getState(id);
97 member.setState(state);
98 member.setDeviceCount(services.mastership().getDevicesOf(id).size());
99 // NOTE: UI-attached is session-based data, not global
100
101 dispatcher.post(new UiModelEvent(CLUSTER_MEMBER_ADDED_OR_UPDATED, member));
Simon Hunt338a3b42016-04-14 09:43:52 -0700102 }
103
Simon Hunt642bc452016-05-04 19:34:45 -0700104 /**
105 * Removes from the model the specified controller node.
106 *
107 * @param cnode controller node to be removed
108 */
Simon Hunt338a3b42016-04-14 09:43:52 -0700109 void removeClusterMember(ControllerNode cnode) {
Simon Hunt642bc452016-05-04 19:34:45 -0700110 NodeId id = cnode.id();
111 UiClusterMember member = uiTopology.findClusterMember(id);
112 if (member != null) {
113 uiTopology.remove(member);
114 dispatcher.post(new UiModelEvent(CLUSTER_MEMBER_REMOVED, member));
115 } else {
116 log.warn("Tried to remove non-member cluster node {}", id);
117 }
Simon Hunt338a3b42016-04-14 09:43:52 -0700118 }
119
120 void updateMasterships(DeviceId deviceId, RoleInfo roleInfo) {
121 // TODO: store the updated mastership information
122 // TODO: post event
123 }
124
125 void addOrUpdateRegion(Region region) {
126 // TODO: find or create region assoc. with parameter
127 // TODO: post event
128 }
129
130 void removeRegion(Region region) {
131 // TODO: find region assoc. with parameter; remove from model
132 // TODO: post event
133 }
134
Simon Huntcda9c032016-04-11 10:32:54 -0700135 void addOrUpdateDevice(Device device) {
Simon Hunt23fb1352016-04-11 12:15:19 -0700136 // TODO: find or create device assoc. with parameter
137 // FIXME
Simon Huntcda9c032016-04-11 10:32:54 -0700138 UiDevice uiDevice = new UiDevice();
139
Simon Hunt23fb1352016-04-11 12:15:19 -0700140 // TODO: post the (correct) event
Simon Hunt642bc452016-05-04 19:34:45 -0700141 dispatcher.post(new UiModelEvent(DEVICE_ADDED_OR_UPDATED, uiDevice));
Simon Huntcda9c032016-04-11 10:32:54 -0700142 }
143
144 void removeDevice(Device device) {
Simon Hunt23fb1352016-04-11 12:15:19 -0700145 // TODO: get UiDevice associated with the given parameter; remove from model
146 // FIXME
Simon Huntcda9c032016-04-11 10:32:54 -0700147 UiDevice uiDevice = new UiDevice();
148
Simon Hunt23fb1352016-04-11 12:15:19 -0700149 // TODO: post the (correct) event
150 dispatcher.post(new UiModelEvent(DEVICE_REMOVED, uiDevice));
Simon Huntcda9c032016-04-11 10:32:54 -0700151
152 }
153
Simon Hunt23fb1352016-04-11 12:15:19 -0700154 void addOrUpdateLink(Link link) {
155 // TODO: find ui-link assoc. with parameter; create or update.
156 // TODO: post event
157 }
158
159 void removeLink(Link link) {
160 // TODO: find ui-link assoc. with parameter; update or remove.
161 // TODO: post event
162 }
163
164 void addOrUpdateHost(Host host) {
165 // TODO: find or create host assoc. with parameter
166 // TODO: post event
167 }
168
169 void moveHost(Host host, Host prevHost) {
170 // TODO: process host-move
171 // TODO: post event
172 }
173
174 void removeHost(Host host) {
175 // TODO: find host assoc. with parameter; remove from model
176 }
Simon Hunt338a3b42016-04-14 09:43:52 -0700177
178 /**
179 * Returns the number of members in the cluster.
180 *
181 * @return number of cluster members
182 */
183 public int clusterMemberCount() {
184 return uiTopology.clusterMemberCount();
185 }
186
187 /**
188 * Returns the number of regions configured in the topology.
189 *
190 * @return number of regions
191 */
192 public int regionCount() {
193 return uiTopology.regionCount();
194 }
Simon Huntcda9c032016-04-11 10:32:54 -0700195}