3 * Copyright 2016 RIFT.IO Inc
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 var _
= require('lodash');
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
26 function mergeVnfrNfviMetrics(vnfr
, nsr
) {
27 for (var i
= 0; i
< vnfr
.length
; i
++) {
28 var title
= vnfr
[i
].title
;
30 for (var j
= 0; j
< nsr
.length
; j
++) {
31 if (nsr
[j
] && nsr
[j
].title
== title
) {
33 nsr
[j
].data
= nsr
[j
].data
.concat(vnfr
[i
].data
).sort(function(a
,b
) {
34 return (a
.name
> b
.name
) ? 1 : -1;
48 function terminationPointValues (node
) {
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';
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'])
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
]);
74 // Transforms one or more topologies into a collection of nodes, links
76 function transformNetworkTopology(raw
) {
87 if ('collection' in raw
) {
88 networks
= raw
.collection
;
90 if ('ietf-network:network' in networks
) {
91 networks
= networks
['ietf-network:network'];
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
);
101 var raw_nodes
= network
['node'] || [];
103 for (var index
=0, tot
=raw_nodes
.length
; index
< tot
; index
++) {
104 var termination_point
= [];
107 // TODO: make short name,
108 name
: raw_nodes
[index
]['node-id'],
109 full_name
: raw_nodes
[index
]['node-id'],
113 attr
: terminationPointValues(raw_nodes
[index
]).attrs
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;
122 var raw_links
= network
['ietf-network-topology:link'];
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'];
128 source
: node_mapper
[source_name
],
129 target
: node_mapper
[dest_name
]
131 links
.push(new_link
);
134 // prep for next network
138 // D3 is expecting array indexes of the nodes for the links
139 // Now for the links to find the nodes
144 network_ids
: network_ids
150 transformNetworkTopology
: transformNetworkTopology
,
151 mergeVnfrNfviMetrics
: mergeVnfrNfviMetrics