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