Authentication support for cdt
[appc/cdt.git] / src / app / admin / view-edit / ansible-server.component.ts
1 /*
2 ============LICENSE_START==========================================
3 ===================================================================
4 Copyright (C) 2018-2020 AT&T Intellectual Property. All rights reserved.
5 ===================================================================
6
7 Unless otherwise specified, all software contained herein is licensed
8 under the Apache License, Version 2.0 (the License);
9 you may not use this software except in compliance with the License.
10 You may obtain a copy of the License at
11
12     http://www.apache.org/licenses/LICENSE-2.0
13
14 Unless required by applicable law or agreed to in writing, software
15 distributed under the License is distributed on an "AS IS" BASIS,
16 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 See the License for the specific language governing permissions and
18 limitations under the License.
19
20 ============LICENSE_END============================================
21 */
22
23 import * as XLSX from 'xlsx';
24 import * as _ from 'underscore';
25
26 import { ActivatedRoute, Router } from '@angular/router';
27 import { Component, OnInit, ViewChild } from '@angular/core';
28 import { Observable } from 'rxjs/Observable';
29 import { UtilityService } from '../../shared/services/utilityService/utility.service';
30
31 // Common Confirm Modal
32 import { DialogService } from 'ng2-bootstrap-modal';
33 import { ParamShareService } from '../../shared/services/paramShare.service';
34
35
36 declare var $: any;
37 type AOA = Array<Array<any>>;
38
39 @Component({
40     selector: 'ansible-server',
41     templateUrl: './ansible-server.component.html',
42     styleUrls: ['./ansible-server.component.css'],
43 })
44 export class AnsibleServerComponent implements OnInit {
45   
46     //settings for the notifications.
47     options = {
48         timeOut: 4500,
49         showProgressBar: true,
50         pauseOnHover: true,
51         clickToClose: true,
52         maxLength: 200
53     };
54    
55     public ownerIdErrMsg;
56     public regionIdErrMsg;
57     public tenantIdErrMsg
58     public zeroTenantIdsErrorMsg;
59     public porterrorMessage;
60     public portwarningMessage;
61     public errorMessage;
62     public warningMessage;
63     public invalid = true;
64     public portinvalid = true;
65     public item;
66     public sample = {ownerid:"", regionid:"", tenantid:""};
67     public updateIndex;
68     public currentUser;
69     constructor(
70         private paramShareService: ParamShareService, 
71         private router: Router, 
72         private activeROute: ActivatedRoute,
73         private utilService: UtilityService
74         
75       )
76     {
77       console.log("testing....");
78     }
79
80 ngOnInit() {
81     this.currentUser = sessionStorage['userId'];
82     this.item = JSON.parse(sessionStorage.getItem("ansibleserver"));
83     this.updateIndex = parseInt(sessionStorage.getItem("updateIndex"));
84     console.log("index===>"+this.updateIndex);
85     if(!this.paramShareService.ansibleServerData) {
86         this.invalid = true;
87     } else {
88         this.invalid = false;
89     }
90     console.log("selecteditem===>"+JSON.stringify(this.item));
91 }
92
93 addInfo() {
94     console.log("selectediteminfo===>"+JSON.stringify(this.item.info));
95     if(this.validateTenantId()) {
96         this.item.info.push(this.sample)
97         this.sample = {ownerid:"", regionid:"", tenantid:""};
98         this.zeroTenantIdsErrorMsg = "";
99     }
100
101 }
102
103 removeInfo(index) {
104     console.log("selectediteminfo===>"+index+JSON.stringify(this.item.info));
105     this.item.info.splice(index, 1);
106     console.log("selectediteminfo===>"+JSON.stringify(this.item.info));
107     
108 }
109
110 // save() {
111 //     console.log("ansibleServerData===>"+ JSON.stringify(this.paramShareService.ansibleServerData))
112 //     let ansibleServer = this.createAnsibleserverData(this.item);
113 //     this.paramShareService.ansibleServerData["fqdn-list"].push(ansibleServer);
114 //      this.router.navigate(['../admin'], {
115 //                 relativeTo: this.activeROute
116 //             });
117 // }
118
119 update() {
120     console.log("ansibleServerData===>"+ JSON.stringify(this.paramShareService.ansibleServerData))
121     
122         
123         let ansibleServer = this.createAnsibleserverData(this.item);
124         //need to revisit to initialize paramShareService.ansibleServerData
125         if(!this.paramShareService.ansibleServerData) {
126             this.paramShareService.ansibleServerData = {"fqdn-list" : []};
127         }
128         if(this.updateIndex != this.paramShareService.ansibleServerData["fqdn-list"].length) {
129             ansibleServer["modify-username"] = this.currentUser;
130             ansibleServer["modify-date"] = this.utilService.getDate();
131             console.log("update........")
132         }
133         this.paramShareService.ansibleServerData["fqdn-list"][this.updateIndex] = ansibleServer;
134         this.router.navigate(['../admin'], {
135                 relativeTo: this.activeROute
136             });
137     
138     
139 }
140
141 cancel() {
142     sessionStorage.removeItem("ansibleserver");
143      this.router.navigate(['../admin'], {
144                 relativeTo: this.activeROute
145             });
146 }
147
148 createAnsibleserverData(displayAnsibleServer){
149     let cloudOwnerList = this.createCloudOwnerList(displayAnsibleServer);
150     let anisble = { 
151         "vnf-management-server-fqdn": displayAnsibleServer.server+":"+displayAnsibleServer.port,
152         "cloud-owner-list":cloudOwnerList,
153                     "description":displayAnsibleServer.descr,
154                     "username":displayAnsibleServer.creator,
155                     "create-date":displayAnsibleServer['created-date'],
156                     "modify-username":displayAnsibleServer.modifier,
157                     "modify-date":displayAnsibleServer['modified-date']
158     };
159     return anisble;
160
161 }
162
163 createCloudOwnerList(displayAnsibleServer) {
164         let cloudOwnerList = [];
165
166         //prepare unique cloud-owner
167         for(let i=0; i<displayAnsibleServer.info.length; i++) {
168             let info = displayAnsibleServer.info[i];
169             let cloudOwner = {};
170             cloudOwner["cloud-owner"] = info.ownerid;
171             let exist = false;
172             cloudOwnerList.forEach(element => {
173                 if(element["cloud-owner"] == info.ownerid) {
174                     exist = true;
175                     
176                 }
177             });
178             if(!exist){
179                 cloudOwnerList.push(cloudOwner);
180             }
181             
182         }
183         console.log("cloudOwnerList===>"+JSON.stringify(cloudOwnerList));
184
185         //prepare region id
186         cloudOwnerList.forEach(cloudOwner => {
187             let regionIdList = [];
188             for(let i=0; i<displayAnsibleServer.info.length; i++) {
189                 let info = displayAnsibleServer.info[i];
190                 if(cloudOwner["cloud-owner"] == info.ownerid) {
191                     let exist = false;
192                     regionIdList.forEach(element => {
193                         if(element["region-id"] == info.regionid) {
194                             exist = true;
195                         }
196                     });
197                     if(!exist){
198                         regionIdList.push({"region-id":info.regionid});
199                     }
200                 }
201                 
202         }
203          cloudOwner["region-id-list"] = regionIdList;
204         });
205         
206          console.log("cloudOwnerList===>"+JSON.stringify(cloudOwnerList));
207
208          //prepare tenant id
209         cloudOwnerList.forEach(cloudOwner => {
210             cloudOwner["region-id-list"].forEach(regionid => {
211             let teanantIdList = [];
212             for(let i=0; i<displayAnsibleServer.info.length; i++) {
213                 let info = displayAnsibleServer.info[i];
214                 if(cloudOwner["cloud-owner"] == info.ownerid && regionid["region-id"] == info.regionid) {
215                     let exist = false;
216                     teanantIdList.forEach(element => {
217                         if(element == info.tenantid) {
218                             exist = true;
219                         }
220                     });
221                     if(!exist){
222                         teanantIdList.push(info.tenantid);
223                     }
224                 }
225                 
226         }
227          regionid["tenant-id-list"] = teanantIdList;
228          });
229         });
230         console.log("cloudOwnerList===>"+JSON.stringify(cloudOwnerList));
231         return cloudOwnerList;
232     }
233     
234     //validating the fdqn url
235     validateFdqn(fdqn) {
236         if (fdqn.trim().length < 1) {
237             this.errorMessage = 'Please enter Configuration Server FQDN';
238             this.warningMessage = '';
239             this.invalid = true;
240         } else if (fdqn.startsWith(' ') || fdqn.endsWith(' ')) {
241             this.errorMessage = 'Leading and trailing spaces are not allowed';
242             this.warningMessage = '';
243             this.invalid = true;
244         } else if (!(fdqn.startsWith('http') || fdqn.endsWith('https'))) {
245             this.warningMessage = 'FDQN can start with eighther http or https protocol';
246             this.errorMessage = '';
247             this.invalid = false;
248             
249         // } else if (name.includes('  ')) {
250         //     this.errorMessage = 'More than one space is not allowed in VNFC Type';
251         //     this.invalid = true;
252         // } else if (name.length > 50) {
253         //     this.errorMessage = 'VNFC Type should be of minimum one character and maximum 50 character';
254         //     this.invalid = true;
255         //
256      } else {
257             this.invalid = false;
258             this.errorMessage = '';
259             this.warningMessage = '';
260         }
261     }
262
263     //validating the port
264     validatePort(port) {
265         if (port.trim().length < 1) {
266             this.porterrorMessage = 'Please enter port';
267             this.portwarningMessage = '';
268             this.invalid = true;
269         } else if (port.startsWith(' ') || port.endsWith(' ')) {
270             this.porterrorMessage = 'Leading and trailing spaces are not allowed';
271             this.portwarningMessage = '';
272             this.invalid = true;
273             
274         } else if (isNaN(port)) {
275             this.portwarningMessage = '';
276             this.porterrorMessage = 'Port should be a number';
277             this.invalid = true;
278             port = parseInt(port);
279         } else if (!(0 <= port && port <= 65535 )) {
280             this.portwarningMessage = 'Port should be a number in range of 0 to 65535';
281             this.porterrorMessage = '';
282             this.invalid = false;
283         } else {
284             this.invalid = false;
285             this.porterrorMessage = '';
286             this.portwarningMessage = '';
287         }
288     }
289
290     validate() {
291         this.validateFdqn(this.item.server);
292         this.validatePort(this.item.port);
293         if(this.item.info.length <= 0) {
294             this.zeroTenantIdsErrorMsg = "Please add atleast one Tenant ID.";
295             this.invalid = true;
296         }
297         if(!this.invalid) {
298             this.update();
299         }
300     }
301
302     validateTenantId(){
303         let valid = true;
304         if(this.sample.ownerid.trim().length == 0) {
305             this.ownerIdErrMsg = "Enter OwnerID";
306             valid = false;
307         } else {
308             this.ownerIdErrMsg = "";
309         }
310         if(this.sample.regionid.trim().length == 0) {
311             this.regionIdErrMsg = "Enter RegionID";
312             valid = false;
313         } else {
314             this.regionIdErrMsg = "";
315         }
316         if(this.sample.tenantid.trim().length == 0) {
317             this.tenantIdErrMsg = "Enter TenantID";
318             valid = false;
319         } else {
320             this.tenantIdErrMsg = "";
321         }
322         return valid;
323     }
324
325
326 }