aboutsummaryrefslogtreecommitdiffstats
path: root/src/app/admin/view-edit/ansible-server.component.ts
blob: b629b657a54011d39437e50961d50a213f44c04e (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
/*
============LICENSE_START==========================================
===================================================================
Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
===================================================================

Unless otherwise specified, all software contained herein is licensed
under the Apache License, Version 2.0 (the License);
you may not use this software 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.

============LICENSE_END============================================
*/

import * as XLSX from 'xlsx';
import * as _ from 'underscore';

import { ActivatedRoute, Router } from '@angular/router';
import { Component, OnInit, ViewChild } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { UtilityService } from '../../shared/services/utilityService/utility.service';

// Common Confirm Modal
import { DialogService } from 'ng2-bootstrap-modal';
import { ParamShareService } from '../../shared/services/paramShare.service';


declare var $: any;
type AOA = Array<Array<any>>;

@Component({
    selector: 'ansible-server',
    templateUrl: './ansible-server.component.html',
    styleUrls: ['./ansible-server.component.css'],
})
export class AnsibleServerComponent implements OnInit {
  
    //settings for the notifications.
    options = {
        timeOut: 4500,
        showProgressBar: true,
        pauseOnHover: true,
        clickToClose: true,
        maxLength: 200
    };
   
    public ownerIdErrMsg;
    public regionIdErrMsg;
    public tenantIdErrMsg
    public zeroTenantIdsErrorMsg;
    public porterrorMessage;
    public portwarningMessage;
    public errorMessage;
    public warningMessage;
    public invalid = true;
    public portinvalid = true;
    public item;
    public sample = {ownerid:"", regionid:"", tenantid:""};
    public updateIndex;
    public currentUser;
    constructor(
        private paramShareService: ParamShareService, 
        private router: Router, 
        private activeROute: ActivatedRoute,
        private utilService: UtilityService
        
      )
    {
      console.log("testing....");
    }

ngOnInit() {
    this.currentUser = localStorage['userId'];
    this.item = JSON.parse(sessionStorage.getItem("ansibleserver"));
    this.updateIndex = parseInt(sessionStorage.getItem("updateIndex"));
    console.log("index===>"+this.updateIndex);
    if(!this.paramShareService.ansibleServerData) {
        this.invalid = true;
    } else {
        this.invalid = false;
    }
    console.log("selecteditem===>"+JSON.stringify(this.item));
}

addInfo() {
    console.log("selectediteminfo===>"+JSON.stringify(this.item.info));
    if(this.validateTenantId()) {
        this.item.info.push(this.sample)
        this.sample = {ownerid:"", regionid:"", tenantid:""};
        this.zeroTenantIdsErrorMsg = "";
    }

}

removeInfo(index) {
    console.log("selectediteminfo===>"+index+JSON.stringify(this.item.info));
    this.item.info.splice(index, 1);
    console.log("selectediteminfo===>"+JSON.stringify(this.item.info));
    
}

// save() {
//     console.log("ansibleServerData===>"+ JSON.stringify(this.paramShareService.ansibleServerData))
//     let ansibleServer = this.createAnsibleserverData(this.item);
//     this.paramShareService.ansibleServerData["fqdn-list"].push(ansibleServer);
//      this.router.navigate(['../admin'], {
//                 relativeTo: this.activeROute
//             });
// }

update() {
    console.log("ansibleServerData===>"+ JSON.stringify(this.paramShareService.ansibleServerData))
    
        
        let ansibleServer = this.createAnsibleserverData(this.item);
        //need to revisit to initialize paramShareService.ansibleServerData
        if(!this.paramShareService.ansibleServerData) {
            this.paramShareService.ansibleServerData = {"fqdn-list" : []};
        }
        if(this.updateIndex != this.paramShareService.ansibleServerData["fqdn-list"].length) {
            ansibleServer["modify-username"] = this.currentUser;
            ansibleServer["modify-date"] = this.utilService.getDate();
            console.log("update........")
        }
        this.paramShareService.ansibleServerData["fqdn-list"][this.updateIndex] = ansibleServer;
        this.router.navigate(['../admin'], {
                relativeTo: this.activeROute
            });
    
    
}

cancel() {
    sessionStorage.removeItem("ansibleserver");
     this.router.navigate(['../admin'], {
                relativeTo: this.activeROute
            });
}

createAnsibleserverData(displayAnsibleServer){
    let cloudOwnerList = this.createCloudOwnerList(displayAnsibleServer);
    let anisble = { 
        "vnf-management-server-fqdn": displayAnsibleServer.server+":"+displayAnsibleServer.port,
        "cloud-owner-list":cloudOwnerList,
                    "description":displayAnsibleServer.descr,
                    "username":displayAnsibleServer.creator,
                    "create-date":displayAnsibleServer['created-date'],
                    "modify-username":displayAnsibleServer.modifier,
                    "modify-date":displayAnsibleServer['modified-date']
    };
    return anisble;

}

createCloudOwnerList(displayAnsibleServer) {
        let cloudOwnerList = [];

        //prepare unique cloud-owner
        for(let i=0; i<displayAnsibleServer.info.length; i++) {
            let info = displayAnsibleServer.info[i];
            let cloudOwner = {};
            cloudOwner["cloud-owner"] = info.ownerid;
            let exist = false;
            cloudOwnerList.forEach(element => {
                if(element["cloud-owner"] == info.ownerid) {
                    exist = true;
                    
                }
            });
            if(!exist){
                cloudOwnerList.push(cloudOwner);
            }
            
        }
        console.log("cloudOwnerList===>"+JSON.stringify(cloudOwnerList));

        //prepare region id
        cloudOwnerList.forEach(cloudOwner => {
            let regionIdList = [];
            for(let i=0; i<displayAnsibleServer.info.length; i++) {
                let info = displayAnsibleServer.info[i];
                if(cloudOwner["cloud-owner"] == info.ownerid) {
                    let exist = false;
                    regionIdList.forEach(element => {
                        if(element["region-id"] == info.regionid) {
                            exist = true;
                        }
                    });
                    if(!exist){
                        regionIdList.push({"region-id":info.regionid});
                    }
                }
                
        }
         cloudOwner["region-id-list"] = regionIdList;
        });
        
         console.log("cloudOwnerList===>"+JSON.stringify(cloudOwnerList));

         //prepare tenant id
        cloudOwnerList.forEach(cloudOwner => {
            cloudOwner["region-id-list"].forEach(regionid => {
            let teanantIdList = [];
            for(let i=0; i<displayAnsibleServer.info.length; i++) {
                let info = displayAnsibleServer.info[i];
                if(cloudOwner["cloud-owner"] == info.ownerid && regionid["region-id"] == info.regionid) {
                    let exist = false;
                    teanantIdList.forEach(element => {
                        if(element == info.tenantid) {
                            exist = true;
                        }
                    });
                    if(!exist){
                        teanantIdList.push(info.tenantid);
                    }
                }
                
        }
         regionid["tenant-id-list"] = teanantIdList;
         });
        });
        console.log("cloudOwnerList===>"+JSON.stringify(cloudOwnerList));
        return cloudOwnerList;
    }
    
    //validating the fdqn url
    validateFdqn(fdqn) {
        if (fdqn.trim().length < 1) {
            this.errorMessage = 'Please enter Configuration Server FQDN';
            this.warningMessage = '';
            this.invalid = true;
        } else if (fdqn.startsWith(' ') || fdqn.endsWith(' ')) {
            this.errorMessage = 'Leading and trailing spaces are not allowed';
            this.warningMessage = '';
            this.invalid = true;
        } else if (!(fdqn.startsWith('http') || fdqn.endsWith('https'))) {
            this.warningMessage = 'FDQN can start with eighther http or https protocol';
            this.errorMessage = '';
            this.invalid = false;
            
        // } else if (name.includes('  ')) {
        //     this.errorMessage = 'More than one space is not allowed in VNFC Type';
        //     this.invalid = true;
        // } else if (name.length > 50) {
        //     this.errorMessage = 'VNFC Type should be of minimum one character and maximum 50 character';
        //     this.invalid = true;
        //
     } else {
            this.invalid = false;
            this.errorMessage = '';
            this.warningMessage = '';
        }
    }

    //validating the port
    validatePort(port) {
        if (port.trim().length < 1) {
            this.porterrorMessage = 'Please enter port';
            this.portwarningMessage = '';
            this.invalid = true;
        } else if (port.startsWith(' ') || port.endsWith(' ')) {
            this.porterrorMessage = 'Leading and trailing spaces are not allowed';
            this.portwarningMessage = '';
            this.invalid = true;
            
        } else if (isNaN(port)) {
            this.portwarningMessage = '';
            this.porterrorMessage = 'Port should be a number';
            this.invalid = true;
            port = parseInt(port);
        } else if (!(0 <= port && port <= 65535 )) {
            this.portwarningMessage = 'Port should be a number in range of 0 to 65535';
            this.porterrorMessage = '';
            this.invalid = false;
        } else {
            this.invalid = false;
            this.porterrorMessage = '';
            this.portwarningMessage = '';
        }
    }

    validate() {
        this.validateFdqn(this.item.server);
        this.validatePort(this.item.port);
        if(this.item.info.length <= 0) {
            this.zeroTenantIdsErrorMsg = "Please add atleast one Tenant ID.";
            this.invalid = true;
        }
        if(!this.invalid) {
            this.update();
        }
    }

    validateTenantId(){
        let valid = true;
        if(this.sample.ownerid.trim().length == 0) {
            this.ownerIdErrMsg = "Enter OwnerID";
            valid = false;
        } else {
            this.ownerIdErrMsg = "";
        }
        if(this.sample.regionid.trim().length == 0) {
            this.regionIdErrMsg = "Enter RegionID";
            valid = false;
        } else {
            this.regionIdErrMsg = "";
        }
        if(this.sample.tenantid.trim().length == 0) {
            this.tenantIdErrMsg = "Enter TenantID";
            valid = false;
        } else {
            this.tenantIdErrMsg = "";
        }
        return valid;
    }


}