blob: 09c6ce77cc2c3c7dc2471fade3ad196ff20b0b1c [file] [log] [blame]
Steven Burrows57e24e92016-08-04 18:38:24 +01001/*
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
17/*
18 ONOS GUI -- Topology Region Module.
19 Module that holds the current region in memory
20 */
21
22(function () {
23 'use strict';
24
Steven Burrows6deb4ce2016-08-26 16:06:23 +010025 // Injected Services
Steven Burrowsdfa52b02016-09-02 13:50:43 +010026 var Model;
Steven Burrows57e24e92016-08-04 18:38:24 +010027
Steven Burrowsdfa52b02016-09-02 13:50:43 +010028 // Internal
Steven Burrowsb15a3942017-01-17 17:25:04 +000029 var instance;
Steven Burrows57e24e92016-08-04 18:38:24 +010030
Steven Burrows8ea5dea2016-12-27 13:28:41 +000031 // 'static' vars
32 var ROOT = '(root)';
33
Steven Burrows57e24e92016-08-04 18:38:24 +010034 angular.module('ovTopo2')
Steven Burrowsaf96a212016-12-28 12:57:02 +000035 .factory('Topo2RegionService', [
36 '$log', 'Topo2Model', 'Topo2SubRegionService', 'Topo2DeviceService',
Steven Burrows1aa4f582016-12-13 15:05:41 -050037 'Topo2HostService', 'Topo2LinkService', 'Topo2ZoomService', 'Topo2DetailsPanelService',
Steven Burrowsaf96a212016-12-28 12:57:02 +000038 'Topo2BreadcrumbService', 'Topo2ViewController',
39 function ($log, _Model_, t2sr, t2ds, t2hs, t2ls, t2zs, t2dps, t2bcs, ViewController) {
Steven Burrows57e24e92016-08-04 18:38:24 +010040
Steven Burrowsdfa52b02016-09-02 13:50:43 +010041 Model = _Model_;
Steven Burrows57e24e92016-08-04 18:38:24 +010042
Steven Burrowsaf96a212016-12-28 12:57:02 +000043 var Region = ViewController.extend({
44 initialize: function () {
45 instance = this;
46 this.model = null;
47 },
48 addRegion: function (data) {
Steven Burrows57e24e92016-08-04 18:38:24 +010049
Steven Burrowsaf96a212016-12-28 12:57:02 +000050 var RegionModel = Model.extend({
51 findNodeById: this.findNodeById,
52 nodes: this.regionNodes.bind(this)
53 });
Steven Burrowsec1f45c2016-08-08 16:14:41 +010054
Steven Burrowsaf96a212016-12-28 12:57:02 +000055 this.model = new RegionModel({
56 id: data.id,
57 layerOrder: data.layerOrder
58 });
59
60 this.model.set({
61 subregions: t2sr.createSubRegionCollection(data.subregions, this),
62 devices: t2ds.createDeviceCollection(data.devices, this),
63 hosts: t2hs.createHostCollection(data.hosts, this),
64 links: t2ls.createLinkCollection(data.links, this)
65 });
66
67 angular.forEach(this.model.get('links').models, function (link) {
68 link.createLink();
69 });
70
71 // Hide Breadcrumbs if there are no subregions configured in the root region
72 if (this.isRootRegion() && !this.model.get('subregions').models.length) {
73 t2bcs.hide();
74 }
75 },
76 isRootRegion: function () {
77 return this.model.get('id') === ROOT;
78 },
79 findNodeById: function (link, id) {
80 if (link.get('type') !== 'UiEdgeLink') {
81 // Remove /{port} from id if needed
82 var regex = new RegExp('^[^/]*');
83 id = regex.exec(id)[0];
84 }
85 return this.model.get('devices').get(id) ||
86 this.model.get('hosts').get(id) ||
87 this.model.get('subregions').get(id);
88 },
89 regionNodes: function () {
90
91 if (this.model) {
92 return [].concat(
93 this.model.get('devices').models,
94 this.model.get('hosts').models,
95 this.model.get('subregions').models
96 );
97 }
98
99 return [];
100 },
101 regionLinks: function () {
102 return (this.model) ? this.model.get('links').models : [];
103 },
Steven Burrowsb15a3942017-01-17 17:25:04 +0000104 getLink: function (linkId) {
105 return this.model.get('links').get(linkId);
106 },
Steven Burrows42eb9e22017-02-06 14:20:24 +0000107 getDevice: function (deviceId) {
108 return this.model.get('devices').get(deviceId);
109 },
Steven Burrowsaf96a212016-12-28 12:57:02 +0000110 filterRegionNodes: function (predicate) {
111 var nodes = this.regionNodes();
112 return _.filter(nodes, predicate);
113 },
114 deselectAllNodes: function () {
115 var selected = this.filterRegionNodes(function (node) {
116 return node.get('selected', true);
117 });
118
119 if (selected.length) {
120
121 selected.forEach(function (node) {
122 node.deselect();
123 });
124
125 t2dps().el.hide();
126 return true;
127 }
128
129 return false;
130 },
131 deselectLink: function () {
132 var selected = _.filter(this.regionLinks(), function (link) {
133 return link.get('selected', true);
134 });
135
136 if (selected.length) {
137
138 selected.forEach(function (link) {
139 link.deselect();
140 });
141
142 t2dps().el.hide();
143 return true;
144 }
145
146 return false;
Steven Burrowsb15a3942017-01-17 17:25:04 +0000147 },
148
149 update: function (event) {
Steven Burrows42eb9e22017-02-06 14:20:24 +0000150
Steven Burrowsb15a3942017-01-17 17:25:04 +0000151 if (this[event.type]) {
152 this[event.type](event);
Steven Burrowsb15a3942017-01-17 17:25:04 +0000153 } else {
154 $log.error("Unhanded topology update", event);
155 }
Steven Burrows42eb9e22017-02-06 14:20:24 +0000156
157 this.layout.update()
Steven Burrowsb15a3942017-01-17 17:25:04 +0000158 },
159
160 // Topology update event handlers
161 LINK_ADDED_OR_UPDATED: function (event) {
162 if (event.memo === 'added') {
163 var link = this.model.get('links').add(event.data);
164 link.createLink();
165 }
166 },
167 LINK_REMOVED: function (event) {
168 var link = this.getLink(event.subject);
169 link.remove();
Steven Burrows42eb9e22017-02-06 14:20:24 +0000170 this.model.get('links').remove(link);
171 },
172 DEVICE_ADDED_OR_UPDATED: function (event) {
173
174 var device;
175
176 if (event.memo === 'added') {
177 device = this.model.get('devices').add(event.data);
178 $log('Added device', device)
179 } else if (event.memo === 'updated') {
180 device = this.getDevice(event.subject);
181 device.set(event.data);
182 }
183 },
184 DEVICE_REMOVED: function (event) {
185 device.remove();
Steven Burrowsaf96a212016-12-28 12:57:02 +0000186 }
187 });
188
189 function getInstance() {
190 return instance || new Region();
191 }
192
193 return getInstance();
Steven Burrowsb15a3942017-01-17 17:25:04 +0000194
Steven Burrows57e24e92016-08-04 18:38:24 +0100195 }]);
196
197})();