summaryrefslogtreecommitdiffstats
path: root/src/app/vnfs/build-artifacts/parameter-definitions/parameter-definition.service.ts
blob: 14af6ab47e7ab0179ddcd9f43eefdcc64d2ee6fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/*
============LICENSE_START==========================================
===================================================================
Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.

Copyright (C) 2018 IBM.
===================================================================

Unless otherwise specified, all software contained herein is licensed
under the Apache License, Version 2.0 (the License);
you may not use this software except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
============LICENSE_END============================================
*/


import {Injectable, ViewChild} from '@angular/core';
import {saveAs} from 'file-saver';
import {ParamShareService} from '../../../shared/services/paramShare.service';
import {MappingEditorService} from '../../../shared/services/mapping-editor.service';
import {ModalComponent} from '../../../shared/modal/modal.component';
import {UtilityService} from '../../../shared/services/utilityService/utility.service';
import {NotificationsService} from 'angular2-notifications';
import 'rxjs/add/operator/map';
import { appConstants } from '../../../../constants/app-constants';

let YAML = require('yamljs');

declare var $: any;

@Injectable()
export class ParameterDefinitionService {

    public vnfcTypeData: string = '';
    public selectedUploadType: string;
    @ViewChild(ModalComponent) modalComponent: ModalComponent;
    public title: string;
    public parameterDefinitionMap: { [index: string]: string; } = {};
    public parameterNameValues = {};
    public displayParamObjects;
    public modelParamDefinitionObjects;
    public vnfType: any;
    vnfcType: any;
    protocol: any;
    public refNameObj = {};
    public action;
    public artifactName;
    public type;
    public appDataObject: any;
    public downloadDataObject: any;
    public artifact_fileName;
    identifier: any;
    public myKeyFileName = null;
    public myPdFileName = null;
    private selectedActionReference: any;
    private apiToken = localStorage['apiToken'];
    private userId = localStorage['userId'];
    constructor(private mappingEditorService: MappingEditorService,
                private paramShareService: ParamShareService,
                private nService: NotificationsService,
                private utilService: UtilityService) {
    }

    public initialize() {

    }

    public setValues(vnfType, vnfcType, protocol, action, artifactName) {
        this.vnfType = vnfType;
        this.vnfcType = vnfcType;
        this.protocol = protocol;
        this.action = action;
        this.artifact_fileName = artifactName;
    }

    public afterInit(artifactName, displayParamObjects) {

    }

    //========================== End of saveChanges() Method============================================
    /* Saves pd to appc */
    public sendPD(yamlString: String) {
        let result: any;
        let payload = '{"userID": "' + this.userId + '","vnf-type" : "' + this.vnfType + '","action" : "' + this.action + '","artifact-name" : "' + this.artifact_fileName + '","artifact-type" : "APPC-CONFIG","artifact-version" : "0.0.1","artifact-contents" : ' + yamlString + '}';
        let input = {
            'input': {
                'design-request': {
                    'request-id': this.apiToken,
                    'action': 'uploadArtifact',
                    'payload': payload
                }
            }
        };
        this.appDataObject.pd = input;
    }


    //========================== End of filetrByFieldChanged() Method============================================
    removeUnwantedvalues(obj) {
        let result = Object.assign({}, obj);
        result['vnf-parameter-list'].forEach(obj => {
            delete obj['ruleTypeValues'];
            delete obj['showFilterFields'];
            delete obj['enableFilterByValue'];
        });
        return result;
    }

    //========================== End of removeUnwantedvalues() Method============================================
    populateDataUponSource(displayParamObjects) {
        displayParamObjects.forEach(parameter => {
            if (parameter.source == 'A&AI') {
                parameter.ruleTypeValues = [null, 'vnf-name', 'vm-name-list', 'vnfc-name-list', 'vnf-oam-ipv4-address', 'vnfc-oam-ipv4-address-list'];
                if (parameter['rule-type'] == 'vm-name-list' || parameter['rule-type'] == 'vnfc-name-list' || parameter['rule-type'] == 'vnfc-oam-ipv4-address-list') {
                    parameter.showFilterFields = true;
                    parameter.enableFilterByValue = false;
                } else {
                    parameter.showFilterFields = false;
                }

            } else if (parameter.source == 'Manual') {
                parameter.ruleTypeValues = [null];
            }
            else {
                parameter.ruleTypeValues = [parameter['rule-type']];
            }
        });

    }

