[PORTAL-7] Rebase
[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.domain.SharedContext;
31 import org.openecomp.portalapp.portal.logging.aop.EPMetricsLog;
32 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
33 import org.openecomp.portalsdk.core.service.DataAccessService;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.context.annotation.EnableAspectJAutoProxy;
36 import org.springframework.stereotype.Service;
37
38 @Service("basicAuthenticationCredentialService")
39 @EnableAspectJAutoProxy
40 @EPMetricsLog
41 public class BasicAuthenticationCredentialServiceImpl implements BasicAuthenticationCredentialService {
42
43         private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(BasicAuthenticationCredentialServiceImpl.class);
44
45         @Autowired
46         private DataAccessService dataAccessService;
47
48         @Override
49         public BasicAuthCredentials getBasicAuthCredentialByAppName(String appName) {
50         
51                 List<Criterion> restrictionsList = new ArrayList<Criterion>();
52                 Criterion contextIdCrit = Restrictions.eq("applicationName", appName);
53                 restrictionsList.add(contextIdCrit);
54                 @SuppressWarnings("unchecked")
55                 List<BasicAuthCredentials> credList = (List<BasicAuthCredentials>) dataAccessService.getList(BasicAuthCredentials.class, null, restrictionsList, null);
56                 if (credList == null || credList.size() == 0) {
57                         logger.error(EELFLoggerDelegate.errorLogger,
58                                         "getBasicAuthCredentialByAppName: no credential(s) for " + appName);
59                         return null;
60                 }
61                 logger.debug(EELFLoggerDelegate.debugLogger,
62                                 "getBasicAuthCredentialByAppName: cred list size: " + credList.size());
63                 BasicAuthCredentials cred = (BasicAuthCredentials) credList.get(0);
64                 cred.setEndpoints(getEndpointsByAccountId(cred.getId()));
65                 return cred;
66         }
67         
68         private List<EPEndpoint> getEndpointsByAccountId(long id){
69                 
70                 List<EPEndpoint> list = new ArrayList<>();
71                 List<Criterion> restrictionsList = new ArrayList<Criterion>();
72                 Criterion contextIdCrit = Restrictions.eq("account_id", id);
73                 restrictionsList.add(contextIdCrit);
74                 List<EPEndpointAccount> epList = (List<EPEndpointAccount>) dataAccessService.getList(EPEndpointAccount.class, null, restrictionsList, null);
75                 for(EPEndpointAccount ep: epList){
76                         list.add((EPEndpoint) dataAccessService.getDomainObject(EPEndpoint.class, ep.getEp_id(), null));
77                 }
78                 return list;
79         }
80
81 }