Security/ Package Name changes
[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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
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.portalsdk.core.logging.logic.EELFLoggerDelegate;
52 import org.onap.portalsdk.core.onboarding.util.CipherUtil;
53 import org.onap.portalsdk.core.service.DataAccessService;
54 import org.onap.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 = null;
187                 Map<String, Long> params = new HashMap<>();
188                 params.put("account_id", accountId);
189                 try {
190                         list = this.getDataAccessService().executeNamedQuery("getEPEndpointAccountByAccountId", params, null);
191                 } catch (Exception e) {
192                         logger.error(EELFLoggerDelegate.errorLogger, "getEPEndpointAccountByAccountId failed", e);                      
193                 }
194                 
195                 for(int i = 0; list != null && i < list.size(); i++){
196                         result.add((EPEndpoint) dataAccessService.getDomainObject(EPEndpoint.class, list.get(i).getEp_id(), null));
197                 }
198                 return result;
199         }
200         
201         @Override
202         public void deleteEndpointAccout(Long accountId) throws Exception {
203                 try{
204                         Map<String, String> params = new HashMap<String, String>();
205                         params.put("accountId", Long.toString(accountId));
206                         
207                         dataAccessService.executeNamedQuery("deleteAccountEndpoint", params, null);
208                         dataAccessService.executeNamedQuery("deleteBasicAuthAccount", params, null);
209                         
210                 }catch(Exception e){
211                         logger.error(EELFLoggerDelegate.errorLogger, "deleteEndpointAccout() failed", e);
212                         throw e;
213                 }
214         }
215         
216         private String decryptedPassword(String encryptedPwd) throws Exception {
217                 String result = "";
218                 if (encryptedPwd != null & encryptedPwd.length() > 0) {
219                         try {
220                                 result = CipherUtil.decryptPKC(encryptedPwd,
221                                                 SystemProperties.getProperty(SystemProperties.Decryption_Key));
222                         } catch (Exception e) {
223                                 logger.error(EELFLoggerDelegate.errorLogger, "decryptedPassword() failed", e);
224                                 throw e;
225                         }
226                 }
227                 return result;
228         }
229
230         private String encryptedPassword(String decryptedPwd) throws Exception {
231                 String result = "";
232                 if (decryptedPwd != null & decryptedPwd.length() > 0) {
233                         try {
234                                 result = CipherUtil.encryptPKC(decryptedPwd,
235                                                 SystemProperties.getProperty(SystemProperties.Decryption_Key));
236                         } catch (Exception e) {
237                                 logger.error(EELFLoggerDelegate.errorLogger, "encryptedPassword() failed", e);
238                                 throw e;
239                         }
240                 }
241                 return result;
242         }
243         
244         public DataAccessService getDataAccessService() {
245                 return dataAccessService;
246         }
247 }