Matteo Scandolo | 0344ef3 | 2016-02-22 16:53:22 -0800 | [diff] [blame] | 1 | (function () { |
| 2 | 'use strict'; |
| 3 | |
| 4 | angular.module('xos.serviceTopology') |
| 5 | .service('ChartData', function($rootScope, $q, lodash, Tenant, Node, serviceTopologyConfig, Ceilometer) { |
| 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 = () => { |
| 85 | const ip = this.currentServiceChain.children[0].children[0].tenant.wan_container_ip; |
| 86 | this.logicTopologyData.children[0].subscriberIP = ip; |
| 87 | }; |
| 88 | |
| 89 | this.selectSubscriber = (subscriber) => { |
| 90 | |
| 91 | // append the device with to config settings |
| 92 | serviceTopologyConfig.elWidths.push(160); |
| 93 | |
| 94 | this.addSubscriber(angular.copy(subscriber)); |
| 95 | |
| 96 | this.getSubscriberTag(); |
| 97 | this.getSubscriberIP(); |
| 98 | |
| 99 | }; |
| 100 | |
| 101 | this.highlightInstances = (instances) => { |
| 102 | |
| 103 | const computeNodes = this.logicTopologyData.children[0].children[0].computeNodes; |
| 104 | |
| 105 | // unselect all |
| 106 | computeNodes.map((node) => { |
| 107 | node.instances.map((instance) => { |
| 108 | instance.selected = false |
| 109 | return instance; |
| 110 | }); |
| 111 | }); |
| 112 | |
| 113 | lodash.forEach(instances, (instance) => { |
| 114 | computeNodes.map((node) => { |
| 115 | node.instances.map((d3instance) => { |
| 116 | if(d3instance.id === instance.id){ |
Matteo Scandolo | 06afdfe | 2016-02-23 13:47:14 -0800 | [diff] [blame^] | 117 | // console.log(d3instance, instance); |
Matteo Scandolo | 0344ef3 | 2016-02-22 16:53:22 -0800 | [diff] [blame] | 118 | d3instance.selected = true; |
Matteo Scandolo | 06afdfe | 2016-02-23 13:47:14 -0800 | [diff] [blame^] | 119 | d3instance.stats = instance.stats; //add stats to d3 node |
| 120 | d3instance.container = instance.container; // container info to d3 node |
Matteo Scandolo | 0344ef3 | 2016-02-22 16:53:22 -0800 | [diff] [blame] | 121 | } |
| 122 | return d3instance; |
| 123 | }); |
| 124 | }); |
| 125 | }); |
| 126 | |
| 127 | } |
| 128 | |
| 129 | this.getInstanceStatus = (service) => { |
| 130 | const deferred = $q.defer(); |
| 131 | |
Matteo Scandolo | 0344ef3 | 2016-02-22 16:53:22 -0800 | [diff] [blame] | 132 | let p; |
| 133 | |
Matteo Scandolo | 06afdfe | 2016-02-23 13:47:14 -0800 | [diff] [blame^] | 134 | // subscriber specific |
Matteo Scandolo | 0344ef3 | 2016-02-22 16:53:22 -0800 | [diff] [blame] | 135 | if(this.currentSubscriber){ |
Matteo Scandolo | 06afdfe | 2016-02-23 13:47:14 -0800 | [diff] [blame^] | 136 | |
| 137 | let attr; |
| 138 | try { |
| 139 | attr = JSON.parse(service.tenant.service_specific_attribute); |
| 140 | } |
| 141 | catch(e){ |
| 142 | attr = null; |
| 143 | } |
| 144 | |
| 145 | // if no instances are associated to the container |
| 146 | if(!attr || !attr.instance_id){ |
| 147 | let d = $q.defer(); |
| 148 | d.resolve([]); |
| 149 | p = d.promise; |
| 150 | } |
| 151 | else{ |
| 152 | let instances = [attr.instance_id]; |
| 153 | p = Ceilometer.getInstancesStats(instances) |
| 154 | .then((instances) => { |
| 155 | instances.map(i => { |
| 156 | i.container = { |
| 157 | name: `vcpe-${this.currentSubscriber.tags.sTag}-${this.currentSubscriber.tags.cTag}` |
| 158 | }; |
| 159 | return i; |
| 160 | }); |
| 161 | |
| 162 | // TODO fetch container stats |
| 163 | |
| 164 | return instances; |
| 165 | }); |
| 166 | } |
Matteo Scandolo | 0344ef3 | 2016-02-22 16:53:22 -0800 | [diff] [blame] | 167 | } |
Matteo Scandolo | 06afdfe | 2016-02-23 13:47:14 -0800 | [diff] [blame^] | 168 | // global scope |
Matteo Scandolo | 0344ef3 | 2016-02-22 16:53:22 -0800 | [diff] [blame] | 169 | else { |
Matteo Scandolo | 06afdfe | 2016-02-23 13:47:14 -0800 | [diff] [blame^] | 170 | const param = { |
Matteo Scandolo | 0344ef3 | 2016-02-22 16:53:22 -0800 | [diff] [blame] | 171 | 'service_vsg': {kind: 'vCPE'}, |
| 172 | 'service_vbng': {kind: 'vBNG'}, |
| 173 | 'service_volt': {kind: 'vOLT'} |
| 174 | }; |
| 175 | |
| 176 | p = Tenant.queryVsgInstances(param[service.name]).$promise |
| 177 | .then((instances) => { |
| 178 | |
| 179 | return Ceilometer.getInstancesStats(instances); |
| 180 | }); |
| 181 | } |
| 182 | |
| 183 | p.then((instances) => { |
| 184 | this.highlightInstances(instances); |
| 185 | deferred.resolve(instances); |
| 186 | }) |
| 187 | .catch((e) => { |
| 188 | deferred.reject(e); |
| 189 | }); |
| 190 | |
| 191 | return deferred.promise; |
| 192 | }; |
| 193 | }) |
| 194 | })(); |