NG-UI Added support for the OSM Repository
[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 { DELETEPARAMS, ERRORDATA, MODALCLOSERESPONSEDATA, URLPARAMS } from 'CommonModel';
27 import { DataService } from 'DataService';
28 import { environment } from 'environment';
29 import { RestService } from 'RestService';
30
31 /**
32  * Creating component
33  * @Component takes DeleteComponent.html as template url
34  */
35 @Component({
36   selector: 'app-delete',
37   templateUrl: './DeleteComponent.html',
38   styleUrls: ['./DeleteComponent.scss']
39 })
40 /** Exporting a class @exports DeleteComponent */
41 export class DeleteComponent {
42   /** To inject services @public */
43   public injector: Injector;
44
45   /** Instance for active modal service @public */
46   public activeModal: NgbActiveModal;
47
48   /** Instance of the modal service @public */
49   public title: string;
50
51   /** Show the Delete Ok button to trigger the terminate and delete */
52   public forceDelete: boolean = false;
53
54   /** Check the loading results @public */
55   public isLoadingResults: Boolean = false;
56
57   /** Give the message for the loading @public */
58   public notifyMessage: string = 'DELETELOADERMESSAGE';
59
60   /** Give the message for the loading @public */
61   public message: string = 'PLEASEWAIT';
62
63   /** DataService to pass the data from one component to another @private */
64   private dataService: DataService;
65
66   /** Instance of the rest service @private */
67   private restService: RestService;
68
69   /** Instance of the modal service @private */
70   private id: string;
71
72   /** Variables holds url to be delete @private */
73   private deleteURL: string;
74
75   /** Controls the header form @private */
76   private headers: HttpHeaders;
77
78   /** Input contains component objects @private */
79   @Input() private params: URLPARAMS;
80
81   /** Notifier service to popup notification @private */
82   private notifierService: NotifierService;
83
84   /** Contains tranlsate instance @private */
85   private translateService: TranslateService;
86
87   constructor(injector: Injector) {
88     this.injector = injector;
89     this.restService = this.injector.get(RestService);
90     this.dataService = this.injector.get(DataService);
91     this.activeModal = this.injector.get(NgbActiveModal);
92     this.notifierService = this.injector.get(NotifierService);
93     this.translateService = this.injector.get(TranslateService);
94   }
95
96   /**
97    * Lifecyle Hooks the trigger before component is instantiate
98    */
99   public ngOnInit(): void {
100     this.headers = new HttpHeaders({
101       'Content-Type': 'application/json',
102       Accept: 'application/json',
103       'Cache-Control': 'no-cache, no-store, must-revalidate, max-age=0'
104     });
105     this.dataService.currentMessage.subscribe((data: DELETEPARAMS) => {
106       if (data.identifier !== undefined || data.identifier !== '' || data.identifier !== null) {
107         this.id = data.identifier;
108       }
109       this.createTitleandID(data);
110       this.createDeleteUrl(data);
111     });
112   }
113   /** Generate Title and Id from data @public */
114   public createTitleandID(data: DELETEPARAMS): void {
115     this.title = '';
116     if (data.name !== undefined) {
117       this.title = data.name;
118     } else if (data.shortName !== undefined) {
119       this.title = data.shortName;
120     } else if (data.projectName !== undefined) {
121       this.title = data.projectName;
122       this.id = this.title;
123     } else if (data.userName !== undefined) {
124       this.title = data.userName;
125     } else if (data.username !== undefined) {
126       this.title = data.username;
127     }
128   }
129   /** Generate Delete url from data @public */
130   public createDeleteUrl(data: DELETEPARAMS): void {
131     this.deleteURL = '';
132     if (data.page === 'ns-instance') {
133       this.deleteURL = environment.NSINSTANCESCONTENT_URL;
134       this.forceDelete = this.params.forceDeleteType;
135     } else if (data.page === 'ns-package') {
136       this.deleteURL = environment.NSDESCRIPTORSCONTENT_URL;
137       this.notifyMessage = 'DELETEDSUCCESSFULLY';
138     } else if (data.page === 'vnf-package') {
139       this.deleteURL = environment.VNFPACKAGESCONTENT_URL;
140       this.notifyMessage = 'DELETEDSUCCESSFULLY';
141     } else if (data.page === 'vim-account') {
142       this.deleteURL = environment.VIMACCOUNTS_URL;
143       this.notifyMessage = 'DELETEDSUCCESSFULLY';
144     } else if (data.page === 'wim-account') {
145       this.deleteURL = environment.WIMACCOUNTS_URL;
146       this.notifyMessage = 'DELETEDSUCCESSFULLY';
147     } else if (data.page === 'projects') {
148       this.deleteURL = environment.PROJECTS_URL;
149       this.notifyMessage = 'DELETEDSUCCESSFULLY';
150       this.id = data.id;
151     } else if (data.page === 'users') {
152       this.deleteURL = environment.USERS_URL;
153       this.notifyMessage = 'DELETEDSUCCESSFULLY';
154     } else if (data.page === 'network-slice') {
155       this.deleteURL = environment.NETWORKSLICETEMPLATECONTENT_URL;
156       this.notifyMessage = 'DELETEDSUCCESSFULLY';
157     } else if (data.page === 'net-slice-instance') {
158       this.deleteURL = environment.NETWORKSLICEINSTANCESCONTENT_URL;
159       this.forceDelete = this.params.forceDeleteType;
160     } else if (data.page === 'roles') {
161       this.deleteURL = environment.ROLES_URL;
162       this.notifyMessage = 'DELETEDSUCCESSFULLY';
163     } else if (data.page === 'pdu-instances') {
164       this.deleteURL = environment.PDUINSTANCE_URL;
165     } else if (data.page === 'sdn-controller') {
166       this.deleteURL = environment.SDNCONTROLLER_URL;
167       this.notifyMessage = 'DELETEDSUCCESSFULLY';
168     } else if (data.page === 'k8-cluster') {
169       this.deleteURL = environment.K8SCLUSTER_URL;
170       this.notifyMessage = 'DELETEDSUCCESSFULLY';
171     } else if (data.page === 'k8-repo') {
172       this.deleteURL = environment.K8REPOS_URL;
173       this.notifyMessage = 'DELETEDSUCCESSFULLY';
174     } else if (data.page === 'osmrepo') {
175       this.deleteURL = environment.OSMREPOS_URL;
176       this.notifyMessage = 'DELETEDSUCCESSFULLY';
177     }
178   }
179   /** Generate Data function @public */
180   public deleteData(): void {
181     this.isLoadingResults = true;
182     const modalData: MODALCLOSERESPONSEDATA = {
183       message: 'Done'
184     };
185     let deletingURl: string = '';
186     if (this.forceDelete) {
187       deletingURl = this.deleteURL + '/' + this.id + '?FORCE=true';
188       this.notifyMessage = 'DELETEDSUCCESSFULLY';
189     } else {
190       deletingURl = this.deleteURL + '/' + this.id;
191     }
192     this.restService.deleteResource(deletingURl).subscribe((res: {}) => {
193       this.activeModal.close(modalData);
194       this.notifierService.notify('success', this.translateService.instant(this.notifyMessage, { title: this.title}));
195     }, (error: ERRORDATA) => {
196       this.isLoadingResults = false;
197       this.restService.handleError(error, 'delete');
198     }, () => {
199       this.isLoadingResults = false;
200     });
201   }
202 }