2 Copyright 2020 TATA ELXSI
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
8 http://www.apache.org/licenses/LICENSE-2.0
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.
16 Author: KUMARAN M (kumaran.m@tataelxsi.co.in), RAJESH S (rajesh.s@tataelxsi.co.in), BARATH KUMAR R (barath.r@tataelxsi.co.in)
19 * @file Instantiate NS Modal Component.
21 import { Component, ElementRef, Injector, OnInit, ViewChild } from '@angular/core';
22 import { FormBuilder, FormGroup, Validators } from '@angular/forms';
23 import { Router } from '@angular/router';
24 import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
25 import { TranslateService } from '@ngx-translate/core';
26 import { NotifierService } from 'angular-notifier';
27 import { APIURLHEADER, ERRORDATA, MODALCLOSERESPONSEDATA } from 'CommonModel';
28 import { DataService } from 'DataService';
29 import { environment } from 'environment';
30 import * as jsyaml from 'js-yaml';
31 import { NSCREATEPARAMS, NSData, NSDDetails } from 'NSDModel';
32 import { RestService } from 'RestService';
33 import { SharedService } from 'SharedService';
34 import { isNullOrUndefined } from 'util';
35 import { VimAccountDetails } from 'VimAccountModel';
39 * @Component takes InstantiateNsComponent.html as template url
42 selector: 'app-instantiate-ns',
43 templateUrl: './InstantiateNsComponent.html',
44 styleUrls: ['./InstantiateNsComponent.scss']
46 /** Exporting a class @exports InstantiateNsComponent */
47 export class InstantiateNsComponent implements OnInit {
48 /** To inject services @public */
49 public injector: Injector;
51 /** Contains all the nsd data collections */
52 public nsdSelect: NSDDetails;
54 /** FormGroup instance added to the form @ html @public */
55 public instantiateForm: FormGroup;
57 /** Contains all vim account collections */
58 public vimAccountSelect: VimAccountDetails;
60 /** Instance for active modal service @public */
61 public activeModal: NgbActiveModal;
63 /** Variable set for twoway binding @public */
66 /** Variable set for twoway bindng @public */
67 public vimAccountId: string;
69 /** Form submission Add */
70 public submitted: boolean = false;
72 /** Check the loading results @public */
73 public isLoadingResults: boolean = false;
75 /** Give the message for the loading @public */
76 public message: string = 'PLEASEWAIT';
78 /** Contains Selected VIM Details @public */
79 public selectedVIMDetails: VimAccountDetails = null;
81 /** Element ref for fileInputConfig @public */
82 @ViewChild('fileInputConfig', { static: true }) public fileInputConfig: ElementRef;
84 /** Element ref for fileInputConfigLabel @public */
85 @ViewChild('fileInputConfigLabel', { static: true }) public fileInputConfigLabel: ElementRef;
87 /** Element ref for fileInputSSH @public */
88 @ViewChild('fileInputSSH', { static: true }) public fileInputSSH: ElementRef;
90 /** Element ref for fileInputSSHLabel @public */
91 @ViewChild('fileInputSSHLabel', { static: true }) public fileInputSSHLabel: ElementRef;
93 /** Holds teh instance of AuthService class of type AuthService @private */
94 private router: Router;
96 /** FormBuilder instance added to the formBuilder @private */
97 private formBuilder: FormBuilder;
99 /** Utilizes rest service for any CRUD operations @private */
100 private restService: RestService;
102 /** Utilizes data service for any communication @private */
103 private dataService: DataService;
105 /** Notifier service to popup notification @private */
106 private notifierService: NotifierService;
108 /** Contains tranlsate instance @private */
109 private translateService: TranslateService;
111 /** Contains all methods related to shared @private */
112 private sharedService: SharedService;
114 /** Contains the ssh key to be hosted in dom @private */
115 private copySSHKey: string;
117 constructor(injector: Injector) {
118 this.injector = injector;
119 this.restService = this.injector.get(RestService);
120 this.activeModal = this.injector.get(NgbActiveModal);
121 this.formBuilder = this.injector.get(FormBuilder);
122 this.dataService = this.injector.get(DataService);
123 this.notifierService = this.injector.get(NotifierService);
124 this.router = this.injector.get(Router);
125 this.translateService = this.injector.get(TranslateService);
126 this.sharedService = this.injector.get(SharedService);
129 public ngOnInit(): void {
130 /** Setting up initial value for NSD */
131 this.dataService.currentMessage.subscribe((event: NSData) => {
132 if (event.identifier !== undefined || event.identifier !== '' || event.identifier !== null) {
133 this.nsdId = event.identifier;
136 /** On Initializing call the methods */
137 this.instantiateFormAction();
138 this.getDetailsnsd();
139 this.getDetailsvimAccount();
142 /** On modal initializing forms @public */
143 public instantiateFormAction(): void {
144 this.instantiateForm = this.formBuilder.group({
145 nsName: ['', [Validators.required]],
146 nsDescription: ['', [Validators.required]],
147 nsdId: ['', [Validators.required]],
148 vimAccountId: ['', [Validators.required]],
154 /** Convenience getter for easy access to form fields */
155 get f(): FormGroup['controls'] { return this.instantiateForm.controls; }
157 /** Call the nsd details in the selection options @public */
158 public getDetailsnsd(): void {
159 this.restService.getResource(environment.NSDESCRIPTORSCONTENT_URL).subscribe((nsPackages: NSDDetails) => {
160 this.nsdSelect = nsPackages;
161 }, (error: ERRORDATA) => {
162 this.restService.handleError(error, 'get');
166 /** Call the vimAccount details in the selection options @public */
167 public getDetailsvimAccount(): void {
168 this.restService.getResource(environment.VIMACCOUNTS_URL).subscribe((vimAccounts: VimAccountDetails) => {
169 this.vimAccountSelect = vimAccounts;
170 }, (error: ERRORDATA) => {
171 this.restService.handleError(error, 'get');
175 /** On modal submit instantiateNsSubmit will called @public */
176 public instantiateNsSubmit(): void {
177 this.submitted = true;
178 this.sharedService.cleanForm(this.instantiateForm);
179 if (this.instantiateForm.invalid) {
182 const modalData: MODALCLOSERESPONSEDATA = {
185 if (isNullOrUndefined(this.instantiateForm.value.ssh_keys) || this.instantiateForm.value.ssh_keys === '') {
186 delete this.instantiateForm.value.ssh_keys;
188 this.copySSHKey = JSON.parse(JSON.stringify(this.instantiateForm.value.ssh_keys));
189 // tslint:disable-next-line: no-backbone-get-set-outside-model
190 this.instantiateForm.get('ssh_keys').setValue([this.copySSHKey]);
192 if (isNullOrUndefined(this.instantiateForm.value.config) || this.instantiateForm.value.config === '') {
193 delete this.instantiateForm.value.config;
195 const validJSON: boolean = this.sharedService.checkJson(this.instantiateForm.value.config);
197 this.instantiateForm.value.config = JSON.parse(this.instantiateForm.value.config);
198 Object.keys(this.instantiateForm.value.config).forEach((item: string) => {
199 this.instantiateForm.value[item] = this.instantiateForm.value.config[item];
201 delete this.instantiateForm.value.config;
203 const getConfigJson: string = jsyaml.load(this.instantiateForm.value.config, { json: true });
204 Object.keys(getConfigJson).forEach((item: string) => {
205 this.instantiateForm.value[item] = getConfigJson[item];
207 delete this.instantiateForm.value.config;
210 const apiURLHeader: APIURLHEADER = {
211 url: environment.NSINSTANCESCONTENT_URL
213 this.isLoadingResults = true;
214 this.restService.postResource(apiURLHeader, this.instantiateForm.value).subscribe((result: {}) => {
215 this.activeModal.close(modalData);
216 this.notifierService.notify('success', this.instantiateForm.value.nsName +
217 this.translateService.instant('PAGE.NSINSTANCE.CREATEDSUCCESSFULLY'));
218 this.router.navigate(['/instances/ns']).catch();
219 }, (error: ERRORDATA) => {
220 this.isLoadingResults = false;
221 this.restService.handleError(error, 'post');
222 if (!isNullOrUndefined(this.copySSHKey)) {
223 // tslint:disable-next-line: no-backbone-get-set-outside-model
224 this.instantiateForm.get('ssh_keys').setValue(this.copySSHKey);
229 /** ssh file process @private */
230 public sshFile(files: FileList): void {
231 if (files && files.length === 1) {
232 this.sharedService.getFileString(files, 'pub').then((fileContent: string): void => {
233 const getSSHJson: string = jsyaml.load(fileContent, { json: true });
234 // tslint:disable-next-line: no-backbone-get-set-outside-model
235 this.instantiateForm.get('ssh_keys').setValue(getSSHJson);
236 }).catch((err: string): void => {
237 if (err === 'typeError') {
238 this.notifierService.notify('error', this.translateService.instant('PUBFILETYPEERRROR'));
240 this.notifierService.notify('error', this.translateService.instant('ERROR'));
242 this.fileInputSSHLabel.nativeElement.innerText = this.translateService.instant('CHOOSEFILE');
243 this.fileInputSSH.nativeElement.value = null;
245 } else if (files && files.length > 1) {
246 this.notifierService.notify('error', this.translateService.instant('DROPFILESVALIDATION'));
248 this.fileInputSSHLabel.nativeElement.innerText = files[0].name;
249 this.fileInputSSH.nativeElement.value = null;
252 /** Config file process @private */
253 public configFile(files: FileList): void {
254 if (files && files.length === 1) {
255 const fileFormat: string = this.sharedService.fetchFileExtension(files).toLocaleLowerCase();
256 if (fileFormat === 'yaml' || fileFormat === 'yml') {
257 this.sharedService.getFileString(files, 'yaml').then((fileContent: string): void => {
258 // tslint:disable-next-line: no-backbone-get-set-outside-model
259 this.instantiateForm.get('config').setValue(fileContent);
260 }).catch((err: string): void => {
261 if (err === 'typeError') {
262 this.notifierService.notify('error', this.translateService.instant('YAMLFILETYPEERRROR'));
264 this.notifierService.notify('error', this.translateService.instant('ERROR'));
266 this.fileInputConfigLabel.nativeElement.innerText = this.translateService.instant('CHOOSEFILE');
267 this.fileInputConfig.nativeElement.value = null;
269 } else if (fileFormat === 'json') {
270 this.sharedService.getFileString(files, 'json').then((fileContent: string): void => {
271 const getConfigJson: string = jsyaml.load(fileContent, { json: true });
272 // tslint:disable-next-line: no-backbone-get-set-outside-model
273 this.instantiateForm.get('config').setValue(JSON.stringify(getConfigJson));
274 }).catch((err: string): void => {
275 if (err === 'typeError') {
276 this.notifierService.notify('error', this.translateService.instant('JSONFILETYPEERRROR'));
278 this.notifierService.notify('error', this.translateService.instant('ERROR'));
280 this.fileInputConfigLabel.nativeElement.innerText = this.translateService.instant('CHOOSEFILE');
281 this.fileInputConfig.nativeElement.value = null;
284 } else if (files && files.length > 1) {
285 this.notifierService.notify('error', this.translateService.instant('DROPFILESVALIDATION'));
287 this.fileInputConfigLabel.nativeElement.innerText = files[0].name;
288 this.fileInputConfig.nativeElement.value = null;
291 /** Get Selected VIM details @public */
292 public getSelectedVIMDetails(vimDetails: VimAccountDetails): void {
293 if (!isNullOrUndefined(vimDetails.resources)) {
294 this.selectedVIMDetails = vimDetails;