Merge "Add code for data format webtool"
[dcaegen2/platform/cli.git] / dcaedftool / src / app / df-schema.component.ts
1 // org.onap.dcae\r
2 // ============LICENSE_START====================================================\r
3 // Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.\r
4 // =============================================================================\r
5 // Licensed under the Apache License, Version 2.0 (the "License");\r
6 // you may not use this file except in compliance with the License.\r
7 // You may obtain a copy of the License at\r
8 //\r
9 //     http://www.apache.org/licenses/LICENSE-2.0\r
10 //\r
11 // Unless required by applicable law or agreed to in writing, software\r
12 // distributed under the License is distributed on an "AS IS" BASIS,\r
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
14 // See the License for the specific language governing permissions and\r
15 // limitations under the License.\r
16 // ============LICENSE_END======================================================\r
17 //\r
18 // ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
19 import { Component, Input, OnInit, OnChanges} from '@angular/core';\r
20 import { MdToolbarModule, MdButton, MdInputModule, MdSelectModule} from '@angular/material';\r
21 import {ValidateMetaSchemaService} from './validate-metaschema.service';\r
22 import {MetaSchemaService} from './metaschema.service';\r
23 \r
24 @Component({\r
25   selector: 'app-df-schema',\r
26   templateUrl: './df-schema.component.html',\r
27   styleUrls: [ './df-schema.component.css']\r
28 })\r
29 export class DFSchemaComponent implements OnInit /*, OnChanges */ {\r
30    public dfschema = '';\r
31    private jsonTestObject: any;\r
32    private validJSON: boolean;\r
33    public metaDisplay = false;\r
34    public schemaMsg: string;\r
35    public schemaErrMsgs: string[] = [];\r
36    private schemaMsgColor: string;\r
37    private schemaPassColor = '#00ff00';\r
38    private schemaFailColor = '#ff0000';\r
39    public metaButton = 'Display MetaSchema';\r
40    private hideMeta = 'Hide MetaSchema';\r
41    private displayMeta = 'Display Metaschema';\r
42    private schemaValidate: Object;\r
43    private validateMetaSchema: ValidateMetaSchemaService;\r
44    private metaSchema: MetaSchemaService;\r
45 \r
46 \r
47    // inline for now - TBD\r
48    public dfmetaschema: string;\r
49    private dfjson: string;\r
50 \r
51    constructor (validateMetaSchemaService: ValidateMetaSchemaService, metaSchemaService: MetaSchemaService) {\r
52       this.validateMetaSchema = validateMetaSchemaService;\r
53       this.metaSchema = metaSchemaService;\r
54       this.dfmetaschema = this.metaSchema.currentMetaSchemaFormatted();\r
55    }\r
56 \r
57    ngOnInit(): void {\r
58      this.schemaMsgColor=this.schemaPassColor;\r
59    }\r
60 \r
61    toggleMetaSchema(): void {\r
62      this.metaDisplay = !this.metaDisplay;\r
63      if (this.metaDisplay === true) {\r
64        this.metaButton = this.hideMeta;\r
65      } else {\r
66        this.metaButton = this.displayMeta;\r
67      }\r
68    }\r
69 \r
70    doDFSchemaChange(ev: any) {\r
71      this.schemaErrMsgs = [];\r
72      this.schemaMsg = '';\r
73      this.dfschema = ev.target.value;\r
74      if (this.dfschema.length == 0) {\r
75        return;\r
76      }\r
77      try {\r
78        // check for JSON validity\r
79        this.jsonTestObject = JSON.parse(this.dfschema);\r
80        this.validJSON = true;\r
81        this.schemaMsg = 'Valid JSON';\r
82      } catch (jsonErrMsg) {\r
83        this.schemaMsg = 'Invalid JSON: ' + jsonErrMsg;\r
84        return;\r
85      }\r
86      if (this.jsonTestObject.hasOwnProperty('jsonschema')) {\r
87        if (this.jsonTestObject.jsonschema.hasOwnProperty('$schema')){\r
88          try {\r
89            if (!(this.jsonTestObject.jsonschema.$schema === 'http://json-schema.org/draft-04/schema#'  || this.jsonTestObject.jsonschema.$schema === 'http://json-schema.org/draft-06/schema#')) {\r
90              this.schemaMsg = 'Invalid JSON Schema Data Format -  jsonschema$schema version must be 04 or 06';\r
91              return;\r
92            }\r
93          } catch(schemaVersionErr) {\r
94             this.schemaMsg = 'Invalid JSON Schema Data Format -  jsonschema$schema version must be 04 or 06';\r
95             return;\r
96          }\r
97        } else {\r
98             this.schemaMsg = 'Invalid JSON Schema Data Format -  jsonschema$schema must be specified';\r
99             return;\r
100        }\r
101      }\r
102      if (this.validateMetaSchema.validate(this.dfschema) === false) {\r
103        this.schemaMsg = 'Invalid Data Format Schema:';\r
104        this.schemaErrMsgs = this.validateMetaSchema.validateMsgs();\r
105      } else {\r
106        this.schemaMsg = 'Valid Data Format Schema';\r
107      }\r
108    }\r
109  }\r