8a7dd19a350b0caf37b3b09a1c6a3a0f659e70c5
[portal.git] / ecomp-portal-BE-common / src / main / java / org / openecomp / portalapp / portal / service / BasicAuthenticationCredentialServiceImpl.java
1 /*-
2  * ================================================================================
3  * ECOMP Portal
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ================================================================================
19  */
20 package org.openecomp.portalapp.portal.service;
21
22 import java.util.ArrayList;
23 import java.util.List;
24
25 import org.hibernate.criterion.Criterion;
26 import org.hibernate.criterion.Restrictions;
27 import org.openecomp.portalapp.portal.domain.BasicAuthCredentials;
28 import org.openecomp.portalapp.portal.domain.EPEndpoint;
29 import org.openecomp.portalapp.portal.domain.EPEndpointAccount;
30 import org.openecomp.portalapp.portal.logging.aop.EPMetricsLog;
31 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
32 import org.openecomp.portalsdk.core.service.DataAccessService;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.context.annotation.EnableAspectJAutoProxy;
35 import org.springframework.stereotype.Service;
36
37 @Service("basicAuthenticationCredentialService")
38 @EnableAspectJAutoProxy
39 @EPMetricsLog
40 public class BasicAuthenticationCredentialServiceImpl implements BasicAuthenticationCredentialService {
41
42         private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(BasicAuthenticationCredentialServiceImpl.class);
43
44         @Autowired
45         private DataAccessService dataAccessService;
46
47         @Override
48         public BasicAuthCredentials getBasicAuthCredentialByUsernameAndPassword(String username, String password) {
49
50                 List<Criterion> restrictionsList = new ArrayList<Criterion>();
51                 Criterion contextUserNameCrit = Restrictions.eq("username", username);
52                 restrictionsList.add(contextUserNameCrit);
53                 Criterion contextPasswordCrit = Restrictions.eq("password", password);
54                 restrictionsList.add(contextPasswordCrit);
55
56                 @SuppressWarnings("unchecked")
57                 List<BasicAuthCredentials> credList = (List<BasicAuthCredentials>) dataAccessService
58                                 .getList(BasicAuthCredentials.class, null, restrictionsList, null);
59                 if (credList == null || credList.size() == 0) {
60                         logger.error(EELFLoggerDelegate.errorLogger,
61                                         "getBasicAuthCredentialByAppName: no credential(s) for " + username);
62                         return null;
63                 }
64                 logger.debug(EELFLoggerDelegate.debugLogger,
65                                 "getBasicAuthCredentialByAppName: cred list size: " + credList.size());
66                 BasicAuthCredentials cred = (BasicAuthCredentials) credList.get(0);
67                 cred.setEndpoints(getEndpointsByAccountId(cred.getId()));
68                 return cred;
69         }
70
71         private List<EPEndpoint> getEndpointsByAccountId(long id) {
72                 List<EPEndpoint> list = new ArrayList<>();
73                 List<Criterion> restrictionsList = new ArrayList<Criterion>();
74                 Criterion contextIdCrit = Restrictions.eq("account_id", id);
75                 restrictionsList.add(contextIdCrit);
76                 @SuppressWarnings("unchecked")
77                 List<EPEndpointAccount> epList = (List<EPEndpointAccount>) dataAccessService.getList(EPEndpointAccount.class,
78                                 null, restrictionsList, null);
79                 for (EPEndpointAccount ep : epList) {
80                         list.add((EPEndpoint) dataAccessService.getDomainObject(EPEndpoint.class, ep.getEp_id(), null));
81                 }
82                 return list;
83         }
84
85 }