Merge "Remove Controller API url and use blueprintProcessorApi url instead Issue...
[ccsdk/cds.git] / cds-ui / client / src / app / feature-modules / controller-catalog / create-catalog / create-catalog.component.ts
1 /*
2 * ============LICENSE_START=======================================================
3 * ONAP : CDS
4 * ================================================================================
5 * Copyright (C) 2019 TechMahindra
6 *=================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 *     http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
19 */
20
21 import { Component, OnInit, ViewChild, ɵConsole } from '@angular/core';
22 import { FormBuilder, FormGroup, Validators } from '@angular/forms';
23 import { JsonEditorComponent, JsonEditorOptions } from 'ang-jsoneditor';
24 import { Observable } from 'rxjs';
25 import { ICatalogState } from 'src/app/common/core/store/models/catalogState.model';
26 import { ICatalog } from 'src/app/common/core/store/models/catalog.model';
27 import { Store } from '@ngrx/store';
28 import { IAppState } from 'src/app/common/core/store/state/app.state';
29 import { SetCatalogState } from 'src/app/common/core/store/actions/catalog.action';
30 import { CreateCatalogService } from './create-catalog.service';
31 import { NotificationHandlerService } from 'src/app/common/core/services/notification-handler.service';
32
33 @Component({
34   selector: 'app-create-catalog',
35   templateUrl: './create-catalog.component.html',
36   styleUrls: ['./create-catalog.component.scss']
37 })
38 export class CreateCatalogComponent implements OnInit {
39   
40   CatalogFormData: FormGroup;
41   @ViewChild(JsonEditorComponent) editor: JsonEditorComponent;
42   options = new JsonEditorOptions();
43   data:any;
44   derivedFrom: any[] = [{derivedFrom: 'tosca.nodes.Component'},{derivedFrom:'tosca.nodes.VNF'},{derivedFrom:'tosca.nodes.Artifact'},{derivedFrom:'tosca.nodes.ResourceSource'}, {derivedFrom:'tosca.nodes.Workflow'},{derivedFrom:'tosca.nodes.Root'}];
45   definitionType: any[] = [{definitionType: 'node_type'}];
46   ccState: Observable<ICatalogState>;
47   catalog: ICatalog;
48
49   constructor(private formBuilder: FormBuilder, private store: Store<IAppState>, private catalogCreateService: CreateCatalogService, private alertService: NotificationHandlerService) { 
50     this.ccState = this.store.select('catalog');
51     this.CatalogFormData = this.formBuilder.group({
52       Model_Name: ['', Validators.required],
53       User_id: ['', Validators.required],
54       _tags: ['', Validators.required],
55       _type: ['', Validators.required],
56       Derived_From: ['', Validators.required],
57       _description : ['', Validators.required]
58     });   
59   }
60   ngOnInit() {
61     this.options.mode = 'text';
62     this.options.modes = [ 'text', 'tree', 'view'];
63     this.options.statusBar = false; 
64
65     this.ccState.subscribe(
66       catalogdata => {
67         var catalogState: ICatalogState = { catalog: catalogdata.catalog, isLoadSuccess: catalogdata.isLoadSuccess, isSaveSuccess: catalogdata.isSaveSuccess, isUpdateSuccess: catalogdata.isUpdateSuccess };
68         this.catalog = catalogState.catalog;
69         console.log( this.catalog );
70       });
71
72 //    this.catalogCreateService.getDefinition()
73 //    .subscribe(data=>{
74 //        console.log(data);
75 //        data.forEach(element => {
76 //          this.definitionType.push(element)
77 //        });          
78 //    }, error=>{
79 //      window.alert('error' + error);
80 //    })
81 //
82 //    this.catalogCreateService.getDerivedFrom()
83 //    .subscribe(data=>{
84 //        console.log(data);
85 //        data.forEach(element => {
86 //          this.derivedFrom.push(element)
87 //        });          
88 //    }, error=>{
89 //      window.alert('error' + error);
90 //    })
91   }
92   CreateCatalog(){
93     this.catalog = Object.assign({}, this.CatalogFormData.value);
94     this.catalog.definition=this.data;
95     let catalogState = {
96       catalog: this.catalog
97     }
98     this.store.dispatch(new SetCatalogState(catalogState));
99     
100       this.catalogCreateService.saveCatalog(this.catalog)
101       .subscribe(response=>{
102         this.alertService.success("save success")
103       },
104       error=>{
105         this.alertService.error('Error saving resources');
106       })
107  
108   }
109
110   onChange($event) {
111     this.data=JSON.parse($event.srcElement.value); 
112     console.log(this.data);
113   };
114 }