2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2021 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 import {GlobalExtConstants} from './GlobalExtConstants.js';
22 let OXM = GlobalExtConstants.OXM;
24 function camelToDash(str) {
25 return (str.replace(/\W+/g, '-')
26 .replace(/([a-z\d])([A-Z])/g, '$1-$2')).toLowerCase();
29 function sortColumn(col1, col2) {
30 if (col1.value < col2.value) {
32 } else if (col1.value > col2.value) {
39 var buildAttrList = function(inputParam, cols) {
40 // console.log('[getSearchParam] inputParam: ' + inputParam);
41 let searchParam = inputParam;
43 if (['PSERVER', 'COMPLEX', 'CLOUDREGION',
44 'NETWORKPROFILE', 'VIRTUALDATACENTER'].indexOf(inputParam.toUpperCase()) === -1) {
45 searchParam = inputParam.substring(0, inputParam.length - 1);
46 // console.log('[getSearchParam] searchParam: ' + searchParam);
48 console.log('Before loop included',searchParam);
49 if('CHASSIES'.indexOf(inputParam.toUpperCase()) !== -1){
50 searchParam = inputParam.substring(0, inputParam.length - 2) + 's';
51 }else if(inputParam.substr(inputParam.length - 3) === 'ies'){
52 searchParam = inputParam.substring(0, inputParam.length - 3) + 'y';
53 }else if('COMPLEXES'.indexOf(inputParam.toUpperCase()) !== -1){
54 searchParam = inputParam.substring(0, inputParam.length - 2);
56 if(inputParam.toUpperCase() === 'PINTERFACES'){
57 searchParam = 'pInterface';
59 console.log('After loop included',searchParam);
60 if (inputParam.toUpperCase() === 'LINESOFBUSINESS') {
61 searchParam = 'lineOfBusiness';
62 // console.log('[getSearchParam] searchParam: ' + searchParam);
66 let oxmFile = JSON.parse(OXM);
67 let javaTypes = oxmFile['xml-bindings']['java-types'][0]['java-type'];
68 // console.log('[buildAttrList] javaTypes:');
69 // console.log(javaTypes);
72 for (let i = 0; i < javaTypes.length && foundIndex === -1; i++) {
73 if (javaTypes[i]['xml-root-element'][0]['$']['name'] === camelToDash(searchParam)) {
74 // console.log(javaTypes[i]);
79 //Adding Quick fix will be get rid of it later
80 console.log('searchParam>>>>>>>stage2:',inputParam);
81 if(foundIndex === -1){
82 for (let i = 0; i < javaTypes.length && foundIndex === -1; i++) {
83 if (javaTypes[i]['xml-root-element'][0]['$']['name'] === camelToDash(inputParam)) {
86 if(foundIndex === -1){
87 if(inputParam.substr(inputParam.length - 1) === 's'){
88 searchParam = inputParam.substring(0, inputParam.length - 1);
89 console.log('searchParam>>>>>>>stage3:',searchParam);
91 if (javaTypes[i]['xml-root-element'][0]['$']['name'] === camelToDash(searchParam)) {
98 // build the column list
100 if(foundIndex > -1) {
101 //for (let j = 0; j < javaTypes[foundIndex]['java-attributes'][0]['xml-element'].length; j++) {
102 // columns.push({value: javaTypes[foundIndex]['java-attributes'][0]['xml-element'][j]['$']['name']});
104 if (javaTypes[foundIndex]['java-attributes']) {
105 let elementLength = 0;
106 if (javaTypes[foundIndex]['java-attributes'][0]['xml-element']) {
107 elementLength = javaTypes[foundIndex]['java-attributes'][0]['xml-element'].length;
109 for (var j = 0; j < elementLength; j++) {
110 let isPrimitive = JSON.stringify(javaTypes[foundIndex]['java-attributes'][0]['xml-element'][j]['$']['type']).indexOf('java.lang') > -1;
111 if(isPrimitive) { //add to the list
112 let node = {value: javaTypes[foundIndex]['java-attributes'][0]['xml-element'][j]['$']['name']};
119 //sort the column list
120 columns.sort(sortColumn);
122 // console.log('[buildAttrList] columns:');
123 // console.log(columns);
128 var multiTypeBuildAttrList = function(nodes) {
130 let oxmFile = JSON.parse(OXM);
131 let javaTypes = oxmFile['xml-bindings']['java-types'][0]['java-type'];
133 let typeList = new Map();
135 nodes.forEach(node => {
136 let searchParam = node['node-type'];
139 for (let i = 0; i < javaTypes.length && foundIndex === -1; i++) {
140 if (javaTypes[i]['xml-root-element'][0]['$']['name'] === camelToDash(searchParam)) {
142 if (!typeList.has(searchParam)) {
145 //for (let j = 0; j < javaTypes[foundIndex]['java-attributes'][0]['xml-element'].length; j++) {
147 //value: javaTypes[foundIndex]['java-attributes'][0]['xml-element'][j]['$']['name']
150 if (javaTypes[foundIndex]['java-attributes']) {
151 let elementLength = 0;
152 if (javaTypes[foundIndex]['java-attributes'][0]['xml-element']) {
153 elementLength = javaTypes[foundIndex]['java-attributes'][0]['xml-element'].length;
155 for (var j = 0; j < elementLength; j++) {
156 let isPrimitive = JSON.stringify(javaTypes[foundIndex]['java-attributes'][0]['xml-element'][j]['$']['type']).indexOf('java.lang') > -1;
157 if(isPrimitive) { //add to the list
158 let node = {value: javaTypes[foundIndex]['java-attributes'][0]['xml-element'][j]['$']['name']};
164 props.sort(sortColumn);
165 typeList.set(searchParam, props);
171 // console.log('typeList:');
172 // console.log(typeList);
177 var buildCSV = function(columns, nodes) {
178 let outputArray = [];
181 for (let i = 0; i < columns.length; i++) {
182 let str = columns[i].value;
183 outputArray[0][str] = columns[i];
186 // console.log('[buildCSV] initial outputArray with columns:');
187 // console.log(outputArray);
189 // console.log('[buildCSV] nodes.length:');
190 // console.log(nodes.length);
193 for (let j = 1; j < nodes.length + 1; j++) {
195 for (var k in nodes[q].properties) {
196 if (nodes[q].properties.hasOwnProperty(k)) {
198 outputArray[j][str] = nodes[q].properties[k].toString();
201 let relative = nodes[q]['related-to'];
202 for(var r = 0; r<relative.length; r++){
203 var relationship = '';
204 var relationshipLabel = '';
207 for(var k in relative[r]){
208 let relation = k.toString();
209 if(relation === 'relationship-label'){
210 relationshipLabel = relative[r][k].toString().slice(33);
212 if(relation === 'node-type'){
213 nodeType = relative[r][k].toString();
215 if(relation === 'url'){
216 url = relative[r][k].toString();
218 relationship = 'Relationship ' + relationshipLabel + ' ' + nodeType + ' - ' + url;
219 console.log('relationship>>>>>',relationship);
222 let str = 'Relationship' + cnt;
223 outputArray[j][str] = relationship;
224 outputArray[0][str] ='Relationship'+cnt;
232 var multiTypebuildCSV = function(nodes) {
233 let outputArray = [];
235 nodes.forEach(node => {
237 for (var prop in node.properties) {
238 if (node.properties.hasOwnProperty(prop)) {
239 let val = { value: prop + ': ' + node.properties[prop].toString()};
244 nodeVal.sort(sortColumn);
245 nodeVal.unshift({ value: node['node-type'] }, {value : node.url });
246 let relative = node['related-to'];
247 for(var r = 0; r<relative.length; r++){
248 var relationship = '';
249 var relationshipLabel = '';
252 for(var k in relative[r]){
253 let relation = k.toString();
254 if(relation === 'relationship-label'){
255 relationshipLabel = relative[r][k].toString().slice(33);
257 if(relation === 'node-type'){
258 nodeType = relative[r][k].toString();
260 if(relation === 'url'){
261 url = relative[r][k].toString();
263 relationship = 'Relationship ' + relationshipLabel + ' ' + nodeType + ' - ' + url;
264 console.log('relationship>>>>>',relationship);
267 let val = { value: 'Relationship'+cnt + ': ' + relationship};
270 outputArray.push(nodeVal);
273 // console.log(outputArray);
277 var convertToCSVStr = function(outputArray) {
278 let array = (typeof outputArray !== 'object' ? JSON.parse(outputArray) : outputArray);
282 for (let index in outputArray[0]) {
283 // convert each value to string and comma-separated
286 row = row.slice(0, -1);
287 // append Label row with line break
290 for (let i = 1; i < array.length; i++) {
292 for (let k in outputArray[0]) {
293 if (outputArray[0].hasOwnProperty(k)) {
295 line += array[i][k] + ',';
301 str += line + '\r\n';
306 var multiTypeConvertToCSVStr = function(outputArray) {
307 let array = (typeof outputArray !== 'object' ? JSON.parse(outputArray) : outputArray);
310 // find max length of array to determine number of rows to loop through
311 array.forEach(node => {
312 if (node.length > max) {
317 // console.log('max: ' + max);
319 for (let i = 0; i < max; i++) {
321 for (let j = 0; j < array.length; j++) {
322 line += array[j][i] ? array[j][i].value + ',' : ',';
324 str += line + '\r\n';
329 var downloadCSV = function(csvStr, filename) {
330 // console.log(csvStr);
331 var a = document.createElement('a');
332 a.setAttribute('style', 'display:none;');
333 document.body.appendChild(a);
334 var blob = new Blob([csvStr], { type: 'text/csv' });
335 var url = window.URL.createObjectURL(blob);
337 a.download = filename + '.csv';
341 buildAttrList : buildAttrList,
342 multiTypeBuildAttrList : multiTypeBuildAttrList,
344 multiTypebuildCSV : multiTypebuildCSV,
345 convertToCSVStr : convertToCSVStr,
346 multiTypeConvertToCSVStr : multiTypeConvertToCSVStr,
347 downloadCSV : downloadCSV