Matteo Scandolo | d2044a4 | 2017-08-07 16:08:28 -0700 | [diff] [blame] | 1 | |
| 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 Scandolo | 012dddb | 2016-02-22 16:53:22 -0800 | [diff] [blame] | 19 | (function () { |
| 20 | 'use strict'; |
| 21 | |
Matteo Scandolo | 0456495 | 2016-02-24 11:22:48 -0800 | [diff] [blame] | 22 | angular.module('xos.diagnostic') |
Matteo Scandolo | fe307b1 | 2016-05-17 14:29:01 -0700 | [diff] [blame] | 23 | .service('ChartData', function($rootScope, $q, _, Tenant, Node, serviceTopologyConfig, Ceilometer, Instances) { |
Matteo Scandolo | 012dddb | 2016-02-22 16:53:22 -0800 | [diff] [blame] | 24 | this.currentSubscriber = null; |
| 25 | this.currentServiceChain = null; |
| 26 | |
| 27 | this.logicTopologyData = { |
| 28 | name: 'Router', |
| 29 | type: 'router', |
| 30 | children: [ |
| 31 | { |
Matteo Scandolo | 19acf7c | 2016-03-07 16:07:13 -0800 | [diff] [blame] | 32 | name: 'WAN-Side', |
| 33 | subtitle: 'Virtual Network', |
Matteo Scandolo | 012dddb | 2016-02-22 16:53:22 -0800 | [diff] [blame] | 34 | type: 'network', |
| 35 | children: [ |
| 36 | { |
Matteo Scandolo | 19acf7c | 2016-03-07 16:07:13 -0800 | [diff] [blame] | 37 | name: 'Compute Servers', |
Matteo Scandolo | 012dddb | 2016-02-22 16:53:22 -0800 | [diff] [blame] | 38 | type: 'rack', |
| 39 | computeNodes: [], |
| 40 | children: [ |
| 41 | { |
Matteo Scandolo | 19acf7c | 2016-03-07 16:07:13 -0800 | [diff] [blame] | 42 | name: 'LAN-Side', |
Matteo Scandolo | 7910819 | 2016-03-08 09:33:26 -0800 | [diff] [blame] | 43 | subtitle: 'Virtual Network', |
Matteo Scandolo | 012dddb | 2016-02-22 16:53:22 -0800 | [diff] [blame] | 44 | 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 Scandolo | d4ea877 | 2016-03-01 15:20:29 -0800 | [diff] [blame] | 75 | cTag: tags.cTag, |
| 76 | sTag: tags.sTag |
Matteo Scandolo | 26d17e1 | 2016-02-23 13:47:14 -0800 | [diff] [blame] | 77 | }; |
Matteo Scandolo | 012dddb | 2016-02-22 16:53:22 -0800 | [diff] [blame] | 78 | }; |
| 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 Scandolo | 3a176a2 | 2016-03-07 16:42:03 -0800 | [diff] [blame] | 91 | /** |
| 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 Scandolo | f81130f | 2016-03-11 11:16:58 -0800 | [diff] [blame] | 102 | //remove tags and ip |
| 103 | delete this.logicTopologyData.children[0].children[0].children[0].subscriberTag; |
| 104 | delete this.logicTopologyData.children[0].subscriberIP; |
| 105 | |
Matteo Scandolo | 3a176a2 | 2016-03-07 16:42:03 -0800 | [diff] [blame] | 106 | this.highlightInstances([]); |
| 107 | delete this.logicTopologyData.children[0].children[0].children[0].children[0].children; |
| 108 | } |
| 109 | |
Matteo Scandolo | d4ea877 | 2016-03-01 15:20:29 -0800 | [diff] [blame] | 110 | this.getSubscriberTag = (subscriber) => { |
| 111 | const tags = { |
Matteo Scandolo | e64dcc0 | 2016-08-02 11:53:22 -0700 | [diff] [blame] | 112 | cTag: subscriber.related.c_tag, |
| 113 | sTag: subscriber.related.s_tag |
Matteo Scandolo | d4ea877 | 2016-03-01 15:20:29 -0800 | [diff] [blame] | 114 | }; |
Matteo Scandolo | 26d17e1 | 2016-02-23 13:47:14 -0800 | [diff] [blame] | 115 | |
| 116 | this.addSubscriberTag(tags); |
| 117 | // add tags info to current subscriber |
Matteo Scandolo | d4ea877 | 2016-03-01 15:20:29 -0800 | [diff] [blame] | 118 | this.currentSubscriber.tags = tags; |
Matteo Scandolo | 012dddb | 2016-02-22 16:53:22 -0800 | [diff] [blame] | 119 | |
| 120 | }; |
| 121 | |
Matteo Scandolo | 6c6e594 | 2016-03-02 10:59:46 -0800 | [diff] [blame] | 122 | this.getSubscriberIP = (subscriber) => { |
Matteo Scandolo | e64dcc0 | 2016-08-02 11:53:22 -0700 | [diff] [blame] | 123 | this.logicTopologyData.children[0].subscriberIP = subscriber.related.wan_container_ip; |
Matteo Scandolo | 012dddb | 2016-02-22 16:53:22 -0800 | [diff] [blame] | 124 | }; |
| 125 | |
| 126 | this.selectSubscriber = (subscriber) => { |
Matteo Scandolo | f79f0b3 | 2016-09-30 10:23:10 -0700 | [diff] [blame] | 127 | |
Matteo Scandolo | 012dddb | 2016-02-22 16:53:22 -0800 | [diff] [blame] | 128 | // append the device with to config settings |
| 129 | serviceTopologyConfig.elWidths.push(160); |
| 130 | |
| 131 | this.addSubscriber(angular.copy(subscriber)); |
| 132 | |
Matteo Scandolo | 6aa165f | 2016-02-23 14:03:03 -0800 | [diff] [blame] | 133 | //clean selected instances |
| 134 | this.highlightInstances([]); |
| 135 | |
Matteo Scandolo | d4ea877 | 2016-03-01 15:20:29 -0800 | [diff] [blame] | 136 | this.getSubscriberTag(subscriber); |
Matteo Scandolo | 6c6e594 | 2016-03-02 10:59:46 -0800 | [diff] [blame] | 137 | this.getSubscriberIP(subscriber); |
Matteo Scandolo | 012dddb | 2016-02-22 16:53:22 -0800 | [diff] [blame] | 138 | |
| 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 Scandolo | fe307b1 | 2016-05-17 14:29:01 -0700 | [diff] [blame] | 153 | _.forEach(instances, (instance) => { |
Matteo Scandolo | 012dddb | 2016-02-22 16:53:22 -0800 | [diff] [blame] | 154 | computeNodes.map((node) => { |
| 155 | node.instances.map((d3instance) => { |
| 156 | if(d3instance.id === instance.id){ |
Matteo Scandolo | 26d17e1 | 2016-02-23 13:47:14 -0800 | [diff] [blame] | 157 | // console.log(d3instance, instance); |
Matteo Scandolo | 012dddb | 2016-02-22 16:53:22 -0800 | [diff] [blame] | 158 | d3instance.selected = true; |
Matteo Scandolo | 26d17e1 | 2016-02-23 13:47:14 -0800 | [diff] [blame] | 159 | d3instance.stats = instance.stats; //add stats to d3 node |
| 160 | d3instance.container = instance.container; // container info to d3 node |
Matteo Scandolo | 012dddb | 2016-02-22 16:53:22 -0800 | [diff] [blame] | 161 | } |
| 162 | return d3instance; |
| 163 | }); |
| 164 | }); |
| 165 | }); |
| 166 | |
| 167 | } |
| 168 | |
| 169 | this.getInstanceStatus = (service) => { |
| 170 | const deferred = $q.defer(); |
| 171 | |
Matteo Scandolo | 012dddb | 2016-02-22 16:53:22 -0800 | [diff] [blame] | 172 | let p; |
| 173 | |
Matteo Scandolo | 26d17e1 | 2016-02-23 13:47:14 -0800 | [diff] [blame] | 174 | // subscriber specific |
Matteo Scandolo | 012dddb | 2016-02-22 16:53:22 -0800 | [diff] [blame] | 175 | if(this.currentSubscriber){ |
Matteo Scandolo | 26d17e1 | 2016-02-23 13:47:14 -0800 | [diff] [blame] | 176 | |
| 177 | let attr; |
| 178 | try { |
| 179 | attr = JSON.parse(service.tenant.service_specific_attribute); |
| 180 | } |
| 181 | catch(e){ |
| 182 | attr = null; |
| 183 | } |
| 184 | |
Matteo Scandolo | f0d6e69 | 2016-02-24 11:14:01 -0800 | [diff] [blame] | 185 | // if no instances are associated to the subscriber |
Matteo Scandolo | 26d17e1 | 2016-02-23 13:47:14 -0800 | [diff] [blame] | 186 | if(!attr || !attr.instance_id){ |
| 187 | let d = $q.defer(); |
| 188 | d.resolve([]); |
| 189 | p = d.promise; |
| 190 | } |
Matteo Scandolo | f0d6e69 | 2016-02-24 11:14:01 -0800 | [diff] [blame] | 191 | // if ther is an instance |
Matteo Scandolo | 26d17e1 | 2016-02-23 13:47:14 -0800 | [diff] [blame] | 192 | else{ |
Matteo Scandolo | f0d6e69 | 2016-02-24 11:14:01 -0800 | [diff] [blame] | 193 | 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 Scandolo | 26d17e1 | 2016-02-23 13:47:14 -0800 | [diff] [blame] | 206 | |
| 207 | // TODO fetch container stats |
Matteo Scandolo | f0d6e69 | 2016-02-24 11:14:01 -0800 | [diff] [blame] | 208 | return Ceilometer.getContainerStats(containerName); |
| 209 | }) |
| 210 | .then((containerStats) => { |
| 211 | instance.container.stats = containerStats.stats; |
| 212 | instance.container.port = containerStats.port; |
| 213 | return [instance]; |
Matteo Scandolo | 26d17e1 | 2016-02-23 13:47:14 -0800 | [diff] [blame] | 214 | }); |
| 215 | } |
Matteo Scandolo | 012dddb | 2016-02-22 16:53:22 -0800 | [diff] [blame] | 216 | } |
Matteo Scandolo | 26d17e1 | 2016-02-23 13:47:14 -0800 | [diff] [blame] | 217 | // global scope |
Matteo Scandolo | 012dddb | 2016-02-22 16:53:22 -0800 | [diff] [blame] | 218 | else { |
Matteo Scandolo | 26d17e1 | 2016-02-23 13:47:14 -0800 | [diff] [blame] | 219 | const param = { |
Matteo Scandolo | 012dddb | 2016-02-22 16:53:22 -0800 | [diff] [blame] | 220 | '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 Scandolo | fe307b1 | 2016-05-17 14:29:01 -0700 | [diff] [blame] | 227 | return Ceilometer.getInstancesStats(_.uniq(instances)); |
Matteo Scandolo | 012dddb | 2016-02-22 16:53:22 -0800 | [diff] [blame] | 228 | }); |
| 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 | })(); |