Feature 11011: Multiple NS deletion in OSM
[osm/NG-UI.git] / src / app / utilities / delete / DeleteComponent.ts
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 Delete Model
20  */
21 import { HttpHeaders } from '@angular/common/http';
22 import { Component, Injector, Input } from '@angular/core';
23 import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
24 import { TranslateService } from '@ngx-translate/core';
25 import { NotifierService } from 'angular-notifier';
26 import { APIURLHEADER, DELETEPARAMS, ERRORDATA, MODALCLOSERESPONSEDATA, URLPARAMS } from 'CommonModel';
27 import { DataService } from 'DataService';
28 import { environment } from 'environment';
29 import { RestService } from 'RestService';
30 import { isNullOrUndefined } from 'SharedService';
31
32 /**
33  * Creating component
34  * @Component takes DeleteComponent.html as template url
35  */
36 @Component({
37   selector: 'app-delete',
38   templateUrl: './DeleteComponent.html',
39   styleUrls: ['./DeleteComponent.scss']
40 })
41 /** Exporting a class @exports DeleteComponent */
42 export class DeleteComponent {
43   /** To inject services @public */
44   public injector: Injector;
45
46   /** Instance for active modal service @public */
47   public activeModal: NgbActiveModal;
48
49   /** Instance of the modal service @public */
50   public title: string;
51
52   /** Show the Delete Ok button to trigger the terminate and delete */
53   public forceDelete: boolean = false;
54
55   /** Check the loading results @public */
56   public isLoadingResults: Boolean = false;
57
58   /** Check the page @public */
59   public isPage: Boolean = false;
60
61   /** Number of instances @public */
62   // eslint-disable-next-line @typescript-eslint/no-magic-numbers
63   public noOfInstances: Number = 25;
64
65   /** Give the message for the loading @public */
66   public notifyMessage: string = 'DELETELOADERMESSAGE';
67
68   /** Give the message for the loading @public */
69   public message: string = 'PLEASEWAIT';
70
71   /** DataService to pass the data from one component to another @private */
72   private dataService: DataService;
73
74   /** Instance of the rest service @private */
75   private restService: RestService;
76
77   /** Instance of the modal service @private */
78   private id: string;
79
80   /** Variables holds url to be delete @private */
81   private deleteURL: string;
82
83   /** Controls the header form @private */
84   private headers: HttpHeaders;
85
86   /** Input contains component objects @private */
87   @Input() private params: URLPARAMS;
88
89   /** Notifier service to popup notification @private */
90   private notifierService: NotifierService;
91
92   /** Contains tranlsate instance @private */
93   private translateService: TranslateService;
94
95   constructor(injector: Injector) {
96     this.injector = injector;
97     this.restService = this.injector.get(RestService);
98     this.dataService = this.injector.get(DataService);
99     this.activeModal = this.injector.get(NgbActiveModal);
100     this.notifierService = this.injector.get(NotifierService);
101     this.translateService = this.injector.get(TranslateService);
102   }
103
104   /**
105    * Lifecyle Hooks the trigger before component is instantiate
106    */
107   public ngOnInit(): void {
108     this.headers = new HttpHeaders({
109       'Content-Type': 'application/json',
110       Accept: 'application/json',
111       'Cache-Control': 'no-cache, no-store, must-revalidate, max-age=0'
112     });
113     this.dataService.currentMessage.subscribe((data: DELETEPARAMS) => {
114       if (data.identifier !== undefined || data.identifier !== '' || data.identifier !== null) {
115         this.id = data.identifier;
116       }
117       if (!isNullOrUndefined(this.params)) {
118         if (this.params.page === 'instantiateNS') {
119           this.isPage = true;
120           this.title = '';
121           this.createDeleteUrl(data);
122         } else if (this.params.page === 'ns-instance') {
123           this.createDeleteUrl(data);
124           this.isPage = false;
125         }
126       } else {
127         this.createTitleandID(data);
128         this.createDeleteUrl(data);
129         this.isPage = false;
130       }
131     });
132   }
133   /** Generate Title and Id from data @public */
134   public createTitleandID(data: DELETEPARAMS): void {
135     this.title = '';
136     if (data.name !== undefined) {
137       this.title = data.name;
138     } else if (data.projectName !== undefined) {
139       this.title = data.projectName;
140       this.id = this.title;
141     } else if (data.userName !== undefined) {
142       this.title = data.userName;
143     } else if (data.username !== undefined) {
144       this.title = data.username;
145     } else if (data.productName !== undefined) {
146       this.title = data.productName;
147     }
148   }
149   /** Generate Delete url from data @public */
150   public createDeleteUrl(data: DELETEPARAMS): void {
151     this.deleteURL = '';
152     if (!isNullOrUndefined(this.params)) {
153       if (this.params.page === 'ns-instance') {
154         this.deleteURL = environment.NSINSTANCESCONTENT_URL;
155         this.forceDelete = this.params.forceDeleteType;
156         this.title = this.params.name;
157         this.id = this.params.id;
158       } else if (this.params.page === 'instantiateNS') {
159         this.deleteURL = environment.NSINSTANCESTERMINATE_URL;
160         this.notifyMessage = 'DELETEDSUCCESSFULLY';
161       }
162     }
163     if (data.page === 'ns-package') {
164       this.deleteURL = environment.NSDESCRIPTORSCONTENT_URL;
165       this.notifyMessage = 'DELETEDSUCCESSFULLY';
166     } else if (data.page === 'vnf-package') {
167       this.deleteURL = environment.VNFPACKAGESCONTENT_URL;
168       this.notifyMessage = 'DELETEDSUCCESSFULLY';
169     } else if (data.page === 'vim-account') {
170       this.deleteURL = environment.VIMACCOUNTS_URL;
171       this.notifyMessage = 'DELETEDSUCCESSFULLY';
172     } else if (data.page === 'wim-account') {
173       this.deleteURL = environment.WIMACCOUNTS_URL;
174       this.notifyMessage = 'DELETEDSUCCESSFULLY';
175     } else if (data.page === 'projects') {
176       this.deleteURL = environment.PROJECTS_URL;
177       this.notifyMessage = 'DELETEDSUCCESSFULLY';
178       this.id = data.id;
179     } else if (data.page === 'users') {
180       this.deleteURL = environment.USERS_URL;
181       this.notifyMessage = 'DELETEDSUCCESSFULLY';
182     } else if (data.page === 'network-slice') {
183       this.deleteURL = environment.NETWORKSLICETEMPLATECONTENT_URL;
184       this.notifyMessage = 'DELETEDSUCCESSFULLY';
185     } else if (data.page === 'net-slice-instance') {
186       this.deleteURL = environment.NETWORKSLICEINSTANCESCONTENT_URL;
187       this.forceDelete = this.params.forceDeleteType;
188     } else if (data.page === 'roles') {
189       this.deleteURL = environment.ROLES_URL;
190       this.notifyMessage = 'DELETEDSUCCESSFULLY';
191     } else if (data.page === 'pdu-instances') {
192       this.deleteURL = environment.PDUINSTANCE_URL;
193     } else if (data.page === 'sdn-controller') {
194       this.deleteURL = environment.SDNCONTROLLER_URL;
195       this.notifyMessage = 'DELETEDSUCCESSFULLY';
196     } else if (data.page === 'k8-cluster') {
197       this.deleteURL = environment.K8SCLUSTER_URL;
198       this.notifyMessage = 'DELETEDSUCCESSFULLY';
199     } else if (data.page === 'k8-repo') {
200       this.deleteURL = environment.K8REPOS_URL;
201       this.notifyMessage = 'DELETEDSUCCESSFULLY';
202     } else if (data.page === 'osmrepo') {
203       this.deleteURL = environment.OSMREPOS_URL;
204       this.notifyMessage = 'DELETEDSUCCESSFULLY';
205     }
206   }
207   /** Generate Data function @public */
208   public deleteData(): void {
209     this.isLoadingResults = true;
210     const modalData: MODALCLOSERESPONSEDATA = {
211       message: 'Done'
212     };
213     let deletingURl: string = '';
214     if (this.forceDelete) {
215       deletingURl = this.deleteURL + '/' + this.id + '?FORCE=true';
216       this.notifyMessage = 'DELETEDSUCCESSFULLY';
217     } else {
218       deletingURl = this.deleteURL + '/' + this.id;
219     }
220     this.restService.deleteResource(deletingURl).subscribe((res: {}) => {
221       this.activeModal.close(modalData);
222       this.notifierService.notify('success', this.translateService.instant(this.notifyMessage, { title: this.title }));
223     }, (error: ERRORDATA) => {
224       this.isLoadingResults = false;
225       this.restService.handleError(error, 'delete');
226     }, () => {
227       this.isLoadingResults = false;
228     });
229   }
230
231   /** terminate multiple ns instances function @public */
232   public terminate(): void {
233     this.isLoadingResults = true;
234     const modalData: MODALCLOSERESPONSEDATA = {
235       message: 'Done'
236     };
237     const idData: string[] = [];
238     this.params.identifierList.forEach((data: DELETEPARAMS) => {
239       idData.push(data.identifier);
240     });
241     if (idData.length > this.noOfInstances) {
242       this.activeModal.close(modalData);
243       this.notifierService.notify('warning', this.translateService.instant('WARNINGMESSAGE'));
244     } else {
245       const postData: {} = {
246         ns_ids: idData
247       };
248       let deletingURl: string = '';
249       deletingURl = this.deleteURL;
250       this.notifyMessage = 'DELETELOADMESSAGE';
251       const apiURLHeader: APIURLHEADER = {
252         url: deletingURl,
253         httpOptions: { headers: this.headers }
254       };
255       this.restService.postResource(apiURLHeader, postData).subscribe(() => {
256         this.activeModal.close(modalData);
257         this.isLoadingResults = false;
258         this.notifierService.notify('success', this.translateService.instant(this.notifyMessage));
259       }, (error: ERRORDATA) => {
260         this.isLoadingResults = false;
261         this.restService.handleError(error, 'post');
262       }, () => {
263         this.isLoadingResults = false;
264       });
265     }
266   }
267 }