    //========================== End of getPD() Method============================================
    populatePD(result: any) {
        let fileContent = JSON.stringify(result);
        //Added code to deserialize, serialize and format the response keys for display purposes ??May be unneessary?? To Do: - Check 
        let fileObj = JSON.parse(fileContent);
        this.displayParamObjects = this.formatFileContentForDisplay(fileObj);
        this.populateDataUponSource(this.displayParamObjects);
        this.formatResponseForKey(this.displayParamObjects);
        if (undefined !== this.displayParamObjects)
            this.modelParamDefinitionObjects = this.displayParamObjects;
        if (this.displayParamObjects !== undefined && this.displayParamObjects.length > 0) {
            this.paramShareService.setSessionParamData(this.displayParamObjects);
        }
        return this.displayParamObjects;
    }

    //========================== End of populatePD() Method============================================
    /* Formats each object read from YAML file as per page expectations */
    formatResponseForKey(param: any[]) {
        for (var i = 0; i < param.length; i++) {
            this.formatKeys(param[i]);
        }
    }

    //========================== End of formatResponseForKey() Method============================================
    /* Formats for responsekeys of each object */
    formatKeys(parameterDefinitionObject: any) {
        if (null == parameterDefinitionObject || undefined === parameterDefinitionObject)
            return;
        if (null == parameterDefinitionObject['response-keys'])
            parameterDefinitionObject['response-keys'] = [{}];
        for (var j = 0; j < 5; j++) {
            var keysObj = {
                'key-name': null,
                'key-value': null
            };
            if (undefined == parameterDefinitionObject['response-keys'][j] || null == parameterDefinitionObject['response-keys'][j]) {
                parameterDefinitionObject['response-keys'].push(keysObj);
            }
            if (undefined == parameterDefinitionObject['response-keys'][j]['key-name']) {
                parameterDefinitionObject['response-keys'][j]['key-name'] = null;
            }
            if (undefined == parameterDefinitionObject['response-keys'][j]['key-value']) {
                parameterDefinitionObject['response-keys'][j]['key-value'] = null;
            }
        }
        if (null == parameterDefinitionObject['request-keys'])
            parameterDefinitionObject['request-keys'] = [{}];
        for (var k = 0; k < 3; k++) {
            var keysObj = {
                'key-name': null,
                'key-value': null
            };
            if (undefined == parameterDefinitionObject['request-keys'][k] || null == parameterDefinitionObject['request-keys'][k]) {
                parameterDefinitionObject['request-keys'].push(keysObj);
            }
            if (undefined == parameterDefinitionObject['request-keys'][k]['key-name']) {
                parameterDefinitionObject['request-keys'][k]['key-name'] = null;
            }
            if (undefined == parameterDefinitionObject['request-keys'][k]['key-value']) {
                parameterDefinitionObject['request-keys'][k]['key-value'] = null;
            }
        }
    }

    //========================== End of formatKeys() Method============================================
    //Send null if there are no keys present - Check with key names being absent
    formatKeysForFileGeneration() {
        for (var i = 0; i < this.modelParamDefinitionObjects.length; i++) {
            if (this.modelParamDefinitionObjects[i]['response-keys'][0]['key-name'] == null && this.modelParamDefinitionObjects[i]['response-keys'][1]['key-name'] == null && this.modelParamDefinitionObjects[i]['response-keys'][2]['key-name'] == null)
                this.modelParamDefinitionObjects[i]['response-keys'] = null;
            if (this.modelParamDefinitionObjects[i]['request-keys'][0]['key-name'] == null && this.modelParamDefinitionObjects[i]['request-keys'][1]['key-name'] == null && this.modelParamDefinitionObjects[i]['request-keys'][2]['key-name'] == null)
                this.modelParamDefinitionObjects[i]['request-keys'] = null;
        }
    }

