Matteo Scandolo | 012dddb | 2016-02-22 16:53:22 -0800 | [diff] [blame] | 1 | (function () { |
| 2 | 'use strict'; |
| 3 | |
Matteo Scandolo | 0456495 | 2016-02-24 11:22:48 -0800 | [diff] [blame] | 4 | angular.module('xos.diagnostic') |
Matteo Scandolo | fe307b1 | 2016-05-17 14:29:01 -0700 | [diff] [blame] | 5 | .service('ChartData', function($rootScope, $q, _, Tenant, Node, serviceTopologyConfig, Ceilometer, Instances) { |
Matteo Scandolo | 012dddb | 2016-02-22 16:53:22 -0800 | [diff] [blame] | 6 | this.currentSubscriber = null; |
| 7 | this.currentServiceChain = null; |
| 8 | |
| 9 | this.logicTopologyData = { |
| 10 | name: 'Router', |
| 11 | type: 'router', |
| 12 | children: [ |
| 13 | { |
Matteo Scandolo | 19acf7c | 2016-03-07 16:07:13 -0800 | [diff] [blame] | 14 | name: 'WAN-Side', |
| 15 | subtitle: 'Virtual Network', |
Matteo Scandolo | 012dddb | 2016-02-22 16:53:22 -0800 | [diff] [blame] | 16 | type: 'network', |
| 17 | children: [ |
| 18 | { |
Matteo Scandolo | 19acf7c | 2016-03-07 16:07:13 -0800 | [diff] [blame] | 19 | name: 'Compute Servers', |
Matteo Scandolo | 012dddb | 2016-02-22 16:53:22 -0800 | [diff] [blame] | 20 | type: 'rack', |
| 21 | computeNodes: [], |
| 22 | children: [ |
| 23 | { |
Matteo Scandolo | 19acf7c | 2016-03-07 16:07:13 -0800 | [diff] [blame] | 24 | name: 'LAN-Side', |
Matteo Scandolo | 7910819 | 2016-03-08 09:33:26 -0800 | [diff] [blame] | 25 | subtitle: 'Virtual Network', |
Matteo Scandolo | 012dddb | 2016-02-22 16:53:22 -0800 | [diff] [blame] | 26 | type: 'network', |
| 27 | children: [{ |
| 28 | name: 'Subscriber', |
| 29 | type: 'subscriber' |
| 30 | }] //subscribers goes here |
| 31 | } |
| 32 | ] |
| 33 | } |
| 34 | ] |
| 35 | } |
| 36 | ] |
| 37 | }; |
| 38 | |
| 39 | this.getLogicTree = () => { |
| 40 | const deferred = $q.defer(); |
| 41 | |
| 42 | Node.queryWithInstances().$promise |
| 43 | .then((computeNodes) => { |
| 44 | this.logicTopologyData.children[0].children[0].computeNodes = computeNodes; |
| 45 | // LogicTopologyHelper.updateTree(svg); |
| 46 | deferred.resolve(this.logicTopologyData); |
| 47 | }); |
| 48 | |
| 49 | return deferred.promise; |
| 50 | }; |
| 51 | |
| 52 | /** |
| 53 | * Add Subscriber tag to LAN Network |
| 54 | */ |
| 55 | this.addSubscriberTag = (tags) => { |
| 56 | this.logicTopologyData.children[0].children[0].children[0].subscriberTag = { |
Matteo Scandolo | d4ea877 | 2016-03-01 15:20:29 -0800 | [diff] [blame] | 57 | cTag: tags.cTag, |
| 58 | sTag: tags.sTag |
Matteo Scandolo | 26d17e1 | 2016-02-23 13:47:14 -0800 | [diff] [blame] | 59 | }; |
Matteo Scandolo | 012dddb | 2016-02-22 16:53:22 -0800 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | /** |
| 63 | * Add Subscribers to the tree |
| 64 | */ |
| 65 | this.addSubscriber = (subscriber) => { |
| 66 | subscriber.children = subscriber.devices; |
| 67 | |
| 68 | // add subscriber to data tree |
| 69 | this.logicTopologyData.children[0].children[0].children[0].children = [subscriber]; |
| 70 | return this.logicTopologyData; |
| 71 | }; |
| 72 | |
Matteo Scandolo | 3a176a2 | 2016-03-07 16:42:03 -0800 | [diff] [blame] | 73 | /** |
| 74 | * Remove a subscriber from the tree |
| 75 | */ |
| 76 | |
| 77 | this.removeSubscriber = () => { |
| 78 | this.logicTopologyData.children[0].children[0].children[0].children[0].humanReadableName = 'Subscriber'; |
| 79 | this.currentSubscriber = null; |
| 80 | if(serviceTopologyConfig.elWidths[serviceTopologyConfig.elWidths.length - 1] === 160){ |
| 81 | serviceTopologyConfig.elWidths.pop(); |
| 82 | } |
| 83 | |
Matteo Scandolo | f81130f | 2016-03-11 11:16:58 -0800 | [diff] [blame] | 84 | //remove tags and ip |
| 85 | delete this.logicTopologyData.children[0].children[0].children[0].subscriberTag; |
| 86 | delete this.logicTopologyData.children[0].subscriberIP; |
| 87 | |
Matteo Scandolo | 3a176a2 | 2016-03-07 16:42:03 -0800 | [diff] [blame] | 88 | this.highlightInstances([]); |
| 89 | delete this.logicTopologyData.children[0].children[0].children[0].children[0].children; |
| 90 | } |
| 91 | |
Matteo Scandolo | d4ea877 | 2016-03-01 15:20:29 -0800 | [diff] [blame] | 92 | this.getSubscriberTag = (subscriber) => { |
| 93 | const tags = { |
| 94 | cTag: subscriber.c_tag, |
| 95 | sTag: subscriber.s_tag |
| 96 | }; |
Matteo Scandolo | 26d17e1 | 2016-02-23 13:47:14 -0800 | [diff] [blame] | 97 | |
| 98 | this.addSubscriberTag(tags); |
| 99 | // add tags info to current subscriber |
Matteo Scandolo | d4ea877 | 2016-03-01 15:20:29 -0800 | [diff] [blame] | 100 | this.currentSubscriber.tags = tags; |
Matteo Scandolo | 012dddb | 2016-02-22 16:53:22 -0800 | [diff] [blame] | 101 | |
| 102 | }; |
| 103 | |
Matteo Scandolo | 6c6e594 | 2016-03-02 10:59:46 -0800 | [diff] [blame] | 104 | this.getSubscriberIP = (subscriber) => { |
| 105 | // const ip = JSON.parse(this.currentServiceChain.children[0].children[0].tenant.service_specific_attribute).wan_container_ip; |
Matteo Scandolo | f0d6e69 | 2016-02-24 11:14:01 -0800 | [diff] [blame] | 106 | // const ip = this.currentServiceChain.children[0].children[0].tenant.wan_container_ip; |
Matteo Scandolo | 6c6e594 | 2016-03-02 10:59:46 -0800 | [diff] [blame] | 107 | this.logicTopologyData.children[0].subscriberIP = subscriber.wan_container_ip; |
Matteo Scandolo | 012dddb | 2016-02-22 16:53:22 -0800 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | this.selectSubscriber = (subscriber) => { |
Matteo Scandolo | 012dddb | 2016-02-22 16:53:22 -0800 | [diff] [blame] | 111 | // append the device with to config settings |
| 112 | serviceTopologyConfig.elWidths.push(160); |
| 113 | |
| 114 | this.addSubscriber(angular.copy(subscriber)); |
| 115 | |
Matteo Scandolo | 6aa165f | 2016-02-23 14:03:03 -0800 | [diff] [blame] | 116 | //clean selected instances |
| 117 | this.highlightInstances([]); |
| 118 | |
Matteo Scandolo | d4ea877 | 2016-03-01 15:20:29 -0800 | [diff] [blame] | 119 | this.getSubscriberTag(subscriber); |
Matteo Scandolo | 6c6e594 | 2016-03-02 10:59:46 -0800 | [diff] [blame] | 120 | this.getSubscriberIP(subscriber); |
Matteo Scandolo | 012dddb | 2016-02-22 16:53:22 -0800 | [diff] [blame] | 121 | |
| 122 | }; |
| 123 | |
| 124 | this.highlightInstances = (instances) => { |
| 125 | |
| 126 | const computeNodes = this.logicTopologyData.children[0].children[0].computeNodes; |
| 127 | |
| 128 | // unselect all |
| 129 | computeNodes.map((node) => { |
| 130 | node.instances.map((instance) => { |
| 131 | instance.selected = false |
| 132 | return instance; |
| 133 | }); |
| 134 | }); |
| 135 | |
Matteo Scandolo | fe307b1 | 2016-05-17 14:29:01 -0700 | [diff] [blame] | 136 | _.forEach(instances, (instance) => { |
Matteo Scandolo | 012dddb | 2016-02-22 16:53:22 -0800 | [diff] [blame] | 137 | computeNodes.map((node) => { |
| 138 | node.instances.map((d3instance) => { |
| 139 | if(d3instance.id === instance.id){ |
Matteo Scandolo | 26d17e1 | 2016-02-23 13:47:14 -0800 | [diff] [blame] | 140 | // console.log(d3instance, instance); |
Matteo Scandolo | 012dddb | 2016-02-22 16:53:22 -0800 | [diff] [blame] | 141 | d3instance.selected = true; |
Matteo Scandolo | 26d17e1 | 2016-02-23 13:47:14 -0800 | [diff] [blame] | 142 | d3instance.stats = instance.stats; //add stats to d3 node |
| 143 | d3instance.container = instance.container; // container info to d3 node |
Matteo Scandolo | 012dddb | 2016-02-22 16:53:22 -0800 | [diff] [blame] | 144 | } |
| 145 | return d3instance; |
| 146 | }); |
| 147 | }); |
| 148 | }); |
| 149 | |
| 150 | } |
| 151 | |
| 152 | this.getInstanceStatus = (service) => { |
| 153 | const deferred = $q.defer(); |
| 154 | |
Matteo Scandolo | 012dddb | 2016-02-22 16:53:22 -0800 | [diff] [blame] | 155 | let p; |
| 156 | |
Matteo Scandolo | 26d17e1 | 2016-02-23 13:47:14 -0800 | [diff] [blame] | 157 | // subscriber specific |
Matteo Scandolo | 012dddb | 2016-02-22 16:53:22 -0800 | [diff] [blame] | 158 | if(this.currentSubscriber){ |
Matteo Scandolo | 26d17e1 | 2016-02-23 13:47:14 -0800 | [diff] [blame] | 159 | |
| 160 | let attr; |
| 161 | try { |
| 162 | attr = JSON.parse(service.tenant.service_specific_attribute); |
| 163 | } |
| 164 | catch(e){ |
| 165 | attr = null; |
| 166 | } |
| 167 | |
Matteo Scandolo | f0d6e69 | 2016-02-24 11:14:01 -0800 | [diff] [blame] | 168 | // if no instances are associated to the subscriber |
Matteo Scandolo | 26d17e1 | 2016-02-23 13:47:14 -0800 | [diff] [blame] | 169 | if(!attr || !attr.instance_id){ |
| 170 | let d = $q.defer(); |
| 171 | d.resolve([]); |
| 172 | p = d.promise; |
| 173 | } |
Matteo Scandolo | f0d6e69 | 2016-02-24 11:14:01 -0800 | [diff] [blame] | 174 | // if ther is an instance |
Matteo Scandolo | 26d17e1 | 2016-02-23 13:47:14 -0800 | [diff] [blame] | 175 | else{ |
Matteo Scandolo | f0d6e69 | 2016-02-24 11:14:01 -0800 | [diff] [blame] | 176 | let instance = {}; |
| 177 | p = Instances.get({id: attr.instance_id}).$promise |
| 178 | .then(function(_instance){ |
| 179 | instance = _instance; |
| 180 | return Ceilometer.getInstanceStats(instance.instance_uuid); |
| 181 | }) |
| 182 | .then((stats) => { |
| 183 | instance.stats = stats; |
| 184 | const containerName = `vcpe-${this.currentSubscriber.tags.sTag}-${this.currentSubscriber.tags.cTag}`; |
| 185 | // append containers |
| 186 | instance.container = { |
| 187 | name: containerName |
| 188 | }; |
Matteo Scandolo | 26d17e1 | 2016-02-23 13:47:14 -0800 | [diff] [blame] | 189 | |
| 190 | // TODO fetch container stats |
Matteo Scandolo | f0d6e69 | 2016-02-24 11:14:01 -0800 | [diff] [blame] | 191 | return Ceilometer.getContainerStats(containerName); |
| 192 | }) |
| 193 | .then((containerStats) => { |
| 194 | instance.container.stats = containerStats.stats; |
| 195 | instance.container.port = containerStats.port; |
| 196 | return [instance]; |
Matteo Scandolo | 26d17e1 | 2016-02-23 13:47:14 -0800 | [diff] [blame] | 197 | }); |
| 198 | } |
Matteo Scandolo | 012dddb | 2016-02-22 16:53:22 -0800 | [diff] [blame] | 199 | } |
Matteo Scandolo | 26d17e1 | 2016-02-23 13:47:14 -0800 | [diff] [blame] | 200 | // global scope |
Matteo Scandolo | 012dddb | 2016-02-22 16:53:22 -0800 | [diff] [blame] | 201 | else { |
Matteo Scandolo | 26d17e1 | 2016-02-23 13:47:14 -0800 | [diff] [blame] | 202 | const param = { |
Matteo Scandolo | 012dddb | 2016-02-22 16:53:22 -0800 | [diff] [blame] | 203 | 'service_vsg': {kind: 'vCPE'}, |
| 204 | 'service_vbng': {kind: 'vBNG'}, |
| 205 | 'service_volt': {kind: 'vOLT'} |
| 206 | }; |
| 207 | |
| 208 | p = Tenant.queryVsgInstances(param[service.name]).$promise |
| 209 | .then((instances) => { |
Matteo Scandolo | fe307b1 | 2016-05-17 14:29:01 -0700 | [diff] [blame] | 210 | return Ceilometer.getInstancesStats(_.uniq(instances)); |
Matteo Scandolo | 012dddb | 2016-02-22 16:53:22 -0800 | [diff] [blame] | 211 | }); |
| 212 | } |
| 213 | |
| 214 | p.then((instances) => { |
| 215 | this.highlightInstances(instances); |
| 216 | deferred.resolve(instances); |
| 217 | }) |
| 218 | .catch((e) => { |
| 219 | deferred.reject(e); |
| 220 | }); |
| 221 | |
| 222 | return deferred.promise; |
| 223 | }; |
| 224 | }) |
| 225 | })(); |