summaryrefslogtreecommitdiffstats
path: root/dcaedftool/src/app/df-schema.component.ts
blob: 8075c797d2040b80d9e1b1e0a80eb269760e622e (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
// org.onap.dcae
// ============LICENSE_START====================================================
// Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
// =============================================================================
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file 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======================================================
//
// ECOMP is a trademark and service mark of AT&T Intellectual Property.
import { Component, Input, OnInit, OnChanges} from '@angular/core';
import { MdToolbarModule, MdButton, MdInputModule, MdSelectModule} from '@angular/material';
import {ValidateMetaSchemaService} from './validate-metaschema.service';
import {MetaSchemaService} from './metaschema.service';

@Component({
  selector: 'app-df-schema',
  templateUrl: './df-schema.component.html',
  styleUrls: [ './df-schema.component.css']
})
export class DFSchemaComponent implements OnInit /*, OnChanges */ {
   public dfschema = '';
   private jsonTestObject: any;
   private validJSON: boolean;
   public metaDisplay = false;
   public schemaMsg: string;
   public schemaErrMsgs: string[] = [];
   private schemaMsgColor: string;
   private schemaPassColor = '#00ff00';
   private schemaFailColor = '#ff0000';
   public metaButton = 'Display MetaSchema';
   private hideMeta = 'Hide MetaSchema';
   private displayMeta = 'Display Metaschema';
   private schemaValidate: Object;
   private validateMetaSchema: ValidateMetaSchemaService;
   private metaSchema: MetaSchemaService;


   // inline for now - TBD
   public dfmetaschema: string;
   private dfjson: string;

   constructor (validateMetaSchemaService: ValidateMetaSchemaService, metaSchemaService: MetaSchemaService) {
      this.validateMetaSchema = validateMetaSchemaService;
      this.metaSchema = metaSchemaService;
      this.dfmetaschema = this.metaSchema.currentMetaSchemaFormatted();
   }

   ngOnInit(): void {
     this.schemaMsgColor=this.schemaPassColor;
   }

   toggleMetaSchema(): void {
     this.metaDisplay = !this.metaDisplay;
     if (this.metaDisplay === true) {
       this.metaButton = this.hideMeta;
     } else {
       this.metaButton = this.displayMeta;
     }
   }

   doDFSchemaChange(ev: any) {
     this.schemaErrMsgs = [];
     this.schemaMsg = '';
     this.dfschema = ev.target.value;
     if (this.dfschema.length == 0) {
       return;
     }
     try {
       // check for JSON validity
       this.jsonTestObject = JSON.parse(this.dfschema);
       this.validJSON = true;
       this.schemaMsg = 'Valid JSON';
     } catch (jsonErrMsg) {
       this.schemaMsg = 'Invalid JSON: ' + jsonErrMsg;
       return;
     }
     if (this.jsonTestObject.hasOwnProperty('jsonschema')) {
       if (this.jsonTestObject.jsonschema.hasOwnProperty('$schema')){
         try {
           if (!(this.jsonTestObject.jsonschema.$schema === 'http://json-schema.org/draft-04/schema#'  || this.jsonTestObject.jsonschema.$schema === 'http://json-schema.org/draft-06/schema#')) {
             this.schemaMsg = 'Invalid JSON Schema Data Format -  jsonschema$schema version must be 04 or 06';
             return;
           }
         } catch(schemaVersionErr) {
            this.schemaMsg = 'Invalid JSON Schema Data Format -  jsonschema$schema version must be 04 or 06';
            return;
         }
       } else {
            this.schemaMsg = 'Invalid JSON Schema Data Format -  jsonschema$schema must be specified';
            return;
       }
     }
     if (this.validateMetaSchema.validate(this.dfschema) === false) {
       this.schemaMsg = 'Invalid Data Format Schema:';
       this.schemaErrMsgs = this.validateMetaSchema.validateMsgs();
     } else {
       this.schemaMsg = 'Valid Data Format Schema';
     }
   }
 }