    //========================== End of formatKeysForFileGeneration() Method============================================
    /* Fn to restore response keys in desired format per backend consumption*/
    processResponseKeys(saveModel: any[]) {
        for (var i = 0; i < saveModel.length; i++) {
            if (saveModel[i]['response-keys'] != null) {
                saveModel[i]['response-keys-new'] = [{}];
                saveModel[i]['response-keys-new'][0] = {};//An array of objects ?? so accessing first element
                if (undefined != saveModel[i]['response-keys'][0]['key-name'] && undefined != saveModel[i]['response-keys'][0]['key-value']) {
                    let keyName1 = saveModel[i]['response-keys'][0]['key-name'];
                    saveModel[i]['response-keys-new'][0][keyName1] = saveModel[i]['response-keys'][0]['key-value'];
                }
                if (undefined != saveModel[i]['response-keys'][1]['key-name'] && undefined != saveModel[i]['response-keys'][1]['key-value']) {
                    let keyName2 = saveModel[i]['response-keys'][1]['key-name'];
                    saveModel[i]['response-keys-new'][0][keyName2] = saveModel[i]['response-keys'][1]['key-value'];
                }
                if (undefined != saveModel[i]['response-keys'][2]['key-name'] && undefined != saveModel[i]['response-keys'][2]['key-value']) {
                    let keyName3 = saveModel[i]['response-keys'][2]['key-name'];
                    saveModel[i]['response-keys-new'][0][keyName3] = saveModel[i]['response-keys'][2]['key-value'];
                }
                if (saveModel[i]['response-keys'][3]['key-value'] != undefined && saveModel[i]['response-keys'][3]['key-value'] != null) {
                    let keyName4 = saveModel[i]['response-keys'][3]['key-name'];
                    saveModel[i]['response-keys-new'][0]['filter-by-key'] = saveModel[i]['response-keys'][3]['key-value'];
                }
                if (saveModel[i]['response-keys'][4]['key-value'] != undefined && saveModel[i]['response-keys'][4]['key-value'] != null) {
                    let keyName4 = saveModel[i]['response-keys'][4]['key-name'];
                    saveModel[i]['response-keys-new'][0]['filter-by-value'] = saveModel[i]['response-keys'][4]['key-value'];
                }
            }
            else {
                saveModel[i]['response-keys-new'] = null;
            }
            delete saveModel[i]['response-keys'];
            saveModel[i]['response-keys'] = saveModel[i]['response-keys-new'];
            delete saveModel[i]['response-keys-new'];
        }
        return saveModel;
    }

    //========================== End of processResponseKeys() Method============================================
    /*Fn to format response keys for front end display */
    formatFileContentForDisplay(fileModel: any[]) {
        for (var i = 0; i < fileModel.length; i++) {
            if (undefined != fileModel[i]['response-keys']) {
                let testObj = fileModel[i]['response-keys'];
                let keyNum = 0;
                fileModel[i]['response-keys-new'] = [{}];
                for (var prop in testObj[0]) {
                    if (testObj[0].hasOwnProperty(prop)) {
                        let key = prop;
                        fileModel[i]['response-keys-new'][keyNum] = {};
                        fileModel[i]['response-keys-new'][keyNum]['key-name'] = key;
                        fileModel[i]['response-keys-new'][keyNum]['key-value'] = testObj[0][key];
                    }
                    keyNum++;
                }
                delete fileModel[i]['response-keys'];
                fileModel[i]['response-keys'] = fileModel[i]['response-keys-new'];
                delete fileModel[i]['response-keys=new'];
            }
        }
        return fileModel;
    }

    //========================== End of openModal() Method============================================
    getCorrectParameterDefinitionObject(paramName: string) {
        var result = {
            'obj': {},
            'present': false
        };
        for (var i = 0; i < this.modelParamDefinitionObjects.length; i++) {
            var paramObj = this.modelParamDefinitionObjects[i];
            if (paramObj.name === paramName) {
                result.obj = this.modelParamDefinitionObjects[i];
                result.present = true;
                return result;
            }
        }
        var parameterDefinitionObject = {
            'name': paramName,
            'type': null,
            'description': null,
            'required': null,
            'default': null,
            'source': null,
            'rule-type': null,
            'response-keys': [{}],
            'request-keys': [{}]
        };
        result.obj = parameterDefinitionObject;
        result.present = false;
        return result;
    }

   
    public prepareFileName(): any {
        let fileNameObject: any = this.mappingEditorService.latestAction;
        this.appDataObject = this.mappingEditorService.appDataObject;
        this.downloadDataObject = this.mappingEditorService.downloadDataObject;
        return fileNameObject;
    }

    public destroy(displayParamObjects) {
        this.displayParamObjects = displayParamObjects;
        if (this.mappingEditorService.referenceNameObjects) {
            this.saveChanges('send');
            this.saveChanges('download');
            this.mappingEditorService.changeNavAppData(this.appDataObject);
            this.mappingEditorService.changeNavDownloadData(this.downloadDataObject);
        }
    }

    //========================== End of fileChangeEvent() Method============================================
    /* Saves pd file in YAML format */
    public saveChanges(downLoadOrSend: String) {
        if (undefined != this.displayParamObjects && null != this.displayParamObjects && this.displayParamObjects.length > 0) {
            this.paramShareService.setSessionParamData(this.displayParamObjects);

            //Generate File Name per given rules - if not, return without saving
            this.modelParamDefinitionObjects = this.displayParamObjects.slice(0);
            this.paramShareService.setDisplayData(this.displayParamObjects);
            this.formatKeysForFileGeneration();
            //Added code to serialize, deserialize and then make changes needed to save response keys as needed in pd file
            let jsonString = JSON.stringify(this.modelParamDefinitionObjects, null, '\t');
            jsonString = jsonString.replace(/"null"/g, 'null');
            let saveModel = JSON.parse(jsonString);
            let pdFileObject = this.processResponseKeys(saveModel);
                let yamlObject = {
                    'kind': 'Property Definition',
                    'version': 'V1',
                    'vnf-parameter-list': []
                };
                yamlObject['vnf-parameter-list'] = pdFileObject;
                yamlObject = this.removeUnwantedvalues(yamlObject);
                let yamlStringTemp = YAML.stringify(yamlObject, 6, 1);
                var re = /\'/gi;
                var newYamlStringTemp = yamlStringTemp.replace(re, '"');
                var re2 = / -\n  +/gi;
                var newYamlStringTemp2 = newYamlStringTemp.replace(re2, '- ');
                let yamlString = '---\n' + newYamlStringTemp2;
                if (downLoadOrSend === 'download') {
                    var blob = new Blob([yamlString], {
                        type: 'text/plain'
                    });
                    //let fileName = "pd_" + this.action + "_" + this.type + "_0.0.1V.yaml"
                    this.downloadDataObject.pd.pdData = yamlString;
                    this.downloadDataObject.pd.pdFileName = this.artifact_fileName;
                }
                else {
                    this.sendPD(JSON.stringify(yamlString));
                }
            
            //Restore Keys for display
            for (var i = 0; i < this.modelParamDefinitionObjects.length; i++) {
                this.formatKeys(this.modelParamDefinitionObjects[i]);
            }
        }

    }


    //This method will create parameter definitions as an array of objects from template name-value pairs and associative array for value from external key file if present
    createOrUpdateParameterDefinitionData(usecase) {
        this.parameterNameValues = JSON.parse(localStorage['paramsContent']);
        this.parameterDefinitionMap = this.paramShareService.getData();
        //Return if there are no name-value pairs or send some alert notification
        if (undefined != this.modelParamDefinitionObjects && this.modelParamDefinitionObjects.length > 0 && usecase == 'create') {
            //Do not recreate if object is already created
            return;
        }
        else {
        }
        this.parameterDefinitionMap = this.paramShareService.getData();
        //To Do:: Add Check for empty parameterDefinitionmap
        var nameValueObj = {}, pName, pValue;
        for (var key in this.parameterNameValues) {
            if (this.parameterNameValues.hasOwnProperty(key)) {
                pName = key;
                pValue = this.parameterNameValues[key];
            }

            if (this.parameterDefinitionMap !== undefined)
            //Check if parameter exists - if so, just update the keys, else create new object
                var result = this.getCorrectParameterDefinitionObject(pName);
            var parameterDefinitionObject = result.obj;
            if (parameterDefinitionObject['source'] != 'A&AI' && (undefined !== this.parameterDefinitionMap) && (undefined !== this.parameterDefinitionMap[pValue.toUpperCase()])) {
                var fields = this.parameterDefinitionMap[pValue.toUpperCase()].split('|');
                //Starts with 2, first vallue is source, second is rule-type
                let respInd = 0;
                for (var i = 2; i < fields.length; i += 2) {
                    parameterDefinitionObject['response-keys'][respInd] = {};
                    parameterDefinitionObject['response-keys'][respInd]['key-name'] = fields[i];
                    if ((i + 1) < fields.length) {
                        parameterDefinitionObject['response-keys'][respInd]['key-value'] = fields[i + 1];
                    }
                    respInd++;
                }
                parameterDefinitionObject['source'] = fields[0];
                parameterDefinitionObject['rule-type'] = fields[1];
            } 
            this.formatKeys(parameterDefinitionObject); //Ensure there are 3 elements for response-keys, request-keys for display purposes
            if (!result.present) { //only push if not present
                this.modelParamDefinitionObjects.push(parameterDefinitionObject);
            }
        }
        for (var indx in this.modelParamDefinitionObjects) {
            if (this.modelParamDefinitionObjects[indx] != undefined && (this.modelParamDefinitionObjects[indx].source == undefined || this.modelParamDefinitionObjects[indx].source == null || this.modelParamDefinitionObjects[indx].source == '')) {
                this.modelParamDefinitionObjects[indx].source = 'Manual';
            }
        }
        this.displayParamObjects = this.modelParamDefinitionObjects.slice(0);
        this.paramShareService.setDisplayData(this.displayParamObjects);
    }

    public processKeyFile(fileName, result) {
        this.myKeyFileName = fileName;
        if (!this.myKeyFileName.endsWith('.txt')) {
            this.nService.error(appConstants.notifications.titles.error, appConstants.errors.notTxtFileError);
        }
        this.parameterDefinitionMap = {};
        var rows = result.split(/\r\n|\r|\n/g);
        for (var i = 1; i < rows.length; i++) { //Omit headings, so start from 1
            let ind = rows[i].indexOf('|');
            let key = rows[i].slice(0, ind);
            let value = rows[i].slice(ind + 1);
            this.parameterDefinitionMap[key.toUpperCase()] = value;
        }
        this.paramShareService.setData(this.parameterDefinitionMap);
        //this.notificationService.notifySuccessMessage('External Key file successfully uploaded..');
        let sessionVar = [{}];
        sessionVar = this.paramShareService.getSessionParamData();
        if (sessionVar !== undefined && sessionVar != null && sessionVar.length > 0) {
            if (undefined == this.displayParamObjects)
                this.displayParamObjects = this.modelParamDefinitionObjects = [];
            this.displayParamObjects = sessionVar;
            this.modelParamDefinitionObjects = this.displayParamObjects;
            if (localStorage['paramsContent'] && (undefined !== this.displayParamObjects) && (this.displayParamObjects.length > 0)) {
                this.createOrUpdateParameterDefinitionData('update');
                //update the session variable with the updated data
                this.paramShareService.setSessionParamData(this.displayParamObjects);
            }
        } else {
            this.displayParamObjects = this.modelParamDefinitionObjects = [];
        }
        this.populateDataUponSource(this.displayParamObjects);
        return this.displayParamObjects;
    }

    public processPDfile(fileName, result) {
        this.myPdFileName = fileName;
        if (!this.myPdFileName.endsWith('.yaml')) {
            this.nService.error(appConstants.notifications.titles.error, appConstants.errors.notYAMLFileError);
        }
        var pdObject = YAML.parse(result);
        let fileModel = pdObject['vnf-parameter-list'];
        this.populatePD(fileModel);
        return this.displayParamObjects;
    }
}