74cf1726da52d0cbeda6275fe48c9c04ae3d1841
[portal.git] / ecomp-portal-BE-common / src / main / java / org / onap / portalapp / portal / service / BasicAuthAccountServiceImpl.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
8  * Unless otherwise specified, all software contained herein is licensed
9  * under the Apache License, Version 2.0 (the "License");
10  * you may not use this software except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * Unless otherwise specified, all documentation contained herein is licensed
22  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23  * you may not use this documentation except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
28  * Unless required by applicable law or agreed to in writing, documentation
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  *
34  * ============LICENSE_END============================================
35  *
36  * 
37  */
38 package org.onap.portalapp.portal.service;
39
40 import java.util.ArrayList;
41 import java.util.HashMap;
42 import java.util.List;
43 import java.util.Map;
44
45 import org.hibernate.criterion.Criterion;
46 import org.hibernate.criterion.Restrictions;
47 import org.onap.portalapp.portal.domain.BasicAuthCredentials;
48 import org.onap.portalapp.portal.domain.EPEndpoint;
49 import org.onap.portalapp.portal.domain.EPEndpointAccount;
50 import org.onap.portalapp.portal.logging.aop.EPMetricsLog;
51 import org.onap.portalapp.portal.utils.EPCommonSystemProperties;
52 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
53 import org.onap.portalsdk.core.onboarding.util.CipherUtil;
54 import org.onap.portalsdk.core.service.DataAccessService;
55 import org.onap.portalsdk.core.util.SystemProperties;
56 import org.springframework.beans.factory.annotation.Autowired;
57 import org.springframework.context.annotation.EnableAspectJAutoProxy;
58 import org.springframework.stereotype.Service;
59
60 @Service("basicAuthAccountService")
61 @EnableAspectJAutoProxy
62 @EPMetricsLog
63 public class BasicAuthAccountServiceImpl implements BasicAuthAccountService{
64         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MicroserviceServiceImpl.class);
65
66         @Autowired
67         private DataAccessService dataAccessService;
68
69         @Override
70         public Long saveBasicAuthAccount(BasicAuthCredentials newCredential) throws Exception {
71                 if (newCredential.getPassword() != null)
72                         newCredential.setPassword(encryptedPassword(newCredential.getPassword()));
73                 try{
74                         getDataAccessService().saveDomainObject(newCredential, null);
75                 }catch(Exception e){
76                         logger.error(EELFLoggerDelegate.errorLogger, "saveBasicAuthAccount() failed", e);
77                         throw e;
78                 }
79                 return newCredential.getId();
80         }
81         
82
83         @Override
84         @SuppressWarnings("unchecked")
85         public Long saveEndpoints(EPEndpoint endpoint) throws Exception {
86                 
87                 List<Criterion> restrictionsList = new ArrayList<Criterion>();
88                 Criterion NameCrit = Restrictions.eq("name", endpoint.getName());
89                 restrictionsList.add(NameCrit);
90
91                 List<EPEndpoint> tempList = (List<EPEndpoint>) dataAccessService.getList(EPEndpoint.class, null,
92                                 restrictionsList, null);
93                 if (tempList.size() != 0) {
94                         return tempList.get(0).getId();
95                 } else {
96                         getDataAccessService().saveDomainObject(endpoint, null);
97                         return endpoint.getId();
98                 }
99                 
100         }
101         
102         @Override
103         public void saveEndpointAccount(Long accountId, Long endpointId) throws Exception {
104                 EPEndpointAccount record = new EPEndpointAccount();
105                 record.setAccount_id(accountId);
106                 record.setEp_id(endpointId);
107                 try {
108                         getDataAccessService().saveDomainObject(record, null);
109                 } catch (Exception e) {
110                         logger.error(EELFLoggerDelegate.errorLogger, "saveEndpointAccount() failed", e);
111                         throw e;
112                 }
113
114         }
115         
116         @Override
117         @SuppressWarnings("unchecked")
118         public void updateBasicAuthAccount(Long accountId, BasicAuthCredentials newCredential) throws Exception {
119                 try {
120                         newCredential.setId(accountId);
121                         if (newCredential.getPassword() != null){
122                                 if(newCredential.getPassword().equals(EPCommonSystemProperties.APP_DISPLAY_PASSWORD)){
123                                         BasicAuthCredentials oldMS = getBasicAuthCredentialsById(accountId);
124                                         newCredential.setPassword(oldMS.getPassword()); // keep the old password
125                                 }else
126                                         newCredential.setPassword(encryptedPassword(newCredential.getPassword())); //new password
127                         }
128                         getDataAccessService().saveDomainObject(newCredential, null);
129                         
130                         List<EPEndpoint> endpoints = newCredential.getEndpoints();
131                         List<EPEndpoint> orig_points = getEPEndpoints(accountId);
132                         
133                         for(EPEndpoint temp_ep: orig_points){
134                                 boolean flag = false;
135                                 for(EPEndpoint temp_ep2: endpoints){
136                                         if(temp_ep2.getId() == temp_ep.getId())
137                                                 flag = true;
138                                 }
139                                 if(!flag){
140                                         Map<String, String> params = new HashMap<String, String>();
141                                         params.put("accountId", Long.toString(accountId));
142                                         params.put("epId", Long.toString(temp_ep.getId()));
143                                         dataAccessService.executeNamedQuery("deleteAccountEndpointRecord", params, null);
144                                 }
145                         }
146                         
147                         
148                         for(int i = 0; i < endpoints.size(); i++){
149                                 
150                                 List<Criterion> restrictionsList = new ArrayList<Criterion>();
151                                 Criterion IdCrit = Restrictions.eq("id", endpoints.get(i).getId());
152                                 restrictionsList.add(IdCrit);
153                                 Criterion NameCrit = Restrictions.eq("name", endpoints.get(i).getName());
154                                 restrictionsList.add(NameCrit);
155                                 List<EPEndpoint> tempList = (List<EPEndpoint>) dataAccessService
156                                                 .getList(EPEndpoint.class, null, restrictionsList, null);
157                                 if(tempList.size() == 0){
158                                         if(endpoints.get(i).getId() != null){
159                                                 //delete the record endpoints.get(i).getId(), accountId                                         
160                                                 Map<String, String> params = new HashMap<String, String>();
161                                                 params.put("accountId", Long.toString(accountId));
162                                                 params.put("epId", Long.toString(endpoints.get(i).getId()));
163                                                 dataAccessService.executeNamedQuery("deleteAccountEndpointRecord", params, null);
164                                                 endpoints.get(i).setId(null);
165                                         }
166                                         //create a new endpoint
167                                         Long ep_id = saveEndpoints(endpoints.get(i));
168                                         saveEndpointAccount(accountId, ep_id);                                           
169                                 }
170                         }
171                 } catch (Exception e) {
172                         logger.error(EELFLoggerDelegate.errorLogger, "updateBasicAuthAccount() failed", e);
173                         throw e;
174                 }
175         }
176
177         @Override
178         public List<BasicAuthCredentials> getAccountData() throws Exception {
179                 @SuppressWarnings("unchecked")
180                 List<BasicAuthCredentials> list = (List<BasicAuthCredentials>) dataAccessService.getList(BasicAuthCredentials.class, null);
181                 for (int i = 0; i < list.size(); i++) {
182                         if (list.get(i).getPassword() != null)
183                                 list.get(i).setPassword(EPCommonSystemProperties.APP_DISPLAY_PASSWORD);
184                         list.get(i).setEndpoints(getEPEndpoints(list.get(i).getId()));
185                 }
186                 return list;
187         }
188         
189         @SuppressWarnings("unchecked")
190         private List<EPEndpoint> getEPEndpoints(long accountId) {
191                 List<EPEndpoint> result = new ArrayList<>();
192                 List<EPEndpointAccount> list = null;
193                 Map<String, Long> params = new HashMap<>();
194                 params.put("account_id", accountId);
195                 try {
196                         list = this.getDataAccessService().executeNamedQuery("getEPEndpointAccountByAccountId", params, null);
197                 } catch (Exception e) {
198                         logger.error(EELFLoggerDelegate.errorLogger, "getEPEndpointAccountByAccountId failed", e);                      
199                 }
200                 
201                 for(int i = 0; list != null && i < list.size(); i++){
202                         result.add((EPEndpoint) dataAccessService.getDomainObject(EPEndpoint.class, list.get(i).getEp_id(), null));
203                 }
204                 return result;
205         }
206         
207         @Override
208         public void deleteEndpointAccout(Long accountId) throws Exception {
209                 try{
210                         Map<String, String> params = new HashMap<String, String>();
211                         params.put("accountId", Long.toString(accountId));
212                         
213                         dataAccessService.executeNamedQuery("deleteAccountEndpoint", params, null);
214                         dataAccessService.executeNamedQuery("deleteBasicAuthAccount", params, null);
215                         
216                 }catch(Exception e){
217                         logger.error(EELFLoggerDelegate.errorLogger, "deleteEndpointAccout() failed", e);
218                         throw e;
219                 }
220         }
221         
222         private String decryptedPassword(String encryptedPwd) throws Exception {
223                 String result = "";
224                 if (encryptedPwd != null && encryptedPwd.length() > 0) {
225                         try {
226                                 result = CipherUtil.decryptPKC(encryptedPwd,
227                                                 SystemProperties.getProperty(SystemProperties.Decryption_Key));
228                         } catch (Exception e) {
229                                 logger.error(EELFLoggerDelegate.errorLogger, "decryptedPassword() failed", e);
230                                 throw e;
231                         }
232                 }
233                 return result;
234         }
235
236         private String encryptedPassword(String decryptedPwd) throws Exception {
237                 String result = "";
238                 if (decryptedPwd != null && decryptedPwd.length() > 0) {
239                         try {
240                                 result = CipherUtil.encryptPKC(decryptedPwd,
241                                                 SystemProperties.getProperty(SystemProperties.Decryption_Key));
242                         } catch (Exception e) {
243                                 logger.error(EELFLoggerDelegate.errorLogger, "encryptedPassword() failed", e);
244                                 throw e;
245                         }
246                 }
247                 return result;
248         }
249         
250         public DataAccessService getDataAccessService() {
251                 return dataAccessService;
252         }
253         
254         @Override
255         public BasicAuthCredentials getBasicAuthCredentialsById(long id) throws Exception {
256                 try {
257                         @SuppressWarnings("unchecked")
258                         List<BasicAuthCredentials> list = (List<BasicAuthCredentials>) dataAccessService
259                                         .getList(BasicAuthCredentials.class, null);
260                         for (BasicAuthCredentials auth : list) {
261                                 if (auth != null && auth.getId() == id)
262                                         return auth;
263                         }
264                 } catch (Exception e) {
265                         logger.error(EELFLoggerDelegate.errorLogger, "getBasicAuthCredentialsDataById failed", e);
266                         throw e;
267                 }
268                 return null;
269
270         }
271 }