Rift.IO OSM R1 Initial Submission
[osm/UI.git] / skyquake / plugins / launchpad / api / epa_aggregator.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 EpaSchema = {
19 "vswitch-epa": {
20 "objType": "container",
21 "ovs-offload": {
22 "objType": "leaf"
23 },
24 "ovs-acceleration": {
25 "objType": "leaf"
26 }
27 },
28 "hypervisor-epa": {
29 "objType": "container",
30 "type": {
31 "objType": "leaf"
32 },
33 "version": {
34 "objType": "leaf"
35 }
36 },
37 "host-epa": {
38 "objType": "container",
39 "cpu-model": {
40 "objType": "leaf"
41 },
42 "cpu-arch": {
43 "objType": "leaf"
44 },
45 "cpu-vendor": {
46 "objType": "leaf"
47 },
48 "cpu-socket-count": {
49 "objType": "leaf"
50 },
51 "cpu-core-count": {
52 "objType": "leaf"
53 },
54 "cpu-feature": {
55 "objType": "leaf-list"
56 }
57 },
58 "guest-epa": {
59 "objType": "container",
60 "mempage-size": {
61 "objType": "leaf"
62 },
63 "trusted-execution": {
64 "objType": "leaf"
65 },
66 "cpu-pinning-policy": {
67 "objType": "leaf"
68 },
69 "cpu-thread-pinning-policy": {
70 "objType": "leaf"
71 },
72 "pcie-device": {
73 "objType": "list"
74 },
75 "numa-node-policy": {
76 "objType": "container",
77 "node": {
78 "objType": "list"
79 }
80 }
81 }
82 }
83
84 module.exports = function aggregateEPA(vdur, data) {
85 var data = data || {};
86 var schema = EpaSchema;
87 var findLeaf = function(obj, schema, data) {
88 for (k in obj) {
89 var epaSchema = schema[k];
90 //if key exists in schema
91 if (epaSchema) {
92 if (!data[k]) {
93 data[k] = {};
94 }
95 switch (epaSchema.objType) {
96 case 'container':
97 findLeaf(obj[k], epaSchema, data[k]);
98 break;
99 case 'leaf':
100 aggregateValue(obj[k].toString(), data[k]);
101 break;
102 case 'leaf-list':
103 aggregateLeafListValue(obj[k], data[k]);
104 break;
105 case 'list':
106 aggregateListValue(k, obj[k], data[k]);
107 break;
108 case 'choice':
109 aggregateChoiceValue(obj[k], data[k]);
110 break;
111 }
112 }
113 }
114 return data;
115 }
116 try {
117 vdur.map(function(vm) {
118 findLeaf(vm, schema, data)
119 })
120 }
121 catch(e) {
122
123 }
124
125 return data;
126
127 function aggregateChoiceValue(obj, data) {
128 for (k in obj) {
129 aggregateValue(k, data)
130 }
131 }
132
133 function aggregateValue(value, data) {
134 value = value.toString();
135 if (!data[value]) data[value] = {};
136 var total = data[value];
137 total.total = (!total.total) ? 1 : total.total + 1;
138 }
139
140 function aggregateListValue(k, value, data) {
141 if (value.length > 0) {
142 aggregateValue(k, data);
143 }
144 }
145
146 function aggregateLeafListValue(obj, data) {
147 if (obj.constructor.name == "Array") {
148 obj.map(function(p) {
149 aggregateValue(p, data);
150 })
151 } else {
152 console.log("Oops, something went wrong here. An object was passed that wasn't an Array. Are you sure it was leaf-list?")
153 }
154 }
155 }