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