2 * # ============LICENSE_START=======================================================
3 * # Copyright (c) 2020 AT&T Intellectual Property. All rights reserved.
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
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.
16 * # ============LICENSE_END=========================================================
19 import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';
20 import { SpecValidationService } from '../services/spec-validation.service';
21 import { DownloadService } from '../services/download.service';
22 import { Ng4LoadingSpinnerService } from 'ng4-loading-spinner';
25 selector: 'app-comp-spec-validation',
26 templateUrl: './comp-spec-validation.component.html',
27 styleUrls: ['./comp-spec-validation.component.css']
29 export class CompSpecValidationComponent implements OnInit {
31 @ViewChild('myFile', {static: false})
32 myInputVariable: ElementRef;
34 spec_validator_action;
37 shouldValidate = false;
38 shouldDownload = false;
39 validCompSpec = false;
40 specValidated = false;
41 specValidationOutputSummary: any;
43 constructor(private specValidator: SpecValidationService, private downloadService: DownloadService, private spinnerService: Ng4LoadingSpinnerService ) { }
49 compSpecSelected: any;
50 onCompSpecUpload(event){
51 this.compSpecSelected = event.target.files[0];
52 this.readCsFileContent(this.compSpecSelected);
55 compSpecContent: any = null;
56 readCsFileContent(file) {
58 let fileReader = new FileReader();
59 fileReader.onload = (e) => { this.compSpecContent = fileReader.result; };
60 fileReader.readAsText(file);
64 validateRadioButton(){
65 this.shouldValidate = true;
66 this.shouldDownload = false;
69 downloadRadioButton(){
70 this.shouldValidate = false;
71 this.shouldDownload = true;
72 this.compSpecContent = null;
73 this.specValidated = false
77 this.myInputVariable.nativeElement.value = "";
78 this.compSpecContent = null
79 this.compSpecSelected = null
80 this.specValidated = false
83 specValidationOutputHeader = ''
84 specValidationOutputMessage = '';
86 this.specValidationOutputHeader = ""
87 this.specValidationOutputMessage = ""
88 this.specValidationOutputSummary = ""
90 this.spinnerService.show()
91 this.validateJsonStructure()
92 this.specValidator.sendSpecFile(this.compSpecContent, this.type, this.release).subscribe(
94 this.setSpecValidationMessage(err)
99 setSpecValidationMessage(res){
100 if(res.status === 200){
101 this.specValidationOutputHeader = "Success: Valid Component Spec"
102 this.specValidationOutputMessage = ""
103 this.validCompSpec = true
105 this.specValidationOutputHeader = `${res.status} Error: Invalid Component Spec`
106 this.specValidationOutputSummary = res.error.summary
108 for(let item of res.error.errors){
109 this.specValidationOutputMessage += `- ${item}\n\n`
112 this.validCompSpec = false
115 this.specValidated = true;
116 this.spinnerService.hide()
119 validateJsonStructure() {
121 JSON.parse(this.compSpecContent);
123 this.specValidationOutputHeader = "Error: Invalid Component Spec"
124 this.specValidationOutputSummary = "JSON Structure Error"
125 this.specValidationOutputMessage = error
126 this.validCompSpec = false
127 this.specValidated = true
128 this.spinnerService.hide()
129 throw new Error('JSON Structure error, quit!');
134 this.spinnerService.show()
135 this.specValidator.getSchema(this.type).subscribe(
137 this.downloadService.downloadJSON(res, `${this.release}+_${this.type}_Schema`)
138 this.spinnerService.hide()
141 this.spinnerService.hide()