Rift.IO OSM R1 Initial Submission
[osm/UI.git] / skyquake / plugins / launchpad / api / transforms.js
1 /*
2 *
3 * Copyright 2016 RIFT.IO Inc
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 var _ = require('lodash');
19
20 /**
21 * Merges vnfr["nfvi-metrics"] with nsr["nfvi-metrics"] into a format consumable by the UI LP and Record Cards
22 * @param {Array} vnfr vnfr nfvi metrics
23 * @param {Array} nsr nsr nfvi metrics
24 * @return {Array} Merged NSR nfvi metrics
25 */
26 function mergeVnfrNfviMetrics(vnfr, nsr) {
27 for (var i = 0; i < vnfr.length; i ++) {
28 var title = vnfr[i].title;
29 var matched = false;
30 for (var j = 0; j < nsr.length; j++) {
31 if (nsr[j] && nsr[j].title == title) {
32 matched = true;
33 nsr[j].data = nsr[j].data.concat(vnfr[i].data).sort(function(a,b) {
34 return (a.name > b.name) ? 1 : -1;
35 });
36 break;
37 }
38 }
39 if (!matched) {
40 nsr.push({
41 title: title,
42 data: vnfr[i].data
43 });
44 }
45 }
46 }
47
48 function terminationPointValues (node) {
49 var tpids = [];
50 var attrs = { 'tp-id': [] };
51 var TERM_PT_KEY = 'ietf-network-topology:termination-point';
52 var TERM_PT_ATTR_KEY = 'ietf-l2-topology:l2-termination-point-attributes';
53
54 var node_id = node['node-id'];
55 node[TERM_PT_KEY].forEach(
56 function(elem, index, array) {
57 if ('tp-id' in elem) {
58 attrs['tp-id'].push(elem['tp-id'])
59 }
60 if (TERM_PT_ATTR_KEY in elem) {
61 for (var key in elem[TERM_PT_ATTR_KEY]) {
62 if (!(key in attrs)) {
63 attrs[key] = _.cloneDeep(elem[TERM_PT_ATTR_KEY][key]);
64 }
65 }
66 }
67 });
68 return {
69 'node-id': node_id,
70 attrs: attrs
71 }
72 }
73
74 // Transforms one or more topologies into a collection of nodes, links
75 // usable by D3.js
76 function transformNetworkTopology(raw) {
77 var nodes = [];
78 var links = [];
79
80 var y = 0;
81 var id = 1;
82
83 var node_mapper = {};
84 var network_ids = [];
85
86 var networks = raw;
87 if ('collection' in raw) {
88 networks = raw.collection;
89 }
90 if ('ietf-network:network' in networks) {
91 networks = networks['ietf-network:network'];
92 }
93
94 // for each network in the array of networks
95 for (var net_index=0, tot_networks=networks.length; net_index < tot_networks; net_index++) {
96 var network = networks[net_index];
97 var network_id = networks[net_index]['network-id'];
98 if (network_ids.indexOf(network_id) == -1) {
99 network_ids.push(network_id);
100 }
101 var raw_nodes = network['node'] || [];
102 var x = 0;
103 for (var index=0, tot=raw_nodes.length; index < tot; index++) {
104 var termination_point = [];
105 var new_node = {
106 id: id,
107 // TODO: make short name,
108 name: raw_nodes[index]['node-id'],
109 full_name: raw_nodes[index]['node-id'],
110 network: network_id,
111 x: x,
112 y: y,
113 attr: terminationPointValues(raw_nodes[index]).attrs
114 };
115 nodes.push(new_node);
116 // What do wse do if the name is already collected
117 node_mapper[new_node.name] = nodes.length-1;
118 x += 20;
119 id++;
120 }
121
122 var raw_links = network['ietf-network-topology:link'];
123 if (raw_links) {
124 for (var index=0, tot=raw_links.length; index < tot; index++) {
125 var source_name = raw_links[index]['source']['source-node'];
126 var dest_name = raw_links[index]['destination']['dest-node'];
127 var new_link = {
128 source: node_mapper[source_name],
129 target: node_mapper[dest_name]
130 };
131 links.push(new_link);
132 }
133 }
134 // prep for next network
135 y += 20;
136 }
137
138 // D3 is expecting array indexes of the nodes for the links
139 // Now for the links to find the nodes
140
141 return {
142 nodes: nodes,
143 links: links,
144 network_ids: network_ids
145 };
146 }
147
148
149 module.exports = {
150 transformNetworkTopology: transformNetworkTopology,
151 mergeVnfrNfviMetrics: mergeVnfrNfviMetrics
152 }