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 { HttpHeaders } from '@angular/common/http';
22 import { Component, ElementRef, Injector, OnInit, ViewChild } 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, MODALCLOSERESPONSEDATA } from 'CommonModel';
29 import { DataService } from 'DataService';
30 import { environment } from 'environment';
31 import * as jsyaml from 'js-yaml';
32 import { NetworkSliceData } from 'NetworkSliceModel';
33 import { NSICREATEPARAMS } from 'NSDModel';
34 import { RestService } from 'RestService';
35 import { SharedService, isNullOrUndefined } from 'SharedService';
36 import { VimAccountDetails } from 'VimAccountModel';
39 * @Component takes InstantiateNetSliceTemplateComponent.html as template url
42 selector: 'app-instantiate-net-slice-template',
43 templateUrl: './InstantiateNetSliceTemplateComponent.html',
44 styleUrls: ['./InstantiateNetSliceTemplateComponent.scss']
46 /** Exporting a class @exports InstantiateNetSliceTemplateComponent */
47 export class InstantiateNetSliceTemplateComponent implements OnInit {
48 /** To inject services @public */
49 public injector: Injector;
51 /** FormGroup instance added to the form @ html @public */
52 public netSliceInstantiateForm: FormGroup;
54 /** Instance for active modal service @public */
55 public activeModal: NgbActiveModal;
57 /** Variable set for twoway bindng @public */
58 public vimAccountId: string;
60 /** Contains all the net slice data collections */
61 public netSliceSelect: NetworkSliceData;
63 /** Contains all the VIM data collections */
64 public vimDetailsSelect: VimAccountDetails;
66 /** Variable set for twoway binding @public */
67 public netsliceNstId: string;
69 /** Form submission Add */
70 public submitted: boolean = false;
72 /** Check the loading results for loader status @public */
73 public isLoadingResults: boolean = false;
75 /** Give the message for the loading @public */
76 public message: string = 'PLEASEWAIT';
78 /** Element ref for fileInputConfig @public */
79 @ViewChild('fileInputConfig', { static: true }) public fileInputConfig: ElementRef;
81 /** Element ref for fileInputConfigLabel @public */
82 @ViewChild('fileInputConfigLabel', { static: true }) public fileInputConfigLabel: ElementRef;
84 /** Element ref for fileInputSSH @public */
85 @ViewChild('fileInputSSH', { static: true }) public fileInputSSH: ElementRef;
87 /** Element ref for fileInputSSHLabel @public */
88 @ViewChild('fileInputSSHLabel', { static: true }) public fileInputSSHLabel: ElementRef;
90 /** Holds teh instance of AuthService class of type AuthService @private */
91 private router: Router;
93 /** FormBuilder instance added to the formBuilder @private */
94 private formBuilder: FormBuilder;
96 /** Utilizes rest service for any CRUD operations @private */
97 private restService: RestService;
99 /** Utilizes data service for any communication @private */
100 private dataService: DataService;
102 /** Controls the header form @private */
103 private headers: HttpHeaders;
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 /** Lifecyle Hooks the trigger before component is instantiate @public */
130 public ngOnInit(): void {
131 /** Setting up initial value for NSD */
132 this.netsliceNstId = '';
133 this.dataService.currentMessage.subscribe((event: NetworkSliceData) => {
134 if (event.identifier !== undefined || event.identifier !== '' || event.identifier !== null) {
135 this.netsliceNstId = event.identifier;
138 this.netSliceInstantiateFormAction();
139 this.headers = new HttpHeaders({
140 'Content-Type': 'application/json',
141 Accept: 'application/json',
142 'Cache-Control': 'no-cache, no-store, must-revalidate, max-age=0'
144 /** On Initializing call the methods */
145 this.getNetSliceDetails();
146 this.getVIMDetails();
149 /** Call the netSlice details in the selection options @public */
150 public getNetSliceDetails(): void {
151 this.restService.getResource(environment.NETWORKSLICETEMPLATECONTENT_URL).subscribe((netSlicePackages: NetworkSliceData) => {
152 this.netSliceSelect = netSlicePackages;
153 }, (error: ERRORDATA) => {
154 this.restService.handleError(error, 'get');
158 /** Call the VIM details in the selection options @public */
159 public getVIMDetails(): void {
160 this.restService.getResource(environment.VIMACCOUNTS_URL).subscribe((vimDetails: VimAccountDetails) => {
161 this.vimDetailsSelect = vimDetails;
162 }, (error: ERRORDATA) => {
163 this.restService.handleError(error, 'get');
167 /** On modal initializing forms @public */
168 public netSliceInstantiateFormAction(): void {
169 this.netSliceInstantiateForm = this.formBuilder.group({
170 nsiName: ['', [Validators.required]],
171 nsiDescription: ['', [Validators.required]],
172 nstId: ['', [Validators.required]],
173 vimAccountId: ['', [Validators.required]],
178 /** convenience getter for easy access to form fields */
179 get f(): FormGroup['controls'] { return this.netSliceInstantiateForm.controls; }
181 /** On modal submit instantiateNsSubmit will called @public */
182 public instantiateNSTSubmit(): void {
183 this.submitted = true;
184 this.sharedService.cleanForm(this.netSliceInstantiateForm);
185 if (this.netSliceInstantiateForm.invalid) {
188 const modalData: MODALCLOSERESPONSEDATA = {
191 if (isNullOrUndefined(this.netSliceInstantiateForm.value.ssh_keys) || this.netSliceInstantiateForm.value.ssh_keys === '') {
192 delete this.netSliceInstantiateForm.value.ssh_keys;
194 this.copySSHKey = JSON.parse(JSON.stringify(this.netSliceInstantiateForm.value.ssh_keys));
195 this.netSliceInstantiateForm.get('ssh_keys').setValue(this.copySSHKey);
197 if (isNullOrUndefined(this.netSliceInstantiateForm.value.config) || this.netSliceInstantiateForm.value.config === '') {
198 delete this.netSliceInstantiateForm.value.config;
200 const validJSON: boolean = this.sharedService.checkJson(this.netSliceInstantiateForm.value.config);
202 this.netSliceInstantiateForm.value.config = JSON.parse(this.netSliceInstantiateForm.value.config);
203 Object.keys(this.netSliceInstantiateForm.value.config).forEach((item: string) => {
204 // eslint-disable-next-line security/detect-object-injection
205 this.netSliceInstantiateForm.value[item] = this.netSliceInstantiateForm.value.config[item];
207 delete this.netSliceInstantiateForm.value.config;
209 const getConfigJson: string = jsyaml.load(this.netSliceInstantiateForm.value.config, { json: true });
210 Object.keys(getConfigJson).forEach((item: string) => {
211 // eslint-disable-next-line security/detect-object-injection
212 this.netSliceInstantiateForm.value[item] = getConfigJson[item];
214 delete this.netSliceInstantiateForm.value.config;
217 this.isLoadingResults = true;
218 const apiURLHeader: APIURLHEADER = {
219 url: environment.NETWORKSLICEINSTANCESCONTENT_URL,
220 httpOptions: { headers: this.headers }
222 this.restService.postResource(apiURLHeader, this.netSliceInstantiateForm.value)
223 .subscribe((result: {}) => {
224 this.activeModal.close(modalData);
225 this.isLoadingResults = false;
226 this.notifierService.notify('success', this.netSliceInstantiateForm.value.nsiName +
227 this.translateService.instant('PAGE.NETSLICE.CREATEDSUCCESSFULLY'));
228 this.router.navigate(['/instances/netslice']).catch((): void => {
229 // Catch Navigation Error
231 }, (error: ERRORDATA) => {
232 this.restService.handleError(error, 'post');
233 if (!isNullOrUndefined(this.copySSHKey)) {
234 this.netSliceInstantiateForm.get('ssh_keys').setValue(this.copySSHKey);
236 this.isLoadingResults = false;
240 /** ssh file process @private */
241 public sshFile(files: FileList): void {
242 if (files && files.length === 1) {
243 this.sharedService.getFileString(files, 'pub').then((fileContent: string): void => {
244 const getSSHJson: string = jsyaml.load(fileContent, { json: true });
245 this.netSliceInstantiateForm.get('ssh_keys').setValue(getSSHJson);
246 }).catch((err: string): void => {
247 if (err === 'typeError') {
248 this.notifierService.notify('error', this.translateService.instant('PUBFILETYPEERRROR'));
250 this.notifierService.notify('error', this.translateService.instant('ERROR'));
252 this.fileInputSSHLabel.nativeElement.innerText = this.translateService.instant('CHOOSEFILE');
253 this.fileInputSSH.nativeElement.value = null;
255 } else if (files && files.length > 1) {
256 this.notifierService.notify('error', this.translateService.instant('DROPFILESVALIDATION'));
258 this.fileInputSSHLabel.nativeElement.innerText = files[0].name;
259 this.fileInputSSH.nativeElement.value = null;
262 /** Config file process @private */
263 public configFile(files: FileList): void {
264 if (files && files.length === 1) {
265 const fileFormat: string = this.sharedService.fetchFileExtension(files).toLocaleLowerCase();
266 if (fileFormat === 'yaml' || fileFormat === 'yml') {
267 this.sharedService.getFileString(files, 'yaml').then((fileContent: string): void => {
268 const getConfigJson: string = jsyaml.load(fileContent, { json: true });
269 this.netSliceInstantiateForm.get('config').setValue(JSON.stringify(getConfigJson));
270 }).catch((err: string): void => {
271 if (err === 'typeError') {
272 this.notifierService.notify('error', this.translateService.instant('YAMLFILETYPEERRROR'));
274 this.notifierService.notify('error', this.translateService.instant('ERROR'));
276 this.fileInputConfigLabel.nativeElement.innerText = this.translateService.instant('CHOOSEFILE');
277 this.fileInputConfig.nativeElement.value = null;
279 } else if (fileFormat === 'json') {
280 this.sharedService.getFileString(files, 'json').then((fileContent: string): void => {
281 const getConfigJson: string = jsyaml.load(fileContent, { json: true });
282 this.netSliceInstantiateForm.get('config').setValue(JSON.stringify(getConfigJson));
283 }).catch((err: string): void => {
284 if (err === 'typeError') {
285 this.notifierService.notify('error', this.translateService.instant('JSONFILETYPEERRROR'));
287 this.notifierService.notify('error', this.translateService.instant('ERROR'));
289 this.fileInputConfigLabel.nativeElement.innerText = this.translateService.instant('CHOOSEFILE');
290 this.fileInputConfig.nativeElement.value = null;
293 } else if (files && files.length > 1) {
294 this.notifierService.notify('error', this.translateService.instant('DROPFILESVALIDATION'));
296 this.fileInputConfigLabel.nativeElement.innerText = files[0].name;
297 this.fileInputConfig.nativeElement.value = null;