blob: 09b8cd809f40fd8d523e47ad10baa17fee9e8760 [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 Burrowsaf96a212016-12-28 12:57:02 +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 },
104 filterRegionNodes: function (predicate) {
105 var nodes = this.regionNodes();
106 return _.filter(nodes, predicate);
107 },
108 deselectAllNodes: function () {
109 var selected = this.filterRegionNodes(function (node) {
110 return node.get('selected', true);
111 });
112
113 if (selected.length) {
114
115 selected.forEach(function (node) {
116 node.deselect();
117 });
118
119 t2dps().el.hide();
120 return true;
121 }
122
123 return false;
124 },
125 deselectLink: function () {
126 var selected = _.filter(this.regionLinks(), function (link) {
127 return link.get('selected', true);
128 });
129
130 if (selected.length) {
131
132 selected.forEach(function (link) {
133 link.deselect();
134 });
135
136 t2dps().el.hide();
137 return true;
138 }
139
140 return false;
141 }
142 });
143
144 function getInstance() {
145 return instance || new Region();
146 }
147
148 return getInstance();
Steven Burrows57e24e92016-08-04 18:38:24 +0100149 }]);
150
151})();