blob: 419a3eb18b7f01b4837e17e122aca04d8a9a5871 [file] [log] [blame]
Matteo Scandolod2044a42017-08-07 16:08:28 -07001
2/*
3 * Copyright 2017-present Open Networking Foundation
4
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8
9 * http://www.apache.org/licenses/LICENSE-2.0
10
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18
Matteo Scandolo012dddb2016-02-22 16:53:22 -080019(function () {
20 'use strict';
21
Matteo Scandolo04564952016-02-24 11:22:48 -080022 angular.module('xos.diagnostic')
Matteo Scandolofe307b12016-05-17 14:29:01 -070023 .service('ChartData', function($rootScope, $q, _, Tenant, Node, serviceTopologyConfig, Ceilometer, Instances) {
Matteo Scandolo012dddb2016-02-22 16:53:22 -080024 this.currentSubscriber = null;
25 this.currentServiceChain = null;
26
27 this.logicTopologyData = {
28 name: 'Router',
29 type: 'router',
30 children: [
31 {
Matteo Scandolo19acf7c2016-03-07 16:07:13 -080032 name: 'WAN-Side',
33 subtitle: 'Virtual Network',
Matteo Scandolo012dddb2016-02-22 16:53:22 -080034 type: 'network',
35 children: [
36 {
Matteo Scandolo19acf7c2016-03-07 16:07:13 -080037 name: 'Compute Servers',
Matteo Scandolo012dddb2016-02-22 16:53:22 -080038 type: 'rack',
39 computeNodes: [],
40 children: [
41 {
Matteo Scandolo19acf7c2016-03-07 16:07:13 -080042 name: 'LAN-Side',
Matteo Scandolo79108192016-03-08 09:33:26 -080043 subtitle: 'Virtual Network',
Matteo Scandolo012dddb2016-02-22 16:53:22 -080044 type: 'network',
45 children: [{
46 name: 'Subscriber',
47 type: 'subscriber'
48 }] //subscribers goes here
49 }
50 ]
51 }
52 ]
53 }
54 ]
55 };
56
57 this.getLogicTree = () => {
58 const deferred = $q.defer();
59
60 Node.queryWithInstances().$promise
61 .then((computeNodes) => {
62 this.logicTopologyData.children[0].children[0].computeNodes = computeNodes;
63 // LogicTopologyHelper.updateTree(svg);
64 deferred.resolve(this.logicTopologyData);
65 });
66
67 return deferred.promise;
68 };
69
70 /**
71 * Add Subscriber tag to LAN Network
72 */
73 this.addSubscriberTag = (tags) => {
74 this.logicTopologyData.children[0].children[0].children[0].subscriberTag = {
Matteo Scandolod4ea8772016-03-01 15:20:29 -080075 cTag: tags.cTag,
76 sTag: tags.sTag
Matteo Scandolo26d17e12016-02-23 13:47:14 -080077 };
Matteo Scandolo012dddb2016-02-22 16:53:22 -080078 };
79
80 /**
81 * Add Subscribers to the tree
82 */
83 this.addSubscriber = (subscriber) => {
84 subscriber.children = subscriber.devices;
85
86 // add subscriber to data tree
87 this.logicTopologyData.children[0].children[0].children[0].children = [subscriber];
88 return this.logicTopologyData;
89 };
90
Matteo Scandolo3a176a22016-03-07 16:42:03 -080091 /**
92 * Remove a subscriber from the tree
93 */
94
95 this.removeSubscriber = () => {
96 this.logicTopologyData.children[0].children[0].children[0].children[0].humanReadableName = 'Subscriber';
97 this.currentSubscriber = null;
98 if(serviceTopologyConfig.elWidths[serviceTopologyConfig.elWidths.length - 1] === 160){
99 serviceTopologyConfig.elWidths.pop();
100 }
101
Matteo Scandolof81130f2016-03-11 11:16:58 -0800102 //remove tags and ip
103 delete this.logicTopologyData.children[0].children[0].children[0].subscriberTag;
104 delete this.logicTopologyData.children[0].subscriberIP;
105
Matteo Scandolo3a176a22016-03-07 16:42:03 -0800106 this.highlightInstances([]);
107 delete this.logicTopologyData.children[0].children[0].children[0].children[0].children;
108 }
109
Matteo Scandolod4ea8772016-03-01 15:20:29 -0800110 this.getSubscriberTag = (subscriber) => {
111 const tags = {
Matteo Scandoloe64dcc02016-08-02 11:53:22 -0700112 cTag: subscriber.related.c_tag,
113 sTag: subscriber.related.s_tag
Matteo Scandolod4ea8772016-03-01 15:20:29 -0800114 };
Matteo Scandolo26d17e12016-02-23 13:47:14 -0800115
116 this.addSubscriberTag(tags);
117 // add tags info to current subscriber
Matteo Scandolod4ea8772016-03-01 15:20:29 -0800118 this.currentSubscriber.tags = tags;
Matteo Scandolo012dddb2016-02-22 16:53:22 -0800119
120 };
121
Matteo Scandolo6c6e5942016-03-02 10:59:46 -0800122 this.getSubscriberIP = (subscriber) => {
Matteo Scandoloe64dcc02016-08-02 11:53:22 -0700123 this.logicTopologyData.children[0].subscriberIP = subscriber.related.wan_container_ip;
Matteo Scandolo012dddb2016-02-22 16:53:22 -0800124 };
125
126 this.selectSubscriber = (subscriber) => {
Matteo Scandolof79f0b32016-09-30 10:23:10 -0700127
Matteo Scandolo012dddb2016-02-22 16:53:22 -0800128 // append the device with to config settings
129 serviceTopologyConfig.elWidths.push(160);
130
131 this.addSubscriber(angular.copy(subscriber));
132
Matteo Scandolo6aa165f2016-02-23 14:03:03 -0800133 //clean selected instances
134 this.highlightInstances([]);
135
Matteo Scandolod4ea8772016-03-01 15:20:29 -0800136 this.getSubscriberTag(subscriber);
Matteo Scandolo6c6e5942016-03-02 10:59:46 -0800137 this.getSubscriberIP(subscriber);
Matteo Scandolo012dddb2016-02-22 16:53:22 -0800138
139 };
140
141 this.highlightInstances = (instances) => {
142
143 const computeNodes = this.logicTopologyData.children[0].children[0].computeNodes;
144
145 // unselect all
146 computeNodes.map((node) => {
147 node.instances.map((instance) => {
148 instance.selected = false
149 return instance;
150 });
151 });
152
Matteo Scandolofe307b12016-05-17 14:29:01 -0700153 _.forEach(instances, (instance) => {
Matteo Scandolo012dddb2016-02-22 16:53:22 -0800154 computeNodes.map((node) => {
155 node.instances.map((d3instance) => {
156 if(d3instance.id === instance.id){
Matteo Scandolo26d17e12016-02-23 13:47:14 -0800157 // console.log(d3instance, instance);
Matteo Scandolo012dddb2016-02-22 16:53:22 -0800158 d3instance.selected = true;
Matteo Scandolo26d17e12016-02-23 13:47:14 -0800159 d3instance.stats = instance.stats; //add stats to d3 node
160 d3instance.container = instance.container; // container info to d3 node
Matteo Scandolo012dddb2016-02-22 16:53:22 -0800161 }
162 return d3instance;
163 });
164 });
165 });
166
167 }
168
169 this.getInstanceStatus = (service) => {
170 const deferred = $q.defer();
171
Matteo Scandolo012dddb2016-02-22 16:53:22 -0800172 let p;
173
Matteo Scandolo26d17e12016-02-23 13:47:14 -0800174 // subscriber specific
Matteo Scandolo012dddb2016-02-22 16:53:22 -0800175 if(this.currentSubscriber){
Matteo Scandolo26d17e12016-02-23 13:47:14 -0800176
177 let attr;
178 try {
179 attr = JSON.parse(service.tenant.service_specific_attribute);
180 }
181 catch(e){
182 attr = null;
183 }
184
Matteo Scandolof0d6e692016-02-24 11:14:01 -0800185 // if no instances are associated to the subscriber
Matteo Scandolo26d17e12016-02-23 13:47:14 -0800186 if(!attr || !attr.instance_id){
187 let d = $q.defer();
188 d.resolve([]);
189 p = d.promise;
190 }
Matteo Scandolof0d6e692016-02-24 11:14:01 -0800191 // if ther is an instance
Matteo Scandolo26d17e12016-02-23 13:47:14 -0800192 else{
Matteo Scandolof0d6e692016-02-24 11:14:01 -0800193 let instance = {};
194 p = Instances.get({id: attr.instance_id}).$promise
195 .then(function(_instance){
196 instance = _instance;
197 return Ceilometer.getInstanceStats(instance.instance_uuid);
198 })
199 .then((stats) => {
200 instance.stats = stats;
201 const containerName = `vcpe-${this.currentSubscriber.tags.sTag}-${this.currentSubscriber.tags.cTag}`;
202 // append containers
203 instance.container = {
204 name: containerName
205 };
Matteo Scandolo26d17e12016-02-23 13:47:14 -0800206
207 // TODO fetch container stats
Matteo Scandolof0d6e692016-02-24 11:14:01 -0800208 return Ceilometer.getContainerStats(containerName);
209 })
210 .then((containerStats) => {
211 instance.container.stats = containerStats.stats;
212 instance.container.port = containerStats.port;
213 return [instance];
Matteo Scandolo26d17e12016-02-23 13:47:14 -0800214 });
215 }
Matteo Scandolo012dddb2016-02-22 16:53:22 -0800216 }
Matteo Scandolo26d17e12016-02-23 13:47:14 -0800217 // global scope
Matteo Scandolo012dddb2016-02-22 16:53:22 -0800218 else {
Matteo Scandolo26d17e12016-02-23 13:47:14 -0800219 const param = {
Matteo Scandolo012dddb2016-02-22 16:53:22 -0800220 'service_vsg': {kind: 'vCPE'},
221 'service_vbng': {kind: 'vBNG'},
222 'service_volt': {kind: 'vOLT'}
223 };
224
225 p = Tenant.queryVsgInstances(param[service.name]).$promise
226 .then((instances) => {
Matteo Scandolofe307b12016-05-17 14:29:01 -0700227 return Ceilometer.getInstancesStats(_.uniq(instances));
Matteo Scandolo012dddb2016-02-22 16:53:22 -0800228 });
229 }
230
231 p.then((instances) => {
232 this.highlightInstances(instances);
233 deferred.resolve(instances);
234 })
235 .catch((e) => {
236 deferred.reject(e);
237 });
238
239 return deferred.promise;
240 };
241 })
242})();