2 ============LICENSE_START==========================================
3 ===================================================================
4 Copyright (C) 2018 IBM Intellectual Property. All rights reserved.
5 ===================================================================
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
12 http://www.apache.org/licenses/LICENSE-2.0
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============================================
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';
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';
34 selector: 'app-search-template',
35 templateUrl: './search-template.component.html',
36 styleUrls: ['./search-template.component.scss']
38 export class SearchTemplateComponent implements OnInit {
40 localBluePrintData: IBlueprint;
42 blueprintState: IBlueprintState;
43 bpState: Observable<IBlueprintState>;
44 validfile: boolean = false;
45 uploadedFileName: string;
46 @ViewChild('fileInput') fileInput;
51 private zipFile: JSZip = new JSZip();
52 private fileObject: any;
53 private activationBlueprint: any;
54 private tocsaMetadaData: any;
55 private blueprintName: string;
57 constructor(private store: Store<IAppState>) { }
64 this.file = e.target.files[0];
65 this.uploadedFileName = (this.file.name.split('.'))[0];
66 this.zipFile.files = {};
67 this.zipFile.loadAsync(this.file)
70 this.buildFileViewData(zip);
75 updateBlueprintState() {
76 let data: IBlueprint = this.activationBlueprint ? JSON.parse(this.activationBlueprint.toString()) : this.activationBlueprint;
77 let blueprintState = {
79 name: this.blueprintName,
83 this.store.dispatch(new SetBlueprintState(blueprintState))
84 // this.store.dispatch(new LoadBlueprintSuccess(data));
87 async buildFileViewData(zip) {
88 this.validfile = false;
90 for (var file in zip.files) {
92 // nameForUIDisplay: this.uploadedFileName + '/' + zip.files[file].name,
93 // name: zip.files[file].name,
94 name: this.uploadedFileName + '/' + zip.files[file].name,
97 const value = <any>await zip.files[file].async('string');
98 this.fileObject.data = value;
99 this.paths.push(this.fileObject);
103 this.paths.forEach(path =>{
104 if(path.name.includes("TOSCA.meta")) {
105 this.validfile = true
109 alert('Please update proper file');
113 this.fetchTOSACAMetadata();
114 this.tree = this.arrangeTreeData(this.paths);
116 alert('Please update proper file with TOSCA metadata');
120 arrangeTreeData(paths) {
123 paths.forEach((path) => {
125 const pathParts = path.name.split('/');
126 // pathParts.shift();
127 let currentLevel = tree;
129 pathParts.forEach((part) => {
130 const existingPath = currentLevel.filter(level => level.name === part);
132 if (existingPath.length > 0) {
133 currentLevel = existingPath[0].children;
141 if(part.trim() == this.blueprintName.trim()) {
142 this.activationBlueprint = path.data;
143 newPart.data = JSON.parse(this.activationBlueprint.toString());
144 console.log('newpart', newPart);
146 if(newPart.name !== '') {
147 currentLevel.push(newPart);
148 currentLevel = newPart.children;
153 console.log('tree', tree);
157 fetchTOSACAMetadata() {
159 this.paths.forEach(file =>{
160 if(file.name.includes('TOSCA.meta')) {
161 let keys = file.data.split("\n");
162 keys.forEach((key)=>{
163 let propertyData = key.split(':');
164 toscaData[propertyData[0]] = propertyData[1];
168 this.blueprintName = (((toscaData['Entry-Definitions']).split('/'))[1]).toString();;
169 console.log(toscaData);