Removing INSTAR functionality from CDT for ONAP.
[appc/cdt.git] / src / app / vnfs / build-artifacts / parameter-definitions / parameter.component.ts
1 /*
2 ============LICENSE_START==========================================
3 ===================================================================
4 Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
5 ===================================================================
6
7 Unless otherwise specified, all software contained herein is licensed
8 under the Apache License, Version 2.0 (the License);
9 you may not use this software except in compliance with the License.
10 You may obtain a copy of the License at
11
12     http://www.apache.org/licenses/LICENSE-2.0
13
14 Unless required by applicable law or agreed to in writing, software
15 distributed under the License is distributed on an "AS IS" BASIS,
16 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 See the License for the specific language governing permissions and
18 limitations under the License.
19
20 ECOMP is a trademark and service mark of AT&T Intellectual Property.
21 ============LICENSE_END============================================
22 */
23
24 import { Component, OnInit, ViewChild } from '@angular/core';
25 import { saveAs } from 'file-saver';
26 import { ParamShareService } from '../../../shared/services/paramShare.service';
27 import { MappingEditorService } from '../../../shared/services/mapping-editor.service';
28 import { ModalComponent } from '../../../shared/modal/modal.component';
29 import { HttpUtilService } from '../../../shared/services/httpUtil/http-util.service';
30 import { UtilityService } from '../../../shared/services/utilityService/utility.service';
31 import { environment } from '../../../../environments/environment';
32 import { NotificationsService } from 'angular2-notifications';
33 import { ParameterDefinitionService } from './parameter-definition.service';
34 import 'rxjs/add/operator/map';
35 import { NgProgress } from 'ngx-progressbar';
36
37
38 let YAML = require('yamljs');
39
40 declare var $: any;
41
42 @Component({
43     selector: 'parameter-form',
44     templateUrl: './parameter.component.html',
45     styleUrls: ['../reference-dataform/reference-dataform.component.css'],
46     providers: [ParameterDefinitionService]
47 })
48 export class ParameterComponent implements OnInit {
49     public paramForm: any;
50     public actionType: any;
51     public showFilterFields: boolean;
52     public filterByFieldvalues = [null, 'vm-number', 'vnfc-function-code'];
53     public ruleTypeConfiguaration = {
54         'vnf-name': [
55             {
56                 'key-name': 'unique-key-name',
57                 'key-value': 'parent-name'
58             },
59             {
60                 'key-name': 'unique-key-value',
61                 'key-value': 'vnf'
62             },
63             {
64                 'key-name': 'field-key-name',
65                 'key-value': 'vnf-name'
66             }
67         ],
68         'vm-name-list': [
69             {
70                 'key-name': 'unique-key-name',
71                 'key-value': 'parent-name'
72             },
73             {
74                 'key-name': 'unique-key-value',
75                 'key-value': 'vserver'
76             },
77             {
78                 'key-name': 'field-key-name',
79                 'key-value': 'vserver-name'
80             }
81         ],
82         'vnfc-name-list': [
83             {
84                 'key-name': 'unique-key-name',
85                 'key-value': 'parent-name'
86             },
87             {
88                 'key-name': 'unique-key-value',
89                 'key-value': 'vnfc'
90             },
91             {
92                 'key-name': 'field-key-name',
93                 'key-value': 'vnfc-name'
94             }
95         ],
96         'vnf-oam-ipv4-address': [
97             {
98                 'key-name': 'unique-key-name',
99                 'key-value': 'parent-name'
100             },
101             {
102                 'key-name': 'unique-key-value',
103                 'key-value': 'vnf'
104             },
105             {
106                 'key-name': 'field-key-name',
107                 'key-value': 'ipv4-oam-ipaddress'
108             }
109         ],
110         'vnfc-oam-ipv4-address-list': [
111             {
112                 'key-name': 'unique-key-name',
113                 'key-value': 'parent-name'
114             },
115             {
116                 'key-name': 'unique-key-value',
117                 'key-value': 'vnfc'
118             },
119             {
120                 'key-name': 'field-key-name',
121                 'key-value': 'ipaddress-v4-oam-vip'
122             }
123         ]
124     };
125     public requiredValues: boolean[] = [null, true, false];
126     public sourceValues = ['Manual', 'A&AI'];
127     public ruleTypeValues = [null, 'vnf-name', 'vm-name-list', 'vnfc-name-list', 'vnf-oam-ipv4-address', 'vnfc-oam-ipv4-address-list'];
128     public typeValues = [null, 'ipv4-address', 'ipv6-address', 'ipv4-prefix', 'ipv6-prefix'];
129     public responseKeyNameValues = ['', 'unique-key-name', 'unique-key-value', 'field-key-name'];
130     public responseKeyValues = ['(none)', 'addressfqdn', 'ipaddress-v4', 'ipaddress-v6'];
131     public requestKeyNameValues = [''];
132     public requestKeyValues = ['', '(none)'];
133     public myKeyFileName = null;
134     public myPdFileName = null;
135     public disposable: any;
136     public confirmation: boolean;
137     public showConfirmation: boolean;
138     public test: boolean;
139     apiToken = localStorage['apiToken'];
140     userId = localStorage['userId'];
141     public initialData: any;
142     public intialData: any;
143     public initialAction: any;
144     public item: any = {};
145     public subscription: any;
146     public Actions = [
147         { action: 'ConfigBackup', value: 'ConfigBackup' },
148         { action: 'ConfigModify', value: 'ConfigModify' },
149         { action: 'ConfigRestore', value: 'ConfigRestore' },
150         { action: 'Configure', value: 'Configure' },
151         { action: 'GetRunningConfig', value: 'GetRunningConfig' },
152         { action: 'HealthCheck', value: 'HealthCheck' },
153         { action: 'StartApplication', value: 'StartApplication' },
154         { action: 'StopApplication', value: 'StopApplication' }
155     ];
156     public uploadTypes = [{
157         value: 'External Key File',
158         display: 'KeyFile'
159     },
160     {
161         value: 'Pd File',
162         display: 'Pd File'
163     }
164     ];
165
166     options = {
167         timeOut: 1000,
168         showProgressBar: true,
169         pauseOnHover: true,
170         clickToClose: true,
171         maxLength: 200
172     };
173     public vnfcTypeData: string = '';
174     public selectedUploadType: string;
175     @ViewChild(ModalComponent) modalComponent: ModalComponent;
176     public title: string;
177     public parameterDefinitionMap: { [index: string]: string; } = {};
178     public parameterNameValues = {};
179     public displayParamObjects;
180     public modelParamDefinitionObjects;
181     public vnfType: any;
182     vnfcType: any;
183     protocol: any;
184     public refNameObj = {};
185     public action;
186     public artifactName;
187     public appDataObject: any;
188     public downloadDataObject: any;
189     public artifact_fileName;
190     identifier: any;
191     private selectedActionReference: any;
192
193     constructor (private httpService: HttpUtilService,
194         private parameterDefinitionService: ParameterDefinitionService,
195         private paramShareService: ParamShareService,
196         private mappingEditorService: MappingEditorService,
197         private httpUtil: HttpUtilService,
198         private utilService: UtilityService,
199         private nService: NotificationsService,
200         private ngProgress: NgProgress) {
201     }
202
203     ngOnInit() {
204         this.selectedActionReference = this.parameterDefinitionService.prepareFileName();
205         if (this.selectedActionReference && this.selectedActionReference != undefined) {
206
207             this.vnfType = this.selectedActionReference.scope['vnf-type'];
208             this.vnfcType = this.selectedActionReference.scope['vnfc-type'];
209             this.protocol = this.selectedActionReference['device-protocol'];
210             this.action = this.selectedActionReference.action;
211
212             for (let i = 0; i < this.selectedActionReference['artifact-list'].length; i++) {
213                 let artifactList = this.selectedActionReference['artifact-list'];
214                 if (artifactList[i]['artifact-type'] === 'parameter_definitions') {
215                     this.artifact_fileName = artifactList[i]['artifact-name'];
216                 }
217             }
218             this.parameterDefinitionService.setValues(this.vnfType, this.vnfcType, this.protocol, this.action, this.artifact_fileName);
219         }
220         else {
221             this.selectedActionReference = {
222                 'action': '',
223                 'scope': { 'vnf-type': '', 'vnfc-type': '' },
224                 'vm': [],
225                 'protocol': '',
226                 'download-dg-reference': '',
227                 'user-name': '',
228                 'port-number': '',
229                 'artifact-list': [],
230                 'deviceTemplate': '',
231                 'scopeType': ''
232             };
233         }
234
235         this.identifier = this.mappingEditorService.identifier;
236     }
237
238     ngAfterViewInit() {
239         if (this.mappingEditorService.latestAction) {
240             this.displayParamObjects = [];
241             this.modelParamDefinitionObjects = [];
242             if (this.paramShareService.getSessionParamData() != undefined && this.paramShareService.getSessionParamData().length > 0) {
243                 this.displayParamObjects = this.paramShareService.getSessionParamData();
244             } else {
245                 this.getPD();
246             }
247         } else {
248             this.nService.error('Error', 'Please enter Action and VNF type in Reference Data screen');
249         }
250         return this.displayParamObjects;
251     }
252
253
254     public getPD() {
255         let result: any;
256         let input = {
257             'input': {
258                 'design-request': {
259                     'request-id': this.apiToken,
260                     'action': 'getArtifact',
261                     'payload': '{"userID": "' + this.userId + '", "vnf-type" : "' + this.vnfType + '", "artifact-type":"APPC-CONFIG", "artifact-name":"' + this.artifact_fileName + '"}'
262                 }
263             }
264         };
265         let artifactContent: any;
266         this.ngProgress.start();
267         return this.httpService.post({
268             url: environment.getDesigns,
269             data: input
270         }).subscribe(data => {
271             if (this.utilService.checkResult(data)) {
272                 let result: any = JSON.parse(data.output.data.block).artifactInfo[0];
273                 var pdObject = YAML.parse(result['artifact-content']);
274                 let fileModel = pdObject['vnf-parameter-list'];
275                 this.displayParamObjects = this.parameterDefinitionService.populatePD(fileModel);
276             }
277             else {
278
279             }
280             this.ngProgress.done();
281         },
282
283             error => this.nService.error('Error', 'Error in connecting APPC Server'));
284
285     }
286
287     //========================== End of NGInit() Method============================================
288     selectedNavItem(item: any) {
289         this.item = item;
290     }
291
292     //========================== End of selectedNavItem() Method============================================
293     browsePdFile() {
294         $('#inputFile1').trigger('click');
295     }
296
297     //========================== End of browsePdFile() Method============================================
298     browseKeyFile() {
299         $('#inputFile2').trigger('click');
300
301     }
302
303     //========================== End of browseKeyFile() Method============================================
304
305
306     //========================== End of appendSlashes() Method============================================
307
308
309     //========================== End of prepareFileName() Method============================================
310     ngOnDestroy() {
311         this.parameterDefinitionService.destroy(this.displayParamObjects);
312     }
313
314     //========================== End of ngOnDestroy() Method============================================
315
316     //========================== End of createOrUpdateParameterDefinitionData() Method============================================
317     public showUpload() {
318         this.selectedUploadType = this.uploadTypes[0].value;
319     };
320
321     //========================== End of showUpload() Method============================================
322     //This is called when the user selects new files from the upload button
323     public fileChange(input, uploadType) {
324         if (input.files && input.files[0]) {
325             // Create the file reader
326             let reader = new FileReader();
327             this.readFile(input.files[0], reader, (result) => {
328                 if ('pdfile' === uploadType) {
329                     this.myPdFileName = input.files[0].name;
330                     this.displayParamObjects = this.parameterDefinitionService.processPDfile(this.myPdFileName, result);
331                 }
332             });
333         } else {
334             //this.notificationService.notifyErrorMessage('Failed to read file!Please try again.');
335         }
336     }
337
338     //========================== End of fileChange() Method============================================
339     public readFile(file, reader, callback) {
340         // Set a callback funtion to fire after the file is fully loaded
341         reader.onload = () => {
342             // callback with the results
343             callback(reader.result);
344         };
345         // Read the file
346         reader.readAsText(file, 'UTF-8');
347     }
348
349     //========================== End of readFile() Method============================================
350     fileChangeEvent(fileInput: any) {
351         let obj: any = fileInput.target.files;
352     }
353
354
355     sourceChanged(data, obj) {
356         if (data == 'A&AI') {
357             obj.ruleTypeValues = [null, 'vnf-name', 'vm-name-list', 'vnfc-name-list', 'vnf-oam-ipv4-address', 'vnfc-oam-ipv4-address-list'];
358             for (let x = 0; x < 5; x++) {
359                 obj['response-keys'][x]['key-name'] = null;
360                 obj['response-keys'][x]['key-value'] = null;
361             }
362         } else if (data == 'Manual') {
363             obj.ruleTypeValues = [null];
364             obj['rule-type'] = null;
365             obj.showFilterFields = false;
366             for (let x = 0; x < 5; x++) {
367                 obj['response-keys'][x]['key-name'] = null;
368                 obj['response-keys'][x]['key-value'] = null;
369             }
370         }
371         else {
372             obj.ruleTypeValues = [null];
373         }
374     }
375
376     //========================== End of sourceChanged() Method============================================
377     ruleTypeChanged(data, obj) {
378         if (data == null || data == undefined || data == 'null') {
379             obj.showFilterFields = false;
380             obj['rule-type'] = null;
381             for (let x = 0; x < 5; x++) {
382                 obj['response-keys'][x]['key-name'] = null;
383                 obj['response-keys'][x]['key-value'] = null;
384             }
385         } else {
386             let sourceObject = this.ruleTypeConfiguaration[data];
387             if (data == 'vm-name-list' || data == 'vnfc-name-list' || data == 'vnfc-oam-ipv4-address-list') {
388                 this.showFilterFields = false;
389                 obj.showFilterFields = true;
390                 this.filetrByFieldChanged(obj['response-keys'][3]['key-value'], obj);
391             } else {
392                 obj.showFilterFields = false;
393                 obj['response-keys'][3]['key-name'] = null;
394                 obj['response-keys'][3]['key-value'] = null;
395                 obj['response-keys'][4]['key-name'] = null;
396                 obj['response-keys'][4]['key-value'] = null;
397             }
398             for (let x = 0; x < sourceObject.length; x++) {
399                 obj['response-keys'][x]['key-name'] = sourceObject[x]['key-name'];
400                 obj['response-keys'][x]['key-value'] = sourceObject[x]['key-value'];
401             }
402         }
403
404     }
405
406     //========================== End of ruleTypeChanged() Method============================================
407     filetrByFieldChanged(data, obj) {
408         if (data == null || data == undefined || data == 'null') {
409             obj.enableFilterByValue = false;
410             obj['response-keys'][4]['key-value'] = null;
411         } else {
412             obj.enableFilterByValue = true;
413         }
414
415     }
416
417
418 }