777faee68c3ebf6a56ccf8f4c1c7582295479870
[ccsdk/cds.git] /
1 /*
2 ============LICENSE_START==========================================
3 ===================================================================
4 Copyright (C) 2018 IBM 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 ============LICENSE_END============================================
20 */
21
22 import { Component, OnInit, EventEmitter, Output, ViewChild } from '@angular/core';
23 import { Store } from '@ngrx/store';
24 import * as JSZip from 'jszip';
25 import { Observable } from 'rxjs';
26
27 import { IBlueprint } from '../../../../common/core/store/models/blueprint.model';
28 import { IBlueprintState } from '../../../../common/core/store/models/blueprintState.model';
29 import { IAppState } from '../../../../common/core/store/state/app.state';
30 import { LoadBlueprintSuccess, SET_BLUEPRINT_STATE, SetBlueprintState } from '../../../../common/core/store/actions/blueprint.action';
31 import { json } from 'd3';
32
33 @Component({
34   selector: 'app-search-template',
35   templateUrl: './search-template.component.html',
36   styleUrls: ['./search-template.component.scss']
37 })
38 export class SearchTemplateComponent implements OnInit {
39   file: File;
40   localBluePrintData: IBlueprint;
41   fileText: object[];
42   blueprintState: IBlueprintState;
43   bpState: Observable<IBlueprintState>;
44   validfile: boolean = false;
45
46   @ViewChild('fileInput') fileInput;
47   result: string = '';
48
49   private paths = [];
50   private tree;
51   private zipFile: JSZip = new JSZip();
52   private fileObject: any;
53   private activationBlueprint: any;
54   private tocsaMetadaData: any;
55   private blueprintName: string;
56
57   constructor(private store: Store<IAppState>) { }
58
59   ngOnInit() {
60   }
61
62   fileChanged(e: any) {
63     this.file = e.target.files[0];
64     this.zipFile.files = {};
65     this.zipFile.loadAsync(this.file)
66       .then((zip) => {
67         if(zip) {            
68           this.buildFileViewData(zip);
69         }
70       });
71   }
72
73   updateBlueprintState() {
74     let data: IBlueprint = this.activationBlueprint ? JSON.parse(this.activationBlueprint.toString()) : this.activationBlueprint;
75     let blueprintState = {
76       blueprint: data,
77       name: this.blueprintName,
78       files: this.tree,
79       filesData: this.paths
80     }
81     this.store.dispatch(new SetBlueprintState(blueprintState))
82     // this.store.dispatch(new LoadBlueprintSuccess(data));
83   }
84
85   async buildFileViewData(zip) {
86     this.validfile = false;
87     this.paths = [];
88     for (var file in zip.files) {
89       this.fileObject = {
90         name: zip.files[file].name,
91         data: ''
92       };
93       const value = <any>await  zip.files[file].async('string');
94       this.fileObject.data = value;
95       this.paths.push(this.fileObject); 
96     }
97
98     if(this.paths) {
99       this.paths.forEach(path =>{
100         if(path.name.includes("TOSCA.meta")) {
101           this.validfile = true
102         }
103       });
104     } else {
105       alert('Please update proper file');
106     }
107
108     if(this.validfile) {      
109       this.fetchTOSACAMetadata();
110       this.tree = this.arrangeTreeData(this.paths);
111     } else {
112       alert('Please update proper file with TOSCA metadata');
113     }
114   }
115
116   arrangeTreeData(paths) {
117     const tree = [];
118
119     paths.forEach((path) => {
120
121       const pathParts = path.name.split('/');
122       pathParts.shift();
123       let currentLevel = tree;
124
125       pathParts.forEach((part) => {
126         const existingPath = currentLevel.filter(level => level.name === part);
127
128         if (existingPath.length > 0) {
129           currentLevel = existingPath[0].children;
130         } else {
131           const newPart = {
132             name: part,
133             children: [],
134             data: path.data
135           };
136           if(part.trim() == this.blueprintName.trim()) { 
137             this.activationBlueprint = path.data; 
138             newPart.data = JSON.parse(this.activationBlueprint.toString());            
139             console.log('newpart', newPart);
140           }
141           if(newPart.name !== '') {            
142               currentLevel.push(newPart);
143               currentLevel = newPart.children;
144           }
145         }
146       });
147     });
148     console.log('tree', tree);
149     return tree;
150   }
151
152   fetchTOSACAMetadata() {
153     let toscaData = {};
154     this.paths.forEach(file =>{
155       if(file.name.includes('TOSCA.meta')) {
156         let keys = file.data.split("\n");
157         keys.forEach((key)=>{
158           let propertyData = key.split(':');
159           toscaData[propertyData[0]] = propertyData[1];
160         });
161       }
162     });
163     this.blueprintName = (((toscaData['Entry-Definitions']).split('/'))[1]).toString();;
164     console.log(toscaData);
165   }
166 }