18b6058a87d6a28ec0eb1a30328180a05a2b1681
[osm/NG-UI.git] / src / app / utilities / start-stop-rebuild / StartStopRebuildComponent.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: SANDHYA JS (sandhya.j@tataelxsi.co.in)
17 */
18 /**
19  * @file StartStopRebuild Component
20  */
21 import { HttpHeaders } from '@angular/common/http';
22 import { Component, Injector, Input, OnInit } from '@angular/core';
23 import { AbstractControl, FormBuilder, FormGroup, Validators } from '@angular/forms';
24 import { Router } from '@angular/router';
25 import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
26 import { APIURLHEADER, ERRORDATA, MODALCLOSERESPONSEDATA, URLPARAMS } from 'CommonModel';
27 import { environment } from 'environment';
28 import { StartStopRebuild } from 'NSInstanceModel';
29 import { RestService } from 'RestService';
30 import { SharedService } from 'SharedService';
31 import { isNullOrUndefined } from 'util';
32 import { DF, VNFD } from 'VNFDModel';
33 import { VDUR, VNFInstanceDetails } from 'VNFInstanceModel';
34
35 /**
36  * Creating component
37  * @Component takes StartStopRebuildComponent.html as template url
38  */
39 @Component({
40     selector: 'app-start-stop-rebuild',
41     templateUrl: './StartStopRebuildComponent.html',
42     styleUrls: ['./StartStopRebuildComponent.scss']
43 })
44 export class StartStopRebuildComponent implements OnInit {
45     /** To inject services @public */
46     public injector: Injector;
47     /** Instance for active modal service @public */
48     public activeModal: NgbActiveModal;
49     /** Check the loading results @public */
50     public isLoadingResults: Boolean = false;
51     /** Give the message for the loading @public */
52     public message: string = 'PLEASEWAIT';
53     /** FormGroup instance added to the form @ html @public */
54     public startForm: FormGroup;
55     /** Items for the memberVNFIndex @public */
56     public memberTypes: {}[];
57     /** Contains MemberVNFIndex values @public */
58     public memberVnfIndex: {}[] = [];
59     /** Contains vnfInstanceId of the selected MemberVnfIndex  @public */
60     public instanceId: string;
61     /** Items for vduId & countIndex @public */
62     public vdu: {}[];
63     /** Selected VNFInstanceId @public */
64     public selectedvnfId: string = '';
65     /** Check day1-2 operation  @public */
66     public 'day1-2': boolean;
67     /** Array holds VNFR Data filtered with nsr ID @public */
68     public nsIdFilteredData: {}[] = [];
69     /** Form valid on submit trigger @public */
70     public submitted: boolean = false;
71     /** Input contains Modal dialog component Instance @public */
72     @Input() public instanceType: string;
73     /** Input contains Modal dialog component Instance @public */
74     @Input() public instanceTitle: string;
75     /** Input contains component objects @private */
76     @Input() private params: URLPARAMS;
77     /** FormBuilder instance added to the formBuilder @private */
78     private formBuilder: FormBuilder;
79     /** Instance of the rest service @private */
80     private restService: RestService;
81     /** Controls the header form @private */
82     private headers: HttpHeaders;
83     /** Contains all methods related to shared @private */
84     private sharedService: SharedService;
85     /** Holds the instance of AuthService class of type AuthService @private */
86     private router: Router;
87     constructor(injector: Injector) {
88         this.injector = injector;
89         this.restService = this.injector.get(RestService);
90         this.activeModal = this.injector.get(NgbActiveModal);
91         this.formBuilder = this.injector.get(FormBuilder);
92         this.sharedService = this.injector.get(SharedService);
93         this.router = this.injector.get(Router);
94     }
95     /** convenience getter for easy access to form fields */
96     get f(): FormGroup['controls'] { return this.startForm.controls; }
97     /**
98      * Lifecyle Hooks the trigger before component is instantiate
99      */
100     public ngOnInit(): void {
101         this.initializeForm();
102         this.getMemberVnfIndex();
103         this.headers = new HttpHeaders({
104             'Content-Type': 'application/json',
105             Accept: 'application/json',
106             'Cache-Control': 'no-cache, no-store, must-revalidate, max-age=0'
107         });
108     }
109     /** Initialize start, stop or rebuild Forms @public */
110     public initializeForm(): void {
111         this.startForm = this.formBuilder.group({
112             memberVnfIndex: [null, [Validators.required]],
113             vduId: [null, [Validators.required]],
114             countIndex: [null, [Validators.required]]
115         });
116     }
117
118     /** Getting MemberVnfIndex using VNFInstances API @public */
119     public getMemberVnfIndex(): void {
120         const vnfInstanceData: {}[] = [];
121         this.restService.getResource(environment.VNFINSTANCES_URL).subscribe((vnfInstancesData: VNFInstanceDetails[]): void => {
122             vnfInstancesData.forEach((vnfData: VNFInstanceDetails): void => {
123                 const vnfDataObj: {} =
124                 {
125                     VNFD: vnfData['vnfd-ref'],
126                     VNFInstanceId: vnfData._id,
127                     MemberIndex: vnfData['member-vnf-index-ref'],
128                     NS: vnfData['nsr-id-ref'],
129                     VNFID: vnfData['vnfd-id']
130                 };
131                 vnfInstanceData.push(vnfDataObj);
132             });
133             const nsId: string = 'NS';
134             this.nsIdFilteredData = vnfInstanceData.filter((vnfdData: {}[]): boolean => vnfdData[nsId] === this.params.id);
135             this.nsIdFilteredData.forEach((resVNF: {}[]): void => {
136                 const memberIndex: string = 'MemberIndex';
137                 const vnfinstanceID: string = 'VNFInstanceId';
138                 const assignMemberIndex: {} = {
139                     id: resVNF[memberIndex],
140                     vnfinstanceId: resVNF[vnfinstanceID]
141                 };
142                 this.memberVnfIndex.push(assignMemberIndex);
143             });
144             this.memberTypes = this.memberVnfIndex;
145             this.isLoadingResults = false;
146         }, (error: ERRORDATA): void => {
147             this.restService.handleError(error, 'get');
148             this.isLoadingResults = false;
149         });
150     }
151
152     /** Getting vdu-id & count-index from VNFInstance API */
153     public getVdu(id: string): void {
154         const vnfInstanceData: {}[] = [];
155         const vnfrDetails: {}[] = [];
156         this.getFormControl('vduId').setValue(null);
157         this.getFormControl('countIndex').setValue(null);
158         if (!isNullOrUndefined(id)) {
159             this.restService.getResource(environment.VNFINSTANCES_URL + '/' + id).
160                 subscribe((vnfInstanceDetail: VNFInstanceDetails[]): void => {
161                     this.instanceId = id;
162                     this.selectedvnfId = vnfInstanceDetail['vnfd-ref'];
163                     const VDU: string = 'vdur';
164                     if (vnfInstanceDetail[VDU] !== undefined) {
165                         vnfInstanceDetail[VDU].forEach((vdu: VDUR): void => {
166                             const vnfInstanceDataObj: {} =
167                             {
168                                 'count-index': vdu['count-index'],
169                                 VDU: vdu['vdu-id-ref']
170
171                             };
172                             vnfInstanceData.push(vnfInstanceDataObj);
173                         });
174                         this.vdu = vnfInstanceData;
175                     }
176                     this.checkDay12Operation(this.selectedvnfId);
177                 }, (error: ERRORDATA): void => {
178                     this.restService.handleError(error, 'get');
179                     this.isLoadingResults = false;
180                 });
181         }
182     }
183
184     /** To check primitve actions from VNFR  */
185     public checkDay12Operation(id: string): void {
186         const apiUrl: string = environment.VNFPACKAGES_URL + '?id=' + id;
187         this.restService.getResource(apiUrl).subscribe((vnfdInfo: VNFD[]): void => {
188             const vnfInstances: VNFD = vnfdInfo[0];
189             if (!isNullOrUndefined(vnfInstances.df)) {
190                 vnfInstances.df.forEach((df: DF): void => {
191                     if (df['lcm-operations-configuration'] !== undefined) {
192                         if (df['lcm-operations-configuration']['operate-vnf-op-config']['day1-2'] !== undefined) {
193                             this['day1-2'] = true;
194                         }
195                     } else {
196                         this['day1-2'] = false;
197                     }
198                 });
199             }
200         }, (error: ERRORDATA): void => {
201             this.isLoadingResults = false;
202             this.restService.handleError(error, 'get');
203         });
204     }
205     /** Check Instance type is start or stop or rebuild and proceed action */
206     public instanceCheck(instanceType: string): void {
207         this.submitted = true;
208         this.sharedService.cleanForm(this.startForm);
209         if (this.startForm.invalid) { return; } // Proceed, onces form is valid
210         if (instanceType === 'start') {
211             const startPayload: StartStopRebuild = {
212                 updateType: 'OPERATE_VNF',
213                 operateVnfData: {
214                     vnfInstanceId: this.instanceId,
215                     changeStateTo: 'start',
216                     additionalParam: {
217                         'run-day1': false,
218                         vdu_id: this.startForm.value.vduId,
219                         'count-index': this.startForm.value.countIndex
220                     }
221                 }
222             };
223             this.startInitialization(startPayload);
224         } else if (instanceType === 'stop') {
225             const stopPayload: StartStopRebuild = {
226                 updateType: 'OPERATE_VNF',
227                 operateVnfData: {
228                     vnfInstanceId: this.instanceId,
229                     changeStateTo: 'stop',
230                     additionalParam: {
231                         'run-day1': false,
232                         vdu_id: this.startForm.value.vduId,
233                         'count-index': this.startForm.value.countIndex
234                     }
235                 }
236             };
237             this.startInitialization(stopPayload);
238         } else {
239             const rebuildPayload: StartStopRebuild = {
240                 updateType: 'OPERATE_VNF',
241                 operateVnfData: {
242                     vnfInstanceId: this.instanceId,
243                     changeStateTo: 'rebuild',
244                     additionalParam: {
245                         'run-day1': (this['day1-2'] === true) ? true : false,
246                         vdu_id: this.startForm.value.vduId,
247                         'count-index': this.startForm.value.countIndex
248                     }
249                 }
250             };
251             this.startInitialization(rebuildPayload);
252         }
253     }
254
255     /** Initialize the start, stop or rebuild operation @public */
256     public startInitialization(startPayload: object): void {
257         this.isLoadingResults = true;
258         const apiURLHeader: APIURLHEADER = {
259             url: environment.NSDINSTANCES_URL + '/' + this.params.id + '/update',
260             httpOptions: { headers: this.headers }
261         };
262         const modalData: MODALCLOSERESPONSEDATA = {
263             message: 'Done'
264         };
265         this.restService.postResource(apiURLHeader, startPayload).subscribe((result: {}): void => {
266             this.activeModal.close(modalData);
267             this.router.navigate(['/instances/ns/history-operations/' + this.params.id]).catch();
268         }, (error: ERRORDATA): void => {
269             this.restService.handleError(error, 'post');
270             this.isLoadingResults = false;
271         });
272     }
273
274     /** Used to get the AbstractControl of controlName passed @private */
275     private getFormControl(controlName: string): AbstractControl {
276         return this.startForm.controls[controlName];
277     }
278 }