| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2020 TATA ELXSI |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the 'License'); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | |
| 16 | Author: KUMARAN M (kumaran.m@tataelxsi.co.in), RAJESH S (rajesh.s@tataelxsi.co.in), BARATH KUMAR R (barath.r@tataelxsi.co.in) |
| 17 | */ |
| 18 | /** |
| 19 | * @file Info Compose Package Model |
| 20 | */ |
| 21 | import { HttpClient, HttpHeaders } from '@angular/common/http'; |
| 22 | import { Component, Injector, Input, OnInit } from '@angular/core'; |
| 23 | import { FormBuilder, FormGroup, Validators } from '@angular/forms'; |
| 24 | import { Router } from '@angular/router'; |
| 25 | import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap'; |
| 26 | import { TranslateService } from '@ngx-translate/core'; |
| 27 | import { NotifierService } from 'angular-notifier'; |
| 28 | import { APIURLHEADER, ERRORDATA, URLPARAMS } from 'CommonModel'; |
| 29 | import { DataService } from 'DataService'; |
| 30 | import { environment } from 'environment'; |
| 31 | import * as jsyaml from 'js-yaml'; |
| 32 | import * as pako from 'pako'; |
| 33 | import { RestService } from 'RestService'; |
| 34 | import { SharedService } from 'SharedService'; |
| 35 | |
| 36 | /** This is added globally by the tar.js library */ |
| SANDHYA.JS | 0a34dfa | 2023-04-25 23:59:41 +0530 | [diff] [blame^] | 37 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 38 | declare const Tar: any; |
| 39 | |
| 40 | /** |
| 41 | * Creating component |
| 42 | * @Component takes ComposePackages.html as template url |
| 43 | */ |
| 44 | @Component({ |
| 45 | templateUrl: './ComposePackages.html', |
| 46 | styleUrls: ['./ComposePackages.scss'] |
| 47 | }) |
| 48 | /** Exporting a class @exports ComposePackages */ |
| SANDHYA.JS | 0a34dfa | 2023-04-25 23:59:41 +0530 | [diff] [blame^] | 49 | // eslint-disable-next-line @angular-eslint/component-class-suffix |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 50 | export class ComposePackages implements OnInit { |
| 51 | /** Invoke service injectors @public */ |
| 52 | public injector: Injector; |
| 53 | |
| 54 | /** dataService to pass the data from one component to another @public */ |
| 55 | public dataService: DataService; |
| 56 | |
| 57 | /** Varaibles to hold http client @public */ |
| 58 | public httpClient: HttpClient; |
| 59 | |
| 60 | /** Instance for active modal service @public */ |
| 61 | public activeModal: NgbActiveModal; |
| 62 | |
| 63 | /** FormGroup instance added to the form @ html @public */ |
| 64 | public packagesForm: FormGroup; |
| 65 | |
| 66 | /** Form submission Add */ |
| 67 | public submitted: boolean = false; |
| 68 | |
| 69 | /** To handle loader status for API call @public */ |
| 70 | public isLoadingResults: boolean = false; |
| 71 | |
| 72 | /** Give the message for the loading @public */ |
| 73 | public message: string = 'PLEASEWAIT'; |
| 74 | |
| 75 | /** FormBuilder instance added to the formBuilder @private */ |
| 76 | private formBuilder: FormBuilder; |
| 77 | |
| 78 | /** Instance of the rest service @private */ |
| 79 | private restService: RestService; |
| 80 | |
| 81 | /** Notifier service to popup notification @private */ |
| 82 | private notifierService: NotifierService; |
| 83 | |
| 84 | /** Controls the header form @private */ |
| 85 | private headers: HttpHeaders; |
| 86 | |
| 87 | /** Create URL holds the end point of any packages @private */ |
| 88 | private createURL: string; |
| 89 | |
| 90 | /** Input contains component objects @private */ |
| 91 | @Input() private params: URLPARAMS; |
| 92 | |
| 93 | /** Holds the end point @private */ |
| 94 | private endPoint: string; |
| 95 | |
| 96 | /** Contains all methods related to shared @private */ |
| 97 | private sharedService: SharedService; |
| 98 | |
| 99 | /** Holds teh instance of AuthService class of type AuthService @private */ |
| 100 | private router: Router; |
| 101 | |
| 102 | /** Contains tranlsate instance @private */ |
| 103 | private translateService: TranslateService; |
| 104 | |
| 105 | constructor(injector: Injector) { |
| 106 | this.injector = injector; |
| 107 | this.dataService = this.injector.get(DataService); |
| 108 | this.restService = this.injector.get(RestService); |
| 109 | this.activeModal = this.injector.get(NgbActiveModal); |
| 110 | this.notifierService = this.injector.get(NotifierService); |
| 111 | this.formBuilder = this.injector.get(FormBuilder); |
| 112 | this.router = this.injector.get(Router); |
| 113 | this.translateService = this.injector.get(TranslateService); |
| 114 | this.sharedService = this.injector.get(SharedService); |
| 115 | } |
| 116 | |
| Barath Kumar R | 063a3f1 | 2020-12-29 16:35:09 +0530 | [diff] [blame] | 117 | /** convenience getter for easy access to form fields */ |
| 118 | get f(): FormGroup['controls'] { return this.packagesForm.controls; } |
| 119 | |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 120 | /** |
| 121 | * Lifecyle Hooks the trigger before component is instantiate |
| 122 | */ |
| 123 | public ngOnInit(): void { |
| 124 | this.headers = new HttpHeaders({ |
| 125 | 'Content-Type': 'application/gzip', |
| 126 | Accept: 'application/json', |
| 127 | 'Cache-Control': 'no-cache, no-store, must-revalidate, max-age=0' |
| 128 | }); |
| 129 | this.initializeForm(); |
| 130 | } |
| 131 | |
| 132 | /** initialize Forms @public */ |
| 133 | public initializeForm(): void { |
| 134 | this.packagesForm = this.formBuilder.group({ |
| 135 | name: ['', [Validators.required]] |
| 136 | }); |
| 137 | } |
| 138 | |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 139 | /** Create packages @public */ |
| 140 | public createPackages(): void { |
| 141 | this.submitted = true; |
| 142 | this.sharedService.cleanForm(this.packagesForm); |
| 143 | if (!this.packagesForm.invalid) { |
| 144 | this.isLoadingResults = true; |
| 145 | if (this.params.page === 'ns-package') { |
| 146 | this.endPoint = environment.NSDESCRIPTORSCONTENT_URL; |
| 147 | } else if (this.params.page === 'vnf-package') { |
| 148 | this.endPoint = environment.VNFPACKAGESCONTENT_URL; |
| 149 | } |
| 150 | const descriptor: string = this.packageYaml(this.params.page); |
| 151 | try { |
| SANDHYA.JS | 0a34dfa | 2023-04-25 23:59:41 +0530 | [diff] [blame^] | 152 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 153 | const tar: any = new Tar(); |
| 154 | const out: Uint8Array = tar.append(this.packagesForm.value.name + '/' + this.packagesForm.value.name + '.yaml', |
| 155 | descriptor, { type: '0' }); |
| 156 | const gzipContent: Uint8Array = pako.gzip(out); |
| 157 | this.createPackageApi(gzipContent.buffer); |
| 158 | } catch (e) { |
| 159 | this.isLoadingResults = false; |
| 160 | this.notifierService.notify('error', this.translateService.instant('ERROR')); |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | /** Create packages @public */ |
| 165 | private createPackageApi(packageContent: ArrayBuffer | SharedArrayBuffer): void { |
| 166 | const apiURLHeader: APIURLHEADER = { |
| 167 | url: this.endPoint, |
| 168 | httpOptions: { headers: this.headers } |
| 169 | }; |
| Barath Kumar R | 063a3f1 | 2020-12-29 16:35:09 +0530 | [diff] [blame] | 170 | this.restService.postResource(apiURLHeader, packageContent).subscribe((result: { id: string }): void => { |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 171 | this.isLoadingResults = false; |
| 172 | this.activeModal.close(); |
| 173 | this.composeNSPackages(result.id); |
| Barath Kumar R | 063a3f1 | 2020-12-29 16:35:09 +0530 | [diff] [blame] | 174 | }, (error: ERRORDATA): void => { |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 175 | this.isLoadingResults = false; |
| 176 | this.restService.handleError(error, 'post'); |
| 177 | }); |
| 178 | } |
| 179 | /** Compose NS Packages @private */ |
| 180 | private composeNSPackages(id: string): void { |
| 181 | let packageUrl: string; |
| 182 | if (this.params.page === 'ns-package') { |
| 183 | packageUrl = '/packages/ns/compose/'; |
| 184 | this.notifierService.notify('success', this.packagesForm.value.name + ' ' + |
| 185 | this.translateService.instant('PAGE.NSPACKAGE.CREATEDSUCCESSFULLY')); |
| 186 | } else if (this.params.page === 'vnf-package') { |
| 187 | packageUrl = '/packages/vnf/compose/'; |
| 188 | this.notifierService.notify('success', this.packagesForm.value.name + ' ' + |
| 189 | this.translateService.instant('PAGE.VNFPACKAGE.CREATEDSUCCESSFULLY')); |
| 190 | } |
| Barath Kumar R | 063a3f1 | 2020-12-29 16:35:09 +0530 | [diff] [blame] | 191 | this.router.navigate([packageUrl, id]).catch((): void => { |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 192 | // Catch Navigation Error |
| 193 | }); |
| 194 | } |
| 195 | /** Deafult template for NS and VNF Packages @private */ |
| 196 | private packageYaml(descriptorType: string): string { |
| 197 | let packageYaml: {} = {}; |
| Barath Kumar R | 063a3f1 | 2020-12-29 16:35:09 +0530 | [diff] [blame] | 198 | const composerName: string = 'NGUI Composer'; |
| 199 | const composerDefaultVersion: string = '1.0'; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 200 | if (descriptorType === 'ns-package') { |
| 201 | packageYaml = { |
| Barath Kumar R | 063a3f1 | 2020-12-29 16:35:09 +0530 | [diff] [blame] | 202 | nsd: { |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 203 | nsd: [ |
| 204 | { |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 205 | id: this.packagesForm.value.name, |
| Barath Kumar R | 063a3f1 | 2020-12-29 16:35:09 +0530 | [diff] [blame] | 206 | name: this.packagesForm.value.name, |
| 207 | version: composerDefaultVersion, |
| 208 | description: this.packagesForm.value.name + ' descriptor', |
| 209 | designer: composerName, |
| 210 | df: [ |
| 211 | { |
| 212 | id: 'default-df', |
| 213 | 'vnf-profile': [] |
| 214 | } |
| 215 | ] |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 216 | } |
| 217 | ] |
| 218 | } |
| 219 | }; |
| 220 | } else { |
| 221 | packageYaml = { |
| Barath Kumar R | 063a3f1 | 2020-12-29 16:35:09 +0530 | [diff] [blame] | 222 | vnfd: { |
| 223 | id: this.packagesForm.value.name, |
| 224 | 'product-name': this.packagesForm.value.name, |
| 225 | version: composerDefaultVersion, |
| 226 | description: this.packagesForm.value.name + ' descriptor', |
| 227 | provider: composerName, |
| 228 | df: [ |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 229 | { |
| Barath Kumar R | 063a3f1 | 2020-12-29 16:35:09 +0530 | [diff] [blame] | 230 | id: 'default-df', |
| 231 | 'instantiation-level': [], |
| 232 | 'vdu-profile': [] |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 233 | } |
| Barath Kumar R | 063a3f1 | 2020-12-29 16:35:09 +0530 | [diff] [blame] | 234 | ], |
| 235 | 'ext-cpd': [], |
| 236 | vdu: [], |
| 237 | 'sw-image-desc': [], |
| 238 | 'virtual-storage-desc': [] |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 239 | } |
| 240 | }; |
| 241 | } |
| Barath Kumar R | 86e497d | 2021-05-04 17:16:14 +0530 | [diff] [blame] | 242 | return jsyaml.dump(packageYaml, { sortKeys: true }); |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 243 | } |
| 244 | } |