aboutsummaryrefslogtreecommitdiffstats
path: root/usecaseui-portal/src/app/views/services/onboard-vnf-vm/onboard-vnf-vm.component.ts
blob: 3d312c5cea4f845a899a1298ab98fe5b1b91b7d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
    Copyright (C) 2019 CMCC, Inc. and others. All rights reserved.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

            http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/
import { HttpClient, HttpRequest, HttpResponse } from '@angular/common/http';
import { Component, OnInit, HostBinding, TemplateRef, ViewChild } from '@angular/core';
import { onboardService } from '../../../core/services/onboard.service';
import { slideToRight } from '../../../shared/utils/animates';
import { NzMessageService, UploadFile, NzModalRef, NzModalService } from 'ng-zorro-antd';
import { filter } from 'rxjs/operators';

@Component({
  selector: 'app-onboard-vnf-vm',
  templateUrl: './onboard-vnf-vm.component.html',
  styleUrls: ['./onboard-vnf-vm.component.less'],
  animations: [slideToRight]
})
export class OnboardVnfVmComponent implements OnInit {
  @HostBinding('@routerAnimate') routerAnimateState;
  @ViewChild('notification') notification: any;

  // upload
  tabs: string[] = ['NS', 'VNF', 'PNF'];
  currentTab: string = 'NS'
  fileList: UploadFile[] = [];
  uploading:boolean = false;
  infoId: string;
  display: string = 'block';

  // table
  isSpinning: boolean = false;
  nsTableData: any[];
  vnfTableData: any[];
  pnfTableData: any[];
  status: string = "Onboard Available";
  pageIndex: number = 1;
  pageSize: number = 10;

  // update or delete
  isUpdate: boolean = false;
  jobId: string;

  //url
  url = {
    ns: '/api/nsd/v1/ns_descriptors/*_*/nsd_content',
    vnf: '/api/vnfpkgm/v1/vnf_packages/*_*/package_content',
    pnf: '/api/nsd/v1/pnf_descriptors/*_*/pnfd_content'
  };

  file: {
    name: string,
    uid: string,
    progress: number,
    status: boolean,
    success: number
  };

  constructor(
    private myhttp: onboardService,
    private http: HttpClient,
    private msg: NzMessageService,
    private modalService: NzModalService
  ) { }

  //default Call ns data by default
  ngOnInit() {
    this.getTableData();
  }

  // Handling tab switching request data
  handleTabChange(tab: string): void {
    this.currentTab = tab;
    this.fileList = [];
    this.display = 'block';
    delete this.file;
    switch (tab) {
      case 'NS':
        this.getTableData();
        break
      case 'VNF':
        this.getTableVnfData()
        break
      case 'PNF':
        this.getTablePnfData()
        break
    }
  }

  //before put create--Drag and drop files to the page before uploading
  requestBody = {
    "userDefinedData": {
      "additionalProp1": "",
      "additionalProp2": "",
      "additionalProp3": ""
    }
  }

  beforeUpload = (file: UploadFile): boolean => {
    this.fileList.splice(0,1,file);
    let API: string;
    if(this.currentTab === 'NS'){
      API = 'createNetworkServiceData';
    }else if(this.currentTab === 'VNF'){
      API = 'createVnfData';
    }else {
      API = 'createPnfData';
    }
    this.myhttp.getCreatensData(API, this.requestBody)//on-line
      .subscribe((data) => {
        this.infoId = data["id"];
      }, (err) => {
        console.log(err);
      })
    return false;
  }

  // Drag and drop and click the upload button
  onClick(): void {
    this.display = 'none';
    let tab = this.currentTab === 'NS' ? 'ns' : (this.currentTab === 'VNF' ? 'vnf' : 'pnf')
    this.handleUpload(this.url[tab].replace("*_*", this.infoId));
  }

  handleUpload(url: string): void {
    const formData = new FormData();
    // tslint:disable-next-line:no-any
    this.fileList.forEach((file: any) => {
      formData.set('file', file);
    });
    this.uploading = true;
    this.file = {
      name: this.fileList[0].name,
      uid: this.fileList[0].uid,
      progress: 0,
      status: true,
      success: 0
    };
    let requery = (file) => {
      file.progress += 3;
      setTimeout(() => {
        if (file.progress < 100) {
          requery(file)
        }
      }, 100)
    };
    requery(this.file);
    const req = new HttpRequest('PUT', url, formData, {
      reportProgress: true,
      withCredentials: true
    });
    //Upload pre-empty array
    this.fileList = [];
    this.http.request(req)
      .pipe(filter(e => e instanceof HttpResponse))
      .subscribe(
        (event: {}) => {
          this.file.progress = 100;
          this.file.status = false;
          this.uploading = false;
          this.msg.success('upload successfully.');
          this.currentTab === 'NS' ? this.getTableData() : (this.currentTab === 'VNF' ? this.getTableVnfData() : this.getTablePnfData());
        },
        err => {
          this.file.progress = 100;
          this.file.status = false;
          this.file.success = 1;
          this.uploading = false;
          this.msg.error('upload failed.');
        }
      );
  }

  // Get the NS list
  getTableData(): void{
    this.isSpinning = true;
    //ns vfc lists 
    this.myhttp.getOnboardTableData()
      .subscribe((data) => {
        this.nsTableData = data;
        //ns sdc list
        this.myhttp.getSDC_NSTableData()
          .subscribe((data) => {
            this.isSpinning = false; //loading hide
            let nsData = data;
            // this.NSTableData.map((nsvfc) => { nsvfc.sameid = nsData.find((nssdc) => { return nsvfc.id == nssdc.uuid }) && nsvfc.id; return nsvfc; });
            let sameData = nsData.filter((nssdc) => { return !this.nsTableData.find((nsvfc) => { return nsvfc.id == nssdc.uuid }) });
            this.nsTableData = this.nsTableData.concat(sameData);
          }, (err) => {
            console.error(err);
            this.isSpinning = false;
          })
      }, (err) => {
        console.error(err);
        this.isSpinning = false;
      })
  }

  // Get the vnf list
  getTableVnfData(): void{
    this.isSpinning = true;
    //vnf vfc lists
    this.myhttp.getOnboardTableVnfData()
      .subscribe((data) => {
        this.vnfTableData = data;
        //vnf sdc lists
        this.myhttp.getSDC_VNFTableData()
          .subscribe((data) => {
            this.isSpinning = false; //loading hide
            let vnfData = data;
            // this.VNFTableData.map((vnfvfc) => { vnfvfc.sameid = this.vnfData.find((nssdc) => { return vnfvfc.id == nssdc.uuid }) && vnfvfc.id; return vnfvfc; });
            let sameData = vnfData.filter((vnfsdc) => { return !this.vnfTableData.find((vnfvfc) => { return vnfvfc.id == vnfsdc.uuid }) });
            this.vnfTableData = this.vnfTableData.concat(sameData);
          }, (err) => {
            console.error(err);
            this.isSpinning = false;
          })
      }, (err) => {
        console.error(err);
        this.isSpinning = false;
      })
  }

  // Get pnf list
  getTablePnfData() {
    this.isSpinning = true;
    this.myhttp.getOnboardTablePnfData()
      .subscribe((data) => {
        this.pnfTableData = data;
        this.isSpinning = false;   //loading hide
      }, (err) => {
        console.error(err);
        this.isSpinning = false; 
      })
  }

  // confirm
  showConfirm(requestBody: object, id: string): void{
    let API = this.currentTab === 'NS'? 'getNsonboard' : 'getVnfonboard';
    this.modalService.confirm({
      nzTitle: '<p>Are you sure you want to do this?</p>',
      nzOnOk: () => {
        this.myhttp[API](requestBody)
          .subscribe((data) => {
            if (data.status == "success") {
              if(this.currentTab === 'NS'){
                this.isUpdate = false;
                this.notification.notificationSuccess(this.currentTab, "OnboardingState", id);
                this.getTableData();
              }else{
                this.jobId = data.jobId;
                this.queryProgress(this.jobId, id);
                this.getTableVnfData();
              }
            } else {
              this.isUpdate = false;
              this.notification.notificationFailed(this.currentTab, "OnboardingState", id);
              return false
            } 
          }, (err) => {
            console.log(err);
          })
      }
    })
  }


  // ns onboard Upload button
  updataService(id: string) {
    this.isUpdate = true;
    let requestBody = { "csarId": id };
    this.showConfirm(requestBody, id)
  } 

  //Progress Progress inquiry
  queryProgress(jobId: string, id: string): any{
    let mypromise = new Promise((res) => {
      this.myhttp.getProgress(jobId, 0)
        .subscribe((data) => {
          if (data.responseDescriptor == null || data.responseDescriptor == "null" || data.responseDescriptor.progress == undefined || data.responseDescriptor.progress == null) {
            this.isUpdate = true;
            setTimeout(() => {
              this.queryProgress(this.jobId, id);
            }, 10000)
            return false
          }
          if (data.responseDescriptor.progress > 100) {
            this.isUpdate = false;
            this.notification.notificationFailed(this.currentTab, 'OnboardingState', id);
          }else if (data.responseDescriptor.progress < 100) {
            this.isUpdate = true;
            setTimeout(() => {
              this.queryProgress(this.jobId, id);
            }, 5000)
          } else {
            res(data);
            this.isUpdate = false;
            this.notification.notificationSuccess(this.currentTab, 'OnboardingState', id);
          }
          return false
        })
    })
    return mypromise;
  }

  /* delete button */
  showDeleteConfirm(pkgid: string): void {
    this.modalService.confirm({
      nzTitle: 'Do you Want to delete these items?',
      nzContent: 'Do you Want to delete these items?',
      nzOkText: 'Yes',
      nzCancelText: 'No',
      nzOnOk: () => new Promise((resolve) => {
        this.deleteService(pkgid, resolve);
      }).catch(() => console.log('Oops errors!'))
    });
  }

  //delete nsItem
  deleteService(pkgid, resolve) {
    let API: string;
    if(this.currentTab === 'NS'){
      API = 'deleteNsIdData';
    }else if(this.currentTab === 'VNF'){
      API = 'deleteVnfIdData';
    }else{
      API = 'deletePnfIdData';
    }
    this.myhttp[API](pkgid)
      .subscribe((data) => {
        this.notification.notificationSuccess(this.currentTab, 'OnboardingState', pkgid);
        resolve()
        //refresh list after successful deletion
        switch(this.currentTab){
          case 'NS':
            this.getTableData();
            break
          case 'VNF':
            this.getTableVnfData();
            break
          case 'PNF':
            this.getTablePnfData();
            break
        }
      }, (err) => {
        console.log(err);
        this.notification.notificationFailed(this.currentTab, 'OnboardingState', pkgid);
      })
  }
}