1c353d0711dca0218d1ad480fb5207d790dcd854
[portal.git] / ecomp-portal-BE-common / src / main / java / org / openecomp / portalapp / portal / service / BasicAuthAccountServiceImpl.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright © 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38 package org.openecomp.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.openecomp.portalapp.portal.domain.BasicAuthCredentials;
48 import org.openecomp.portalapp.portal.domain.EPEndpoint;
49 import org.openecomp.portalapp.portal.domain.EPEndpointAccount;
50 import org.openecomp.portalapp.portal.logging.aop.EPMetricsLog;
51 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
52 import org.openecomp.portalsdk.core.onboarding.util.CipherUtil;
53 import org.openecomp.portalsdk.core.service.DataAccessService;
54 import org.openecomp.portalsdk.core.util.SystemProperties;
55 import org.springframework.beans.factory.annotation.Autowired;
56 import org.springframework.context.annotation.EnableAspectJAutoProxy;
57 import org.springframework.stereotype.Service;
58
59 @Service("basicAuthAccountService")
60 @EnableAspectJAutoProxy
61 @EPMetricsLog
62 public class BasicAuthAccountServiceImpl implements BasicAuthAccountService{
63         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MicroserviceServiceImpl.class);
64
65         @Autowired
66         private DataAccessService dataAccessService;
67
68         @Override
69         public Long saveBasicAuthAccount(BasicAuthCredentials newCredential) throws Exception {
70                 if (newCredential.getPassword() != null)
71                         newCredential.setPassword(encryptedPassword(newCredential.getPassword()));
72                 try{
73                         getDataAccessService().saveDomainObject(newCredential, null);
74                 }catch(Exception e){
75                         logger.error(EELFLoggerDelegate.errorLogger, "saveBasicAuthAccount() failed", e);
76                         throw e;
77                 }
78                 return newCredential.getId();
79         }
80         
81
82         @Override
83         @SuppressWarnings("unchecked")
84         public Long saveEndpoints(EPEndpoint endpoint) throws Exception {
85                 
86                 List<Criterion> restrictionsList = new ArrayList<Criterion>();
87                 Criterion NameCrit = Restrictions.eq("name", endpoint.getName());
88                 restrictionsList.add(NameCrit);
89
90                 List<EPEndpoint> tempList = (List<EPEndpoint>) dataAccessService.getList(EPEndpoint.class, null,
91                                 restrictionsList, null);
92                 if (tempList.size() != 0) {
93                         return tempList.get(0).getId();
94                 } else {
95                         getDataAccessService().saveDomainObject(endpoint, null);
96                         return endpoint.getId();
97                 }
98                 
99         }
100         
101         @Override
102         public void saveEndpointAccount(Long accountId, Long endpointId) throws Exception {
103                 EPEndpointAccount record = new EPEndpointAccount();
104                 record.setAccount_id(accountId);
105                 record.setEp_id(endpointId);
106                 try {
107                         getDataAccessService().saveDomainObject(record, null);
108                 } catch (Exception e) {
109                         logger.error(EELFLoggerDelegate.errorLogger, "saveEndpointAccount() failed", e);
110                         throw e;
111                 }
112
113         }
114         
115         @Override
116         @SuppressWarnings("unchecked")
117         public void updateBasicAuthAccount(Long accountId, BasicAuthCredentials newCredential) throws Exception {
118                 try {
119                         newCredential.setId(accountId);
120                         if (newCredential.getPassword() != null)
121                                 newCredential.setPassword(encryptedPassword(newCredential.getPassword()));
122                         getDataAccessService().saveDomainObject(newCredential, null);
123                         
124                         List<EPEndpoint> endpoints = newCredential.getEndpoints();
125                         List<EPEndpoint> orig_points = getEPEndpoints(accountId);
126                         
127                         for(EPEndpoint temp_ep: orig_points){
128                                 boolean flag = false;
129                                 for(EPEndpoint temp_ep2: endpoints){
130                                         if(temp_ep2.getId() == temp_ep.getId())
131                                                 flag = true;
132                                 }
133                                 if(!flag){
134                                         Map<String, String> params = new HashMap<String, String>();
135                                         params.put("accountId", Long.toString(accountId));
136                                         params.put("epId", Long.toString(temp_ep.getId()));
137                                         dataAccessService.executeNamedQuery("deleteAccountEndpointRecord", params, null);
138                                 }
139                         }
140                         
141                         
142                         for(int i = 0; i < endpoints.size(); i++){
143                                 
144                                 List<Criterion> restrictionsList = new ArrayList<Criterion>();
145                                 Criterion IdCrit = Restrictions.eq("id", endpoints.get(i).getId());
146                                 restrictionsList.add(IdCrit);
147                                 Criterion NameCrit = Restrictions.eq("name", endpoints.get(i).getName());
148                                 restrictionsList.add(NameCrit);
149                                 List<EPEndpoint> tempList = (List<EPEndpoint>) dataAccessService
150                                                 .getList(EPEndpoint.class, null, restrictionsList, null);
151                                 if(tempList.size() == 0){
152                                         if(endpoints.get(i).getId() != null){
153                                                 //delete the record endpoints.get(i).getId(), accountId                                         
154                                                 Map<String, String> params = new HashMap<String, String>();
155                                                 params.put("accountId", Long.toString(accountId));
156                                                 params.put("epId", Long.toString(endpoints.get(i).getId()));
157                                                 dataAccessService.executeNamedQuery("deleteAccountEndpointRecord", params, null);
158                                                 endpoints.get(i).setId(null);
159                                         }
160                                         //create a new endpoint
161                                         Long ep_id = saveEndpoints(endpoints.get(i));
162                                         saveEndpointAccount(accountId, ep_id);                                           
163                                 }
164                         }
165                 } catch (Exception e) {
166                         logger.error(EELFLoggerDelegate.errorLogger, "updateBasicAuthAccount() failed", e);
167                         throw e;
168                 }
169         }
170
171         @Override
172         public List<BasicAuthCredentials> getAccountData() throws Exception {
173                 @SuppressWarnings("unchecked")
174                 List<BasicAuthCredentials> list = (List<BasicAuthCredentials>) dataAccessService.getList(BasicAuthCredentials.class, null);
175                 for (int i = 0; i < list.size(); i++) {
176                         if (list.get(i).getPassword() != null)
177                                 list.get(i).setPassword(decryptedPassword(list.get(i).getPassword()));
178                         list.get(i).setEndpoints(getEPEndpoints(list.get(i).getId()));
179                 }
180                 return list;
181         }
182         
183         @SuppressWarnings("unchecked")
184         private List<EPEndpoint> getEPEndpoints(long accountId) {
185                 List<EPEndpoint> result = new ArrayList<>();
186                 List<EPEndpointAccount> list = (List<EPEndpointAccount>) dataAccessService
187                                 .getList(EPEndpointAccount.class, " where account_id = '" + accountId + "'", null, null);
188                 for(int i = 0; i < list.size(); i++){
189                         result.add((EPEndpoint) dataAccessService.getDomainObject(EPEndpoint.class, list.get(i).getEp_id(), null));
190                 }
191                 return result;
192         }
193         
194         @Override
195         public void deleteEndpointAccout(Long accountId) throws Exception {
196                 try{
197                         Map<String, String> params = new HashMap<String, String>();
198                         params.put("accountId", Long.toString(accountId));
199                         
200                         dataAccessService.executeNamedQuery("deleteAccountEndpoint", params, null);
201                         dataAccessService.executeNamedQuery("deleteBasicAuthAccount", params, null);
202                         
203                 }catch(Exception e){
204                         logger.error(EELFLoggerDelegate.errorLogger, "deleteEndpointAccout() failed", e);
205                         throw e;
206                 }
207         }
208         
209         private String decryptedPassword(String encryptedPwd) throws Exception {
210                 String result = "";
211                 if (encryptedPwd != null & encryptedPwd.length() > 0) {
212                         try {
213                                 result = CipherUtil.decrypt(encryptedPwd,
214                                                 SystemProperties.getProperty(SystemProperties.Decryption_Key));
215                         } catch (Exception e) {
216                                 logger.error(EELFLoggerDelegate.errorLogger, "decryptedPassword() failed", e);
217                                 throw e;
218                         }
219                 }
220                 return result;
221         }
222
223         private String encryptedPassword(String decryptedPwd) throws Exception {
224                 String result = "";
225                 if (decryptedPwd != null & decryptedPwd.length() > 0) {
226                         try {
227                                 result = CipherUtil.encrypt(decryptedPwd,
228                                                 SystemProperties.getProperty(SystemProperties.Decryption_Key));
229                         } catch (Exception e) {
230                                 logger.error(EELFLoggerDelegate.errorLogger, "encryptedPassword() failed", e);
231                                 throw e;
232                         }
233                 }
234                 return result;
235         }
236         
237         public DataAccessService getDataAccessService() {
238                 return dataAccessService;
239         }
240 }