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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
|
/*-
* ============LICENSE_START==========================================
* ONAP Portal
* ===================================================================
* Copyright (C) 2017-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.
*
* Unless otherwise specified, all documentation contained herein is licensed
* under the Creative Commons License, Attribution 4.0 Intl. (the "License");
* you may not use this documentation except in compliance with the License.
* You may obtain a copy of the License at
*
* https://creativecommons.org/licenses/by/4.0/
*
* Unless required by applicable law or agreed to in writing, documentation
* 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============================================
*
*
*/
package org.onap.portalapp.portal.controller;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.onap.portalapp.portal.domain.CentralV2RoleFunction;
import org.onap.portalapp.portal.domain.EPApp;
import org.onap.portalapp.portal.domain.EPUser;
import org.onap.portalapp.portal.domain.EcompAuditLog;
import org.onap.portalapp.portal.ecomp.model.PortalRestResponse;
import org.onap.portalapp.portal.ecomp.model.PortalRestStatusEnum;
import org.onap.portalapp.portal.exceptions.InvalidRoleException;
import org.onap.portalapp.portal.logging.aop.EPAuditLog;
import org.onap.portalapp.portal.logging.aop.EPEELFLoggerAdvice;
import org.onap.portalapp.portal.logging.logic.EPLogUtil;
import org.onap.portalapp.portal.service.ExternalAccessRolesService;
import org.onap.portalapp.portal.transport.CentralRole;
import org.onap.portalapp.portal.transport.CentralRoleFunction;
import org.onap.portalapp.portal.transport.CentralUser;
import org.onap.portalapp.portal.transport.CentralV2Role;
import org.onap.portalapp.portal.transport.ExternalRequestFieldsValidator;
import org.onap.portalapp.portal.utils.EPCommonSystemProperties;
import org.onap.portalapp.portal.utils.EcompPortalUtils;
import org.onap.portalapp.portal.utils.PortalConstants;
import org.onap.portalsdk.core.domain.AuditLog;
import org.onap.portalsdk.core.domain.Role;
import org.onap.portalsdk.core.domain.User;
import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
import org.onap.portalsdk.core.restful.domain.EcompRole;
import org.onap.portalsdk.core.restful.domain.EcompUser;
import org.onap.portalsdk.core.service.AuditService;
import org.onap.portalsdk.core.service.UserService;
import org.onap.portalsdk.core.service.UserServiceCentalizedImpl;
import org.onap.portalsdk.core.util.SystemProperties;
import org.onap.portalsdk.core.web.support.UserUtils;
import org.slf4j.MDC;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.TypeFactory;
import io.swagger.annotations.ApiOperation;
@RestController
@RequestMapping("/auxapi")
@org.springframework.context.annotation.Configuration
@EnableAspectJAutoProxy
@EPAuditLog
public class ExternalAccessRolesController implements BasicAuthenticationController {
private static final String ROLE_INVALID_CHARS = "%=():,\"\"";
private static final String SUCCESSFULLY_DELETED = "Successfully Deleted";
private static final String INVALID_UEB_KEY = "Invalid credentials!";
private static final String LOGIN_ID = "LoginId";
RestTemplate template = new RestTemplate();
@Autowired
private AuditService auditService;
private static final String UEBKEY = "uebkey";
private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(ExternalAccessRolesController.class);
@Autowired
private ExternalAccessRolesService externalAccessRolesService;
@Autowired
private UserService userservice = new UserServiceCentalizedImpl();
@ApiOperation(value = "Gets user role for an application.", response = CentralUser.class, responseContainer="List")
@RequestMapping(value = {
"/user/{loginId}" }, method = RequestMethod.GET, produces = "application/json")
public CentralUser getUser(HttpServletRequest request, HttpServletResponse response,
@PathVariable("loginId") String loginId) throws Exception {
CentralUser answer = null;
try {
fieldsValidation(request);
answer = externalAccessRolesService.getUserRoles(loginId, request.getHeader(UEBKEY));
} catch (Exception e) {
sendErrorResponse(response, e);
logger.error(EELFLoggerDelegate.errorLogger, "getUser failed", e);
}
return answer;
}
@ApiOperation(value = "Gets user roles for an application which is upgraded to newer version.", response = String.class, responseContainer="List")
@RequestMapping(value = {
"/v1/user/{loginId}" }, method = RequestMethod.GET, produces = "application/json")
public String getV2UserList(HttpServletRequest request, HttpServletResponse response,
@PathVariable("loginId") String loginId) throws Exception {
String answer = null;
try {
fieldsValidation(request);
answer = externalAccessRolesService.getV2UserWithRoles(loginId, request.getHeader(UEBKEY));
} catch (Exception e) {
sendErrorResponse(response, e);
logger.error(EELFLoggerDelegate.errorLogger, "getV2UserList failed", e);
}
return answer;
}
@ApiOperation(value = "Gets roles for an application.", response = CentralRole.class, responseContainer="Json")
@RequestMapping(value = {
"/roles" }, method = RequestMethod.GET, produces = "application/json")
public List<CentralRole> getRolesForApp(HttpServletRequest request, HttpServletResponse response) throws Exception {
logger.debug(EELFLoggerDelegate.debugLogger, "Request received for getRolesForApp");
List<CentralV2Role> v2CenRole = null;
List<CentralRole> cenRole = null;
try {
fieldsValidation(request);
EPApp app = externalAccessRolesService.getApp(request.getHeader(UEBKEY)).get(0);
// Sync all roles from external system into ONAP portal DB
logger.debug(EELFLoggerDelegate.debugLogger, "getRolesForApp: Entering into syncApplicationRolesWithEcompDB");
externalAccessRolesService.syncApplicationRolesWithEcompDB(app);
logger.debug(EELFLoggerDelegate.debugLogger, "getRolesForApp: Finished syncApplicationRolesWithEcompDB");
v2CenRole = externalAccessRolesService.getRolesForApp(request.getHeader(UEBKEY));
cenRole = externalAccessRolesService.convertV2CentralRoleListToOldVerisonCentralRoleList(v2CenRole);
} catch (Exception e) {
sendErrorResponse(response, e);
logger.error(EELFLoggerDelegate.errorLogger, "getRolesForApp failed", e);
}
logger.debug(EELFLoggerDelegate.debugLogger, "Request completed for getRolesForApp");
return cenRole;
}
@ApiOperation(value = "Gets roles for an application which is upgraded to newer version.", response = CentralV2Role.class, responseContainer="Json")
@RequestMapping(value = {
"/v1/roles" }, method = RequestMethod.GET, produces = "application/json")
public List<CentralV2Role> getV2RolesForApp(HttpServletRequest request, HttpServletResponse response) throws Exception {
logger.debug(EELFLoggerDelegate.debugLogger, "Request received for getV2RolesForApp");
List<CentralV2Role> answer = null;
try {
fieldsValidation(request);
EPApp app = externalAccessRolesService.getApp(request.getHeader(UEBKEY)).get(0);
// Sync all roles from external system into ONAP portal DB
logger.debug(EELFLoggerDelegate.debugLogger, "getV2RolesForApp: Entering into syncApplicationRolesWithEcompDB");
externalAccessRolesService.syncApplicationRolesWithEcompDB(app);
logger.debug(EELFLoggerDelegate.debugLogger, "getV2RolesForApp: Finished syncApplicationRolesWithEcompDB");
answer = externalAccessRolesService.getRolesForApp(request.getHeader(UEBKEY));
} catch (Exception e) {
sendErrorResponse(response, e);
logger.error(EELFLoggerDelegate.errorLogger, "getV2RolesForApp failed", e);
}
logger.debug(EELFLoggerDelegate.debugLogger, "Request completed for getV2RolesForApp");
return answer;
}
@ApiOperation(value = "Gets all role functions for an application for older version.", response = CentralRoleFunction.class, responseContainer="Json")
@RequestMapping(value = {
"/functions" }, method = RequestMethod.GET, produces = "application/json")
public List<CentralRoleFunction> getRoleFunctionsList(HttpServletRequest request, HttpServletResponse response)
throws Exception {
List<CentralV2RoleFunction> answer = null;
List<CentralRoleFunction> roleFuncList = null;
logger.debug(EELFLoggerDelegate.debugLogger, "Request received for getRoleFunctionsList");
try {
fieldsValidation(request);
EPApp app = externalAccessRolesService.getApp(request.getHeader(UEBKEY)).get(0);
// Sync all functions from external system into ONAP portal DB
logger.debug(EELFLoggerDelegate.debugLogger, "getRoleFunctionsList: Entering into syncRoleFunctionFromExternalAccessSystem");
externalAccessRolesService.syncRoleFunctionFromExternalAccessSystem(app);
logger.debug(EELFLoggerDelegate.debugLogger, "getRoleFunctionsList: Finished syncRoleFunctionFromExternalAccessSystem");
answer = externalAccessRolesService.getRoleFuncList(request.getHeader(UEBKEY));
roleFuncList = externalAccessRolesService.convertCentralRoleFunctionToRoleFunctionObject(answer);
} catch (Exception e) {
sendErrorResponse(response, e);
logger.error(EELFLoggerDelegate.errorLogger, "getRoleFunctionsList failed", e);
}
logger.debug(EELFLoggerDelegate.debugLogger, "Request completed for getRoleFunctionsList");
return roleFuncList;
}
@ApiOperation(value = "Gets all role functions for an application which is upgraded to newer version.", response = CentralV2RoleFunction.class, responseContainer="Json")
@RequestMapping(value = {
"/v1/functions" }, method = RequestMethod.GET, produces = "application/json")
public List<CentralV2RoleFunction> getV2RoleFunctionsList(HttpServletRequest request, HttpServletResponse response)
throws Exception {
List<CentralV2RoleFunction> cenRoleFuncList = null;
logger.debug(EELFLoggerDelegate.debugLogger, "Request received for getV2RoleFunctionsList");
try {
fieldsValidation(request);
EPApp app = externalAccessRolesService.getApp(request.getHeader(UEBKEY)).get(0);
// Sync all functions from external system into ONAP portal DB
logger.debug(EELFLoggerDelegate.debugLogger, "getV2RoleFunctionsList: Entering into syncRoleFunctionFromExternalAccessSystem");
externalAccessRolesService.syncRoleFunctionFromExternalAccessSystem(app);
logger.debug(EELFLoggerDelegate.debugLogger, "getV2RoleFunctionsList: Finished syncRoleFunctionFromExternalAccessSystem");
cenRoleFuncList = externalAccessRolesService.getRoleFuncList(request.getHeader(UEBKEY));
} catch (Exception e) {
sendErrorResponse(response, e);
logger.error(EELFLoggerDelegate.errorLogger, "getV2RoleFunctionsList failed", e);
}
logger.debug(EELFLoggerDelegate.debugLogger, "Request completed for getV2RoleFunctionsList");
return cenRoleFuncList;
}
@ApiOperation(value = "Gets role information for an application.", response = CentralRole.class, responseContainer="Json")
@RequestMapping(value = {
"/role/{role_id}" }, method = RequestMethod.GET, produces = "application/json")
public CentralRole getRoleInfo(HttpServletRequest request, HttpServletResponse response,
@PathVariable("role_id") Long roleId) throws Exception {
CentralV2Role answer = null;
CentralRole cenRole = null;
logger.debug(EELFLoggerDelegate.debugLogger, "Request received for getRoleInfo");
try {
fieldsValidation(request);
answer = externalAccessRolesService.getRoleInfo(roleId, request.getHeader(UEBKEY));
cenRole = externalAccessRolesService.convertV2CentralRoleToOldVerisonCentralRole(answer);
} catch (Exception e) {
sendErrorResponse(response, e);
logger.error(EELFLoggerDelegate.errorLogger, "getRoleInfo failed", e);
}
logger.debug(EELFLoggerDelegate.debugLogger, "Request completed for getRoleInfo");
return cenRole;
}
@ApiOperation(value = "Gets v2 role information for an application which is upgraded to newer version.", response = CentralV2Role.class, responseContainer="Json")
@RequestMapping(value = {
"/v1/role/{role_id}" }, method = RequestMethod.GET, produces = "application/json")
public CentralV2Role getV2RoleInfo(HttpServletRequest request, HttpServletResponse response,
@PathVariable("role_id") Long roleId) throws Exception {
CentralV2Role answer = null;
logger.debug(EELFLoggerDelegate.debugLogger, "Request received for getV2RoleInfo");
try {
fieldsValidation(request);
answer = externalAccessRolesService.getRoleInfo(roleId, request.getHeader(UEBKEY));
} catch (Exception e) {
sendErrorResponse(response, e);
logger.error(EELFLoggerDelegate.errorLogger, "getV2RoleInfo failed", e);
}
logger.debug(EELFLoggerDelegate.debugLogger, "Request completed for getV2RoleInfo");
return answer;
}
@ApiOperation(value = "Gets role information for an application provided by function code.", response = CentralRoleFunction.class, responseContainer = "Json")
@RequestMapping(value = { "/function/{code}" }, method = RequestMethod.GET, produces = "application/json")
public CentralRoleFunction getRoleFunction(HttpServletRequest request, HttpServletResponse response,
@PathVariable("code") String code) throws Exception {
CentralV2RoleFunction centralV2RoleFunction = null;
CentralRoleFunction centralRoleFunction = new CentralRoleFunction();
try {
fieldsValidation(request);
centralV2RoleFunction = externalAccessRolesService.getRoleFunction(code, request.getHeader(UEBKEY));
if(centralV2RoleFunction != null && EcompPortalUtils.getFunctionCode(centralV2RoleFunction.getCode()).equals(code)) {
BeanUtils.copyProperties(centralV2RoleFunction, centralRoleFunction, "type","action");
}
} catch (Exception e) {
sendErrorResponse(response, e);
logger.error(EELFLoggerDelegate.errorLogger, "getRoleFunction failed", e);
}
return centralRoleFunction;
}
@ApiOperation(value = "Gets role information for an application provided by function code.", response = CentralV2RoleFunction.class, responseContainer = "Json")
@RequestMapping(value = { "/v1/function/{code}" }, method = RequestMethod.GET, produces = "application/json")
public CentralV2RoleFunction getV2RoleFunction(HttpServletRequest request, HttpServletResponse response,
@PathVariable("code") String code) throws Exception {
CentralV2RoleFunction centralV2RoleFunction = null;
try {
fieldsValidation(request);
centralV2RoleFunction = externalAccessRolesService.getRoleFunction(code, request.getHeader(UEBKEY));
if(centralV2RoleFunction == null || !EcompPortalUtils.getFunctionCode(centralV2RoleFunction.getCode()).equals(code)) {
centralV2RoleFunction = new CentralV2RoleFunction();
}
} catch (Exception e) {
sendErrorResponse(response, e);
logger.error(EELFLoggerDelegate.errorLogger, "getV2RoleFunction failed", e);
}
return centralV2RoleFunction;
}
@ApiOperation(value = "Saves role function for an application.", response = PortalRestResponse.class, responseContainer = "Json")
@RequestMapping(value = { "/roleFunction" }, method = RequestMethod.POST, produces = "application/json")
public PortalRestResponse<String> saveRoleFunction(HttpServletRequest request, HttpServletResponse response,
@RequestBody String roleFunc) throws Exception {
String status = "Successfully saved!";
try {
fieldsValidation(request);
String data = roleFunc;
ObjectMapper mapper = new ObjectMapper();
List<EPApp> applicationList = externalAccessRolesService.getApp(request.getHeader(UEBKEY));
EPApp requestedApp = applicationList.get(0);
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
CentralV2RoleFunction availableRoleFunction = mapper.readValue(data, CentralV2RoleFunction.class);
CentralV2RoleFunction domainRoleFunction = null;
boolean isCentralV2Version = false;
if(availableRoleFunction.getType()!=null && availableRoleFunction.getAction()!= null) {
isCentralV2Version = true;
}
if(isCentralV2Version) {
String code = availableRoleFunction.getType()+"|"+availableRoleFunction.getCode()+"|"+availableRoleFunction.getAction();
domainRoleFunction = externalAccessRolesService.getRoleFunction(code,
requestedApp.getUebKey());
} else {
domainRoleFunction = externalAccessRolesService.getRoleFunction(availableRoleFunction.getCode(),
requestedApp.getUebKey());
}
boolean saveOrUpdateResponse = false;
if (domainRoleFunction != null && isCentralV2Version && domainRoleFunction.getCode().equals(availableRoleFunction.getCode())
&& domainRoleFunction.getType().equals(availableRoleFunction.getType())
&& domainRoleFunction.getAction().equals(availableRoleFunction.getAction())) {
domainRoleFunction.setName(availableRoleFunction.getName());
saveOrUpdateResponse = externalAccessRolesService.saveCentralRoleFunction(domainRoleFunction,
requestedApp);
} else {
availableRoleFunction.setAppId(requestedApp.getId());
saveOrUpdateResponse = externalAccessRolesService.saveCentralRoleFunction(availableRoleFunction,
requestedApp);
}
if(domainRoleFunction != null) {
status = "Successfully updated!";
}
if (saveOrUpdateResponse) {
EPUser user = externalAccessRolesService.getUser(request.getHeader(LOGIN_ID)).get(0);
EPApp app = externalAccessRolesService.getApp(request.getHeader(UEBKEY)).get(0);
String activityCode = (!status.equals("Successfully updated!"))
? EcompAuditLog.CD_ACTIVITY_EXTERNAL_AUTH_ADD_FUNCTION
: EcompAuditLog.CD_ACTIVITY_EXTERNAL_AUTH_UPDATE_FUNCTION;
logger.info(EELFLoggerDelegate.applicationLogger, "saveRoleFunction: succeeded for app {}, function {}",
app.getId(), availableRoleFunction.getCode());
AuditLog auditLog = new AuditLog();
auditLog.setUserId(user.getId());
auditLog.setActivityCode(activityCode);
auditLog.setComments(
EcompPortalUtils.truncateString(
"saveRoleFunction role for app:" + app.getId() + " and function:'"
+ availableRoleFunction.getCode() + "'",
PortalConstants.AUDIT_LOG_COMMENT_SIZE));
auditLog.setAffectedRecordId(user.getOrgUserId());
auditService.logActivity(auditLog, null);
MDC.put(EPCommonSystemProperties.AUDITLOG_BEGIN_TIMESTAMP, EPEELFLoggerAdvice.getCurrentDateTimeUTC());
MDC.put(EPCommonSystemProperties.AUDITLOG_END_TIMESTAMP, EPEELFLoggerAdvice.getCurrentDateTimeUTC());
EcompPortalUtils.calculateDateTimeDifferenceForLog(
MDC.get(EPCommonSystemProperties.AUDITLOG_BEGIN_TIMESTAMP),
MDC.get(EPCommonSystemProperties.AUDITLOG_END_TIMESTAMP));
logger.info(EELFLoggerDelegate.auditLogger,
EPLogUtil.formatAuditLogMessage("ExternalAccessRolesController.saveRoleFunction", activityCode,
String.valueOf(user.getId()), user.getOrgUserId(), availableRoleFunction.getCode()));
MDC.remove(EPCommonSystemProperties.AUDITLOG_BEGIN_TIMESTAMP);
MDC.remove(EPCommonSystemProperties.AUDITLOG_END_TIMESTAMP);
MDC.remove(SystemProperties.MDC_TIMER);
} else {
logger.error(EELFLoggerDelegate.errorLogger, "saveRoleFunction failed");
return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR,
"Failed to saveRoleFunction for '" + availableRoleFunction.getCode() + "'", "Failed");
}
} catch (Exception e) {
if (e.getMessage() == null ||e.getMessage().contains(INVALID_UEB_KEY)) {
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
}else {
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
logger.error(EELFLoggerDelegate.errorLogger, "saveRoleFunction failed", e);
return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, e.getMessage(), "Failed");
}
return new PortalRestResponse<String>(PortalRestStatusEnum.OK, status, "Success");
}
@ApiOperation(value = "Deletes role function for an application.", response = PortalRestResponse.class, responseContainer = "Json")
@RequestMapping(value = { "/roleFunction/{code}" }, method = RequestMethod.DELETE, produces = "application/json")
public PortalRestResponse<String> deleteRoleFunction(HttpServletRequest request, HttpServletResponse response,
@PathVariable("code") String code) throws Exception {
try {
fieldsValidation(request);
EPUser user = externalAccessRolesService.getUser(request.getHeader(LOGIN_ID)).get(0);
EPApp app = externalAccessRolesService.getApp(request.getHeader(UEBKEY)).get(0);
boolean getDelFuncResponse = externalAccessRolesService.deleteCentralRoleFunction(code, app);
if (getDelFuncResponse) {
logger.info(EELFLoggerDelegate.applicationLogger, "deleteRoleFunction: succeeded for app {}, role {}",
app.getId(), code);
AuditLog auditLog = new AuditLog();
auditLog.setUserId(user.getId());
auditLog.setActivityCode(EcompAuditLog.CD_ACTIVITY_EXTERNAL_AUTH_DELETE_FUNCTION);
auditLog.setComments(EcompPortalUtils.truncateString(
"Deleted function for app:" + app.getId() + " and function code:'" + code + "'",
PortalConstants.AUDIT_LOG_COMMENT_SIZE));
auditLog.setAffectedRecordId(user.getOrgUserId());
auditService.logActivity(auditLog, null);
MDC.put(EPCommonSystemProperties.AUDITLOG_BEGIN_TIMESTAMP, EPEELFLoggerAdvice.getCurrentDateTimeUTC());
MDC.put(EPCommonSystemProperties.AUDITLOG_END_TIMESTAMP, EPEELFLoggerAdvice.getCurrentDateTimeUTC());
EcompPortalUtils.calculateDateTimeDifferenceForLog(
MDC.get(EPCommonSystemProperties.AUDITLOG_BEGIN_TIMESTAMP),
MDC.get(EPCommonSystemProperties.AUDITLOG_END_TIMESTAMP));
logger.info(EELFLoggerDelegate.auditLogger,
EPLogUtil.formatAuditLogMessage("ExternalAccessRolesController.deleteRoleFunction",
EcompAuditLog.CD_ACTIVITY_EXTERNAL_AUTH_DELETE_FUNCTION, String.valueOf(user.getId()),
user.getOrgUserId(), code));
MDC.remove(EPCommonSystemProperties.AUDITLOG_BEGIN_TIMESTAMP);
MDC.remove(EPCommonSystemProperties.AUDITLOG_END_TIMESTAMP);
MDC.remove(SystemProperties.MDC_TIMER);
} else {
logger.error(EELFLoggerDelegate.errorLogger, "deleteRoleFunction failed");
return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR,
"Failed to deleteRoleFunction for '" + code + "'", "Failed");
}
} catch (Exception e) {
if (e.getMessage().contains(INVALID_UEB_KEY)) {
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
} else {
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
}
logger.error(EELFLoggerDelegate.errorLogger, "deleteRoleFunction failed", e);
return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, e.getMessage(), "Failed");
}
return new PortalRestResponse<>(PortalRestStatusEnum.OK, SUCCESSFULLY_DELETED, "Success");
}
@ApiOperation(value = "Saves role for an application.", response = PortalRestResponse.class, responseContainer = "Json")
@RequestMapping(value = { "/role" }, method = RequestMethod.POST, produces = "application/json")
public PortalRestResponse<String> saveRole(HttpServletRequest request, HttpServletResponse response,
@RequestBody Role role) throws Exception {
try {
fieldsValidation(request);
ExternalRequestFieldsValidator saveRoleResult = null;
EPUser user = externalAccessRolesService.getUser(request.getHeader(LOGIN_ID)).get(0);
EPApp app = externalAccessRolesService.getApp(request.getHeader(UEBKEY)).get(0);
if(role.getId()!=null && StringUtils.containsAny(role.getName(), ROLE_INVALID_CHARS)) {
throw new InvalidRoleException("Invalid role name found for '" + role.getName() + "'. Any one of the following characters '%,(),=,:,comma, and double quotes' are not allowed");
}
saveRoleResult = externalAccessRolesService.saveRoleForApplication(role, request.getHeader(UEBKEY));
if (saveRoleResult.isResult()) {
String activityCode = (role.getId() == null) ? EcompAuditLog.CD_ACTIVITY_EXTERNAL_AUTH_ADD_ROLE
: EcompAuditLog.CD_ACTIVITY_EXTERNAL_AUTH_UPDATE_ROLE_AND_FUNCTION;
logger.info(EELFLoggerDelegate.applicationLogger, "saveRole: succeeded for app {}, role {}",
app.getId(), role.getName());
AuditLog auditLog = new AuditLog();
auditLog.setUserId(user.getId());
auditLog.setActivityCode(activityCode);
auditLog.setComments(EcompPortalUtils.truncateString(
"saveRole role for app:" + app.getId() + " and role:'" + role.getName() + "'",
PortalConstants.AUDIT_LOG_COMMENT_SIZE));
auditLog.setAffectedRecordId(user.getOrgUserId());
auditService.logActivity(auditLog, null);
MDC.put(EPCommonSystemProperties.AUDITLOG_BEGIN_TIMESTAMP, EPEELFLoggerAdvice.getCurrentDateTimeUTC());
MDC.put(EPCommonSystemProperties.AUDITLOG_END_TIMESTAMP, EPEELFLoggerAdvice.getCurrentDateTimeUTC());
EcompPortalUtils.calculateDateTimeDifferenceForLog(
MDC.get(EPCommonSystemProperties.AUDITLOG_BEGIN_TIMESTAMP),
MDC.get(EPCommonSystemProperties.AUDITLOG_END_TIMESTAMP));
logger.info(EELFLoggerDelegate.auditLogger,
EPLogUtil.formatAuditLogMessage("ExternalAccessRolesController.saveRole", activityCode,
String.valueOf(user.getId()), user.getOrgUserId(), role.getName()));
MDC.remove(EPCommonSystemProperties.AUDITLOG_BEGIN_TIMESTAMP);
MDC.remove(EPCommonSystemProperties.AUDITLOG_END_TIMESTAMP);
MDC.remove(SystemProperties.MDC_TIMER);
} else {
if(saveRoleResult.getDetailMessage().contains("406")){
response.setStatus(HttpServletResponse.SC_NOT_ACCEPTABLE);
return new PortalRestResponse<>(PortalRestStatusEnum.ERROR,
"Failed to create a role for '" + role.getName() + "'. Any one of the following characters '%,(),=,:,comma, and double quotes' are not allowed"
, "Failed");
} else{
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
return new PortalRestResponse<>(PortalRestStatusEnum.ERROR,
"Failed to saveRole for '" + role.getName() + "'", "Failed");
}
}
} catch (Exception e) {
if (e.getMessage().contains(INVALID_UEB_KEY)) {
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
}else {
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
logger.error(EELFLoggerDelegate.errorLogger, "saveRole failed", e);
return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, e.getMessage(), "Failed");
}
return new PortalRestResponse<String>(PortalRestStatusEnum.OK, "Successfully Saved", "Success");
}
@ApiOperation(value = "Deletes role for an application.", response = PortalRestResponse.class, responseContainer = "Json")
@RequestMapping(value = { "/deleteRole/{code}" }, method = RequestMethod.DELETE, produces = "application/json")
public PortalRestResponse<String> deleteRole(HttpServletRequest request, HttpServletResponse response,
@PathVariable String code) throws Exception {
try {
fieldsValidation(request);
boolean deleteResponse = externalAccessRolesService.deleteRoleForApplication(code,
request.getHeader(UEBKEY));
if (deleteResponse) {
EPUser user = externalAccessRolesService.getUser(request.getHeader(LOGIN_ID)).get(0);
EPApp app = externalAccessRolesService.getApp(request.getHeader(UEBKEY)).get(0);
logger.info(EELFLoggerDelegate.applicationLogger, "deleteRole: succeeded for app {}, role {}",
app.getId(), code);
AuditLog auditLog = new AuditLog();
auditLog.setUserId(user.getId());
auditLog.setActivityCode(EcompAuditLog.CD_ACTIVITY_EXTERNAL_AUTH_DELETE_ROLE);
auditLog.setComments(EcompPortalUtils.truncateString(
"Deleted role for app:" + app.getId() + " and role:'" + code + "'",
PortalConstants.AUDIT_LOG_COMMENT_SIZE));
auditLog.setAffectedRecordId(user.getOrgUserId());
auditService.logActivity(auditLog, null);
MDC.put(EPCommonSystemProperties.AUDITLOG_BEGIN_TIMESTAMP, EPEELFLoggerAdvice.getCurrentDateTimeUTC());
MDC.put(EPCommonSystemProperties.AUDITLOG_END_TIMESTAMP, EPEELFLoggerAdvice.getCurrentDateTimeUTC());
EcompPortalUtils.calculateDateTimeDifferenceForLog(
MDC.get(EPCommonSystemProperties.AUDITLOG_BEGIN_TIMESTAMP),
MDC.get(EPCommonSystemProperties.AUDITLOG_END_TIMESTAMP));
logger.info(EELFLoggerDelegate.auditLogger,
EPLogUtil.formatAuditLogMessage("ExternalAccessRolesController.deleteRole",
EcompAuditLog.CD_ACTIVITY_EXTERNAL_AUTH_DELETE_ROLE, String.valueOf(user.getId()),
user.getOrgUserId(), code));
MDC.remove(EPCommonSystemProperties.AUDITLOG_BEGIN_TIMESTAMP);
MDC.remove(EPCommonSystemProperties.AUDITLOG_END_TIMESTAMP);
MDC.remove(SystemProperties.MDC_TIMER);
} else {
logger.error(EELFLoggerDelegate.errorLogger, "deleteRole failed");
return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR,
"Failed to deleteRole for '" + code + "'", "Failed");
}
} catch (Exception e) {
if (e.getMessage().contains(INVALID_UEB_KEY)) {
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
} else {
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
}
logger.error(EELFLoggerDelegate.errorLogger, "deleteRole failed", e);
return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, e.getMessage(), "Failed");
}
return new PortalRestResponse<String>(PortalRestStatusEnum.OK, SUCCESSFULLY_DELETED, "Success");
}
@ApiOperation(value = "Gets active roles for an application.", response = CentralRole.class, responseContainer = "Json")
@RequestMapping(value = { "/activeRoles" }, method = RequestMethod.GET, produces = "application/json")
public List<CentralRole> getActiveRoles(HttpServletRequest request, HttpServletResponse response) throws Exception {
List<CentralRole> roles = null;
try {
fieldsValidation(request);
List<CentralV2Role> cenRoles= externalAccessRolesService.getActiveRoles(request.getHeader(UEBKEY));
roles = externalAccessRolesService.convertV2CentralRoleListToOldVerisonCentralRoleList(cenRoles);
} catch (Exception e) {
sendErrorResponse(response, e);
logger.error(EELFLoggerDelegate.errorLogger, "getActiveRoles failed", e);
}
return roles;
}
@ApiOperation(value = "Gets active roles for an application.", response = CentralV2Role.class, responseContainer = "Json")
@RequestMapping(value = { "/v1/activeRoles" }, method = RequestMethod.GET, produces = "application/json")
public List<CentralV2Role> getV2ActiveRoles(HttpServletRequest request, HttpServletResponse response) throws Exception {
List<CentralV2Role> cenRole = null;
try {
fieldsValidation(request);
cenRole = externalAccessRolesService.getActiveRoles(request.getHeader(UEBKEY));
} catch (Exception e) {
sendErrorResponse(response, e);
logger.error(EELFLoggerDelegate.errorLogger, "getV2ActiveRoles failed", e);
}
return cenRole;
}
@ApiOperation(value = "deletes user roles for an application.", response = PortalRestResponse.class, responseContainer = "Json")
@RequestMapping(value = { "/deleteDependcyRoleRecord/{roleId}" }, method = RequestMethod.DELETE, produces = "application/json")
public PortalRestResponse<String> deleteDependencyRoleRecord(HttpServletRequest request, HttpServletResponse response,
@PathVariable("roleId") Long roleId) throws Exception {
ExternalRequestFieldsValidator removeResult = null;
try {
fieldsValidation(request);
removeResult = externalAccessRolesService.deleteDependencyRoleRecord(roleId,
request.getHeader(UEBKEY), request.getHeader(LOGIN_ID));
if (!removeResult.isResult()) {
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, "Failed to deleteDependencyRoleRecord",
"Failed");
}
} catch (Exception e) {
if (e.getMessage().contains(INVALID_UEB_KEY)) {
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
} else {
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
logger.error(EELFLoggerDelegate.errorLogger, "deleteDependencyRoleRecord failed", e);
return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, e.getMessage(), "Failed");
}
return new PortalRestResponse<>(PortalRestStatusEnum.OK, SUCCESSFULLY_DELETED, "Success");
}
@ApiOperation(value = "deletes roles for an application.", response = PortalRestResponse.class, responseContainer = "Json")
@RequestMapping(value = { "/v2/deleteRole/{roleId}" }, method = RequestMethod.DELETE, produces = "application/json")
public PortalRestResponse<String> deleteRole(HttpServletRequest request, HttpServletResponse response,
@PathVariable("roleId") Long roleId) throws Exception {
ExternalRequestFieldsValidator removeResult = null;
try {
fieldsValidation(request);
removeResult = externalAccessRolesService.deleteDependencyRoleRecord(roleId,
request.getHeader(UEBKEY), request.getHeader(LOGIN_ID));
if (!removeResult.isResult()) {
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, "Failed to deleteRole",
"Failed");
}
} catch (Exception e) {
if (e.getMessage().contains(INVALID_UEB_KEY)) {
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
} else {
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
logger.error(EELFLoggerDelegate.errorLogger, "deleteRole failed", e);
return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, e.getMessage(), "Failed");
}
return new PortalRestResponse<>(PortalRestStatusEnum.OK, SUCCESSFULLY_DELETED, "Success");
}
@ApiOperation(value = "Bulk upload functions for an application.", response = PortalRestResponse.class, responseContainer = "Json")
@RequestMapping(value = { "/upload/portal/functions" }, method = RequestMethod.POST, produces = "application/json")
public PortalRestResponse<String> bulkUploadFunctions(HttpServletRequest request, HttpServletResponse response) throws Exception {
Integer result = 0;
try {
result = externalAccessRolesService.bulkUploadFunctions(request.getHeader(UEBKEY));
} catch (Exception e) {
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
logger.error(EELFLoggerDelegate.errorLogger, "bulkUploadFunctions failed", e);
return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "Failed to bulkUploadFunctions", "Failed");
}
return new PortalRestResponse<String>(PortalRestStatusEnum.OK, "Successfully added: "+result, "Success");
}
@ApiOperation(value = "Bulk upload roles for an application.", response = PortalRestResponse.class, responseContainer = "Json")
@RequestMapping(value = { "/upload/portal/roles" }, method = RequestMethod.POST, produces = "application/json")
public PortalRestResponse<String> bulkUploadRoles(HttpServletRequest request, HttpServletResponse response) throws Exception {
Integer result = 0;
try {
result = externalAccessRolesService.bulkUploadRoles(request.getHeader(UEBKEY));
} catch (Exception e) {
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
logger.error(EELFLoggerDelegate.errorLogger, "bulkUploadRoles failed", e);
return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "Failed to bulkUploadRoles", "Failed");
}
return new PortalRestResponse<String>(PortalRestStatusEnum.OK, "Successfully added: "+result, "Success");
}
@ApiOperation(value = "Bulk upload role functions for an application.", response = PortalRestResponse.class, responseContainer = "Json")
@RequestMapping(value = { "/upload/portal/roleFunctions" }, method = RequestMethod.POST, produces = "application/json")
public PortalRestResponse<String> bulkUploadRoleFunctions(HttpServletRequest request, HttpServletResponse response) throws Exception {
Integer result = 0;
try {
result = externalAccessRolesService.bulkUploadRolesFunctions(request.getHeader(UEBKEY));
} catch (Exception e) {
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
logger.error(EELFLoggerDelegate.errorLogger, "bulkUploadRoleFunctions failed", e);
return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "Failed to bulkUploadRoleFunctions", "Failed");
}
return new PortalRestResponse<String>(PortalRestStatusEnum.OK, "Successfully added: "+result, "Success");
}
@ApiOperation(value = "Bulk upload user roles for an application.", response = PortalRestResponse.class, responseContainer = "Json")
@RequestMapping(value = { "/upload/portal/userRoles" }, method = RequestMethod.POST, produces = "application/json")
public PortalRestResponse<String> bulkUploadUserRoles(HttpServletRequest request, HttpServletResponse response) throws Exception {
Integer result = 0;
try {
result = externalAccessRolesService.bulkUploadUserRoles(request.getHeader(UEBKEY));
} catch (Exception e) {
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
logger.error(EELFLoggerDelegate.errorLogger, "bulkUploadUserRoles failed", e);
return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "Failed to bulkUploadUserRoles", "Failed");
}
return new PortalRestResponse<String>(PortalRestStatusEnum.OK, "Successfully added: "+result, "Success");
}
@ApiOperation(value = "Bulk upload users for renamed role of an application.", response = PortalRestResponse.class, responseContainer = "Json")
@RequestMapping(value = { "/upload/portal/userRole/{roleId}" }, method = RequestMethod.POST, produces = "application/json")
public PortalRestResponse<String> bulkUploadUsersSingleRole(HttpServletRequest request, HttpServletResponse response, @PathVariable Long roleId) throws Exception {
Integer result = 0;
try {
String roleName = request.getHeader("RoleName");
result = externalAccessRolesService.bulkUploadUsersSingleRole(request.getHeader(UEBKEY), roleId, roleName);
} catch (Exception e) {
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
logger.error(EELFLoggerDelegate.errorLogger, "bulkUploadUsersSingleRole failed", e);
return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "Failed to bulkUploadUsersSingleRole", "Failed");
}
return new PortalRestResponse<String>(PortalRestStatusEnum.OK, "Successfully added: "+result, "Success");
}
@ApiOperation(value = "Bulk upload functions for an partner application.", response = PortalRestResponse.class, responseContainer = "Json")
@RequestMapping(value = { "/upload/partner/functions" }, method = RequestMethod.POST, produces = "application/json")
public PortalRestResponse<String> bulkUploadPartnerFunctions(HttpServletRequest request, HttpServletResponse response) throws Exception {
Integer addedFunctions = 0;
try {
addedFunctions = externalAccessRolesService.bulkUploadPartnerFunctions(request.getHeader(UEBKEY));
} catch (Exception e) {
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
logger.error(EELFLoggerDelegate.errorLogger, "bulkUploadFunctions failed", e);
return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "Failed to bulkUploadFunctions", "Failed");
}
return new PortalRestResponse<String>(PortalRestStatusEnum.OK, "Successfully added: '"+addedFunctions+"' functions", "Success");
}
@ApiOperation(value = "Bulk upload roles for an partner application.", response = PortalRestResponse.class, responseContainer = "Json")
@RequestMapping(value = { "/upload/partner/roles" }, method = RequestMethod.POST, produces = "application/json")
public PortalRestResponse<String> bulkUploadPartnerRoles(HttpServletRequest request, HttpServletResponse response, @RequestBody List<Role> upload) throws Exception {
try {
externalAccessRolesService.bulkUploadPartnerRoles(request.getHeader(UEBKEY), upload);
} catch (Exception e) {
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
logger.error(EELFLoggerDelegate.errorLogger, "bulkUploadRoles failed", e);
return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "Failed to bulkUploadRoles", "Failed");
}
return new PortalRestResponse<String>(PortalRestStatusEnum.OK, "Successfully added", "Success");
}
@ApiOperation(value = "Bulk upload role functions for an partner application.", response = PortalRestResponse.class, responseContainer = "Json")
@RequestMapping(value = { "/upload/partner/roleFunctions" }, method = RequestMethod.POST, produces = "application/json")
public PortalRestResponse<String> bulkUploadPartnerRoleFunctions(HttpServletRequest request, HttpServletResponse response) throws Exception {
Integer addedRoleFunctions = 0;
try {
addedRoleFunctions = externalAccessRolesService.bulkUploadPartnerRoleFunctions(request.getHeader(UEBKEY));
} catch (Exception e) {
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
logger.error(EELFLoggerDelegate.errorLogger, "bulkUploadPartnerRoleFunctions failed", e);
return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "Failed to bulkUploadPartnerRoleFunctions", "Failed");
}
return new PortalRestResponse<String>(PortalRestStatusEnum.OK, "Successfully added: '"+addedRoleFunctions + "' role functions", "Success");
}
@ApiOperation(value = "Gets all functions along with global functions", response = List.class, responseContainer = "Json")
@RequestMapping(value = { "/menuFunctions" }, method = RequestMethod.GET, produces = "application/json")
public List<String> getMenuFunctions(HttpServletRequest request, HttpServletResponse response) throws Exception {
List<String> functionsList = null;
try {
fieldsValidation(request);
functionsList = externalAccessRolesService.getMenuFunctionsList(request.getHeader(UEBKEY));
} catch (Exception e) {
sendErrorResponse(response, e);
logger.error(EELFLoggerDelegate.errorLogger, "getMenuFunctions failed", e);
}
return functionsList;
}
@ApiOperation(value = "Gets all active Users of application", response = String.class, responseContainer = "Json")
@RequestMapping(value = { "/users" }, method = RequestMethod.GET, produces = "application/json")
public List<EcompUser> getUsersOfApplication(HttpServletRequest request, HttpServletResponse response) throws Exception {
List<EcompUser> users = null;
try {
fieldsValidation(request);
users = externalAccessRolesService.getAllAppUsers(request.getHeader(UEBKEY));
} catch (Exception e) {
sendErrorResponse(response, e);
logger.error(EELFLoggerDelegate.errorLogger, "getUsersOfApplication failed", e);
}
return users;
}
/**
*
* It checks the input uebkey of the application and throws exception if it is invalid key
*
* @param request
* @throws Exception
*/
private void fieldsValidation(HttpServletRequest request) throws Exception{
List<EPApp> app = externalAccessRolesService.getApp(request.getHeader(UEBKEY));
if(app.isEmpty()){
throw new Exception(INVALID_UEB_KEY);
}
if(!app.isEmpty() && app.get(0).getCentralAuth()){
ResponseEntity<String> response = externalAccessRolesService.getNameSpaceIfExists(app.get(0));
if (response.getStatusCode().value() == HttpServletResponse.SC_NOT_FOUND)
throw new Exception("Invalid NameSpace");
}
}
/**
*
* It returns http response with appropriate message
*
* @param response
* @param e
* @throws IOException
*/
private void sendErrorResponse(HttpServletResponse response, Exception e) throws IOException {
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
final Map<String,String> uebkeyResponse = new HashMap<>();
ObjectMapper mapper = new ObjectMapper();
String reason = "";
if (e.getMessage().contains(INVALID_UEB_KEY)) {
uebkeyResponse.put("error",INVALID_UEB_KEY);
reason = mapper.writeValueAsString(uebkeyResponse);
response.getWriter().write(reason);
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
} else {
uebkeyResponse.put("error",e.getMessage());
reason = mapper.writeValueAsString(uebkeyResponse);
response.getWriter().write(reason);
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
}
}
@ApiOperation(value = "Gets ecompUser of an application.", response = CentralUser.class, responseContainer = "List")
@RequestMapping(value = { "/v2/user/{loginId}" }, method = RequestMethod.GET, produces = "application/json")
public String getEcompUser(HttpServletRequest request, HttpServletResponse response,
@PathVariable("loginId") String loginId) throws Exception {
EcompUser user = new EcompUser();
ObjectMapper mapper = new ObjectMapper();
String answer = null;
try {
fieldsValidation(request);
answer = externalAccessRolesService.getV2UserWithRoles(loginId, request.getHeader(UEBKEY));
if (answer != null) {
User ecompUser = userservice.userMapper(answer);
user = UserUtils.convertToEcompUser(ecompUser);
List<EcompRole> missingRolesOfUser = externalAccessRolesService.missingUserApplicationRoles(request.getHeader(UEBKEY), loginId, user.getRoles());
if (missingRolesOfUser.size() > 0) {
Set<EcompRole> roles = new TreeSet<EcompRole>(missingRolesOfUser);
user.getRoles().addAll(roles);
}
}
} catch (Exception e) {
sendErrorResponse(response, e);
logger.error(EELFLoggerDelegate.errorLogger, "getEcompUser failed", e);
}
return mapper.writeValueAsString(user);
}
@ApiOperation(value = "Gets user ecomp role for an application.", response = CentralUser.class, responseContainer = "List")
@RequestMapping(value = { "/v2/roles" }, method = RequestMethod.GET, produces = "application/json")
public List<EcompRole> getEcompRolesOfApplication(HttpServletRequest request, HttpServletResponse response)
throws Exception {
List<EcompRole> ecompRoles = null;
ObjectMapper mapper = new ObjectMapper();
List<CentralV2Role> cenRole = null;
try {
fieldsValidation(request);
EPApp app = externalAccessRolesService.getApp(request.getHeader(UEBKEY)).get(0);
// Sync all roles from external system into ONAP portal DB
logger.debug(EELFLoggerDelegate.debugLogger,
"getRolesForApp: Entering into syncApplicationRolesWithEcompDB");
externalAccessRolesService.syncApplicationRolesWithEcompDB(app);
logger.debug(EELFLoggerDelegate.debugLogger, "getRolesForApp: Finished syncApplicationRolesWithEcompDB");
cenRole = externalAccessRolesService.getActiveRoles(request.getHeader(UEBKEY));
} catch (Exception e) {
sendErrorResponse(response, e);
logger.error(EELFLoggerDelegate.errorLogger, "getActiveRoles failed", e);
}
if (cenRole != null) {
String res = mapper.writeValueAsString(cenRole);
ecompRoles = new ArrayList<>();
List<Role> roles = mapper.readValue(res,
TypeFactory.defaultInstance().constructCollectionType(List.class, Role.class));
for (Role role : roles)
ecompRoles.add(UserUtils.convertToEcompRole(role));
logger.debug(EELFLoggerDelegate.debugLogger, "Request completed for getEcompRolesOfApplication");
}
return ecompRoles;
}
}
|