blob: 929f1c97b33a76e772d7a33f7e661073d3405e55 [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 Burrows3cc0c372017-02-10 15:15:24 +000038 'Topo2BreadcrumbService', 'Topo2ViewController', 'Topo2SpriteLayerService',
39 function ($log, _Model_, t2sr, t2ds, t2hs, t2ls, t2zs, t2dps, t2bcs, ViewController, t2sls) {
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 }
Steven Burrows3cc0c372017-02-10 15:15:24 +000075
76 // TODO: This should load the sprite layer from the region data
77 if (this.model.get('id') === 'c01') {
78 t2sls.loadLayout('segmentRouting');
79 t2sls.show();
80 } else {
81 t2sls.hide();
82 }
Steven Burrowsaf96a212016-12-28 12:57:02 +000083 },
84 isRootRegion: function () {
85 return this.model.get('id') === ROOT;
86 },
87 findNodeById: function (link, id) {
88 if (link.get('type') !== 'UiEdgeLink') {
89 // Remove /{port} from id if needed
90 var regex = new RegExp('^[^/]*');
91 id = regex.exec(id)[0];
92 }
93 return this.model.get('devices').get(id) ||
94 this.model.get('hosts').get(id) ||
95 this.model.get('subregions').get(id);
96 },
97 regionNodes: function () {
98
99 if (this.model) {
100 return [].concat(
101 this.model.get('devices').models,
102 this.model.get('hosts').models,
103 this.model.get('subregions').models
104 );
105 }
106
107 return [];
108 },
109 regionLinks: function () {
110 return (this.model) ? this.model.get('links').models : [];
111 },
Steven Burrowsb15a3942017-01-17 17:25:04 +0000112 getLink: function (linkId) {
113 return this.model.get('links').get(linkId);
114 },
Steven Burrows42eb9e22017-02-06 14:20:24 +0000115 getDevice: function (deviceId) {
116 return this.model.get('devices').get(deviceId);
117 },
Steven Burrowsaf96a212016-12-28 12:57:02 +0000118 filterRegionNodes: function (predicate) {
119 var nodes = this.regionNodes();
120 return _.filter(nodes, predicate);
121 },
122 deselectAllNodes: function () {
123 var selected = this.filterRegionNodes(function (node) {
124 return node.get('selected', true);
125 });
126
127 if (selected.length) {
128
129 selected.forEach(function (node) {
130 node.deselect();
131 });
132
133 t2dps().el.hide();
134 return true;
135 }
136
137 return false;
138 },
139 deselectLink: function () {
140 var selected = _.filter(this.regionLinks(), function (link) {
141 return link.get('selected', true);
142 });
143
144 if (selected.length) {
145
146 selected.forEach(function (link) {
147 link.deselect();
148 });
149
150 t2dps().el.hide();
151 return true;
152 }
153
154 return false;
Steven Burrowsb15a3942017-01-17 17:25:04 +0000155 },
156
157 update: function (event) {
Steven Burrows42eb9e22017-02-06 14:20:24 +0000158
Steven Burrowsb15a3942017-01-17 17:25:04 +0000159 if (this[event.type]) {
160 this[event.type](event);
Steven Burrowsb15a3942017-01-17 17:25:04 +0000161 } else {
162 $log.error("Unhanded topology update", event);
163 }
Steven Burrows42eb9e22017-02-06 14:20:24 +0000164
165 this.layout.update()
Steven Burrowsb15a3942017-01-17 17:25:04 +0000166 },
167
168 // Topology update event handlers
169 LINK_ADDED_OR_UPDATED: function (event) {
170 if (event.memo === 'added') {
171 var link = this.model.get('links').add(event.data);
172 link.createLink();
173 }
174 },
175 LINK_REMOVED: function (event) {
176 var link = this.getLink(event.subject);
177 link.remove();
Steven Burrows42eb9e22017-02-06 14:20:24 +0000178 this.model.get('links').remove(link);
179 },
180 DEVICE_ADDED_OR_UPDATED: function (event) {
181
182 var device;
183
184 if (event.memo === 'added') {
185 device = this.model.get('devices').add(event.data);
Simon Hunteb3cf542017-02-10 13:18:41 -0800186 $log.debug('Added device', device);
Steven Burrows42eb9e22017-02-06 14:20:24 +0000187 } else if (event.memo === 'updated') {
188 device = this.getDevice(event.subject);
189 device.set(event.data);
190 }
191 },
192 DEVICE_REMOVED: function (event) {
193 device.remove();
Steven Burrowsaf96a212016-12-28 12:57:02 +0000194 }
195 });
196
197 function getInstance() {
198 return instance || new Region();
199 }
200
201 return getInstance();
Steven Burrowsb15a3942017-01-17 17:25:04 +0000202
Steven Burrows57e24e92016-08-04 18:38:24 +0100203 }]);
204
205})();