[PORTAL-7] Rebase
[portal.git] / ecomp-portal-BE-common / src / main / java / org / openecomp / portalapp / portal / service / BasicAuthAccountServiceImpl.java
1 package org.openecomp.portalapp.portal.service;
2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.List;
6 import java.util.Map;
7
8 import org.hibernate.Session;
9 import org.hibernate.criterion.Criterion;
10 import org.hibernate.criterion.Restrictions;
11 import org.openecomp.portalapp.portal.domain.BasicAuthCredentials;
12 import org.openecomp.portalapp.portal.domain.EPEndpoint;
13 import org.openecomp.portalapp.portal.domain.EPEndpointAccount;
14 import org.openecomp.portalapp.portal.domain.MicroserviceData;
15 import org.openecomp.portalapp.portal.domain.MicroserviceParameter;
16 import org.openecomp.portalapp.portal.logging.aop.EPMetricsLog;
17 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
18 import org.openecomp.portalsdk.core.onboarding.util.CipherUtil;
19 import org.openecomp.portalsdk.core.service.DataAccessService;
20 import org.openecomp.portalsdk.core.util.SystemProperties;
21 import org.springframework.beans.factory.annotation.Autowired;
22 import org.springframework.context.annotation.EnableAspectJAutoProxy;
23 import org.springframework.stereotype.Service;
24
25 @Service("basicAuthAccountService")
26 @EnableAspectJAutoProxy
27 @EPMetricsLog
28 public class BasicAuthAccountServiceImpl implements BasicAuthAccountService{
29         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MicroserviceServiceImpl.class);
30
31         @Autowired
32         private DataAccessService dataAccessService;
33
34         @Override
35         public Long saveBasicAuthAccount(BasicAuthCredentials newCredential) throws Exception {
36                 if (newCredential.getPassword() != null)
37                         newCredential.setPassword(encryptedPassword(newCredential.getPassword()));
38                 try{
39                         getDataAccessService().saveDomainObject(newCredential, null);
40                 }catch(Exception e){
41                         logger.error(EELFLoggerDelegate.errorLogger, "saveBasicAuthAccount() failed", e);
42                         throw e;
43                 }
44                 return newCredential.getId();
45         }
46         
47
48         @Override
49         @SuppressWarnings("unchecked")
50         public Long saveEndpoints(EPEndpoint endpoint) throws Exception {
51                 
52                 List<Criterion> restrictionsList = new ArrayList<Criterion>();
53                 Criterion NameCrit = Restrictions.eq("name", endpoint.getName());
54                 restrictionsList.add(NameCrit);
55
56                 List<EPEndpoint> tempList = (List<EPEndpoint>) dataAccessService.getList(EPEndpoint.class, null,
57                                 restrictionsList, null);
58                 if (tempList.size() != 0) {
59                         return tempList.get(0).getId();
60                 } else {
61                         getDataAccessService().saveDomainObject(endpoint, null);
62                         return endpoint.getId();
63                 }
64                 
65         }
66         
67         @Override
68         public void saveEndpointAccount(Long accountId, Long endpointId) throws Exception {
69                 EPEndpointAccount record = new EPEndpointAccount();
70                 record.setAccount_id(accountId);
71                 record.setEp_id(endpointId);
72                 try {
73                         getDataAccessService().saveDomainObject(record, null);
74                 } catch (Exception e) {
75                         logger.error(EELFLoggerDelegate.errorLogger, "saveEndpointAccount() failed", e);
76                         throw e;
77                 }
78
79         }
80         
81         @Override
82         @SuppressWarnings("unchecked")
83         public void updateBasicAuthAccount(Long accountId, BasicAuthCredentials newCredential) throws Exception {
84                 try {
85                         newCredential.setId(accountId);
86                         if (newCredential.getPassword() != null)
87                                 newCredential.setPassword(encryptedPassword(newCredential.getPassword()));
88                         getDataAccessService().saveDomainObject(newCredential, null);
89                         
90                         List<EPEndpoint> endpoints = newCredential.getEndpoints();
91                         List<EPEndpoint> orig_points = getEPEndpoints(accountId);
92                         
93                         for(EPEndpoint temp_ep: orig_points){
94                                 boolean flag = false;
95                                 for(EPEndpoint temp_ep2: endpoints){
96                                         if(temp_ep2.getId() == temp_ep.getId())
97                                                 flag = true;
98                                 }
99                                 if(!flag){
100                                         Map<String, String> params = new HashMap<String, String>();
101                                         params.put("accountId", Long.toString(accountId));
102                                         params.put("epId", Long.toString(temp_ep.getId()));
103                                         dataAccessService.executeNamedQuery("deleteAccountEndpointRecord", params, null);
104                                 }
105                         }
106                         
107                         
108                         for(int i = 0; i < endpoints.size(); i++){
109                                 
110                                 List<Criterion> restrictionsList = new ArrayList<Criterion>();
111                                 Criterion IdCrit = Restrictions.eq("id", endpoints.get(i).getId());
112                                 restrictionsList.add(IdCrit);
113                                 Criterion NameCrit = Restrictions.eq("name", endpoints.get(i).getName());
114                                 restrictionsList.add(NameCrit);
115                                 List<EPEndpoint> tempList = (List<EPEndpoint>) dataAccessService
116                                                 .getList(EPEndpoint.class, null, restrictionsList, null);
117                                 if(tempList.size() == 0){
118                                         if(endpoints.get(i).getId() != null){
119                                                 //delete the record endpoints.get(i).getId(), accountId                                         
120                                                 Map<String, String> params = new HashMap<String, String>();
121                                                 params.put("accountId", Long.toString(accountId));
122                                                 params.put("epId", Long.toString(endpoints.get(i).getId()));
123                                                 dataAccessService.executeNamedQuery("deleteAccountEndpointRecord", params, null);
124                                                 endpoints.get(i).setId(null);
125                                         }
126                                         //create a new endpoint
127                                         Long ep_id = saveEndpoints(endpoints.get(i));
128                                         saveEndpointAccount(accountId, ep_id);                                           
129                                 }
130                         }
131                 } catch (Exception e) {
132                         logger.error(EELFLoggerDelegate.errorLogger, "updateBasicAuthAccount() failed", e);
133                         throw e;
134                 }
135         }
136
137         @Override
138         public List<BasicAuthCredentials> getAccountData() throws Exception {
139                 List<BasicAuthCredentials> list = (List<BasicAuthCredentials>) dataAccessService.getList(BasicAuthCredentials.class, null);
140                 for (int i = 0; i < list.size(); i++) {
141                         if (list.get(i).getPassword() != null)
142                                 list.get(i).setPassword(decryptedPassword(list.get(i).getPassword()));
143                         list.get(i).setEndpoints(getEPEndpoints(list.get(i).getId()));
144                 }
145                 return list;
146         }
147         
148         @SuppressWarnings("unchecked")
149         private List<EPEndpoint> getEPEndpoints(long accountId) {
150                 List<EPEndpoint> result = new ArrayList<>();
151                 List<EPEndpointAccount> list = (List<EPEndpointAccount>) dataAccessService
152                                 .getList(EPEndpointAccount.class, " where account_id = '" + accountId + "'", null, null);
153                 for(int i = 0; i < list.size(); i++){
154                         result.add((EPEndpoint) dataAccessService.getDomainObject(EPEndpoint.class, list.get(i).getEp_id(), null));
155                 }
156                 return result;
157         }
158         
159         @Override
160         public void deleteEndpointAccout(Long accountId) throws Exception {
161                 try{
162                         Map<String, String> params = new HashMap<String, String>();
163                         params.put("accountId", Long.toString(accountId));
164                         
165                         dataAccessService.executeNamedQuery("deleteAccountEndpoint", params, null);
166                         dataAccessService.executeNamedQuery("deleteBasicAuthAccount", params, null);
167                         
168                 }catch(Exception e){
169                         logger.error(EELFLoggerDelegate.errorLogger, "deleteEndpointAccout() failed", e);
170                         throw e;
171                 }
172         }
173         
174         private String decryptedPassword(String encryptedPwd) throws Exception {
175                 String result = "";
176                 if (encryptedPwd != null & encryptedPwd.length() > 0) {
177                         try {
178                                 result = CipherUtil.decrypt(encryptedPwd,
179                                                 SystemProperties.getProperty(SystemProperties.Decryption_Key));
180                         } catch (Exception e) {
181                                 logger.error(EELFLoggerDelegate.errorLogger, "decryptedPassword() failed", e);
182                                 throw e;
183                         }
184                 }
185                 return result;
186         }
187
188         private String encryptedPassword(String decryptedPwd) throws Exception {
189                 String result = "";
190                 if (decryptedPwd != null & decryptedPwd.length() > 0) {
191                         try {
192                                 result = CipherUtil.encrypt(decryptedPwd,
193                                                 SystemProperties.getProperty(SystemProperties.Decryption_Key));
194                         } catch (Exception e) {
195                                 logger.error(EELFLoggerDelegate.errorLogger, "encryptedPassword() failed", e);
196                                 throw e;
197                         }
198                 }
199                 return result;
200         }
201         
202         public DataAccessService getDataAccessService() {
203                 return dataAccessService;
204         }
205 }