2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
26 * https://creativecommons.org/licenses/by/4.0/
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.
34 * ============LICENSE_END============================================
38 package org.onap.portalapp.portal.service;
40 import java.util.ArrayList;
41 import java.util.HashMap;
42 import java.util.List;
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;
59 @Service("basicAuthAccountService")
60 @EnableAspectJAutoProxy
62 public class BasicAuthAccountServiceImpl implements BasicAuthAccountService{
63 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MicroserviceServiceImpl.class);
66 private DataAccessService dataAccessService;
69 public Long saveBasicAuthAccount(BasicAuthCredentials newCredential) throws Exception {
70 if (newCredential.getPassword() != null)
71 newCredential.setPassword(encryptedPassword(newCredential.getPassword()));
73 getDataAccessService().saveDomainObject(newCredential, null);
75 logger.error(EELFLoggerDelegate.errorLogger, "saveBasicAuthAccount() failed", e);
78 return newCredential.getId();
83 @SuppressWarnings("unchecked")
84 public Long saveEndpoints(EPEndpoint endpoint) throws Exception {
86 List<Criterion> restrictionsList = new ArrayList<Criterion>();
87 Criterion NameCrit = Restrictions.eq("name", endpoint.getName());
88 restrictionsList.add(NameCrit);
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();
95 getDataAccessService().saveDomainObject(endpoint, null);
96 return endpoint.getId();
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);
107 getDataAccessService().saveDomainObject(record, null);
108 } catch (Exception e) {
109 logger.error(EELFLoggerDelegate.errorLogger, "saveEndpointAccount() failed", e);
116 @SuppressWarnings("unchecked")
117 public void updateBasicAuthAccount(Long accountId, BasicAuthCredentials newCredential) throws Exception {
119 newCredential.setId(accountId);
120 if (newCredential.getPassword() != null)
121 newCredential.setPassword(encryptedPassword(newCredential.getPassword()));
122 getDataAccessService().saveDomainObject(newCredential, null);
124 List<EPEndpoint> endpoints = newCredential.getEndpoints();
125 List<EPEndpoint> orig_points = getEPEndpoints(accountId);
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())
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);
142 for(int i = 0; i < endpoints.size(); i++){
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);
160 //create a new endpoint
161 Long ep_id = saveEndpoints(endpoints.get(i));
162 saveEndpointAccount(accountId, ep_id);
165 } catch (Exception e) {
166 logger.error(EELFLoggerDelegate.errorLogger, "updateBasicAuthAccount() failed", e);
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()));
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);
190 list = this.getDataAccessService().executeNamedQuery("getEPEndpointAccountByAccountId", params, null);
191 } catch (Exception e) {
192 logger.error(EELFLoggerDelegate.errorLogger, "getEPEndpointAccountByAccountId failed", e);
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));
202 public void deleteEndpointAccout(Long accountId) throws Exception {
204 Map<String, String> params = new HashMap<String, String>();
205 params.put("accountId", Long.toString(accountId));
207 dataAccessService.executeNamedQuery("deleteAccountEndpoint", params, null);
208 dataAccessService.executeNamedQuery("deleteBasicAuthAccount", params, null);
211 logger.error(EELFLoggerDelegate.errorLogger, "deleteEndpointAccout() failed", e);
216 private String decryptedPassword(String encryptedPwd) throws Exception {
218 if (encryptedPwd != null & encryptedPwd.length() > 0) {
220 result = CipherUtil.decryptPKC(encryptedPwd,
221 SystemProperties.getProperty(SystemProperties.Decryption_Key));
222 } catch (Exception e) {
223 logger.error(EELFLoggerDelegate.errorLogger, "decryptedPassword() failed", e);
230 private String encryptedPassword(String decryptedPwd) throws Exception {
232 if (decryptedPwd != null & decryptedPwd.length() > 0) {
234 result = CipherUtil.encryptPKC(decryptedPwd,
235 SystemProperties.getProperty(SystemProperties.Decryption_Key));
236 } catch (Exception e) {
237 logger.error(EELFLoggerDelegate.errorLogger, "encryptedPassword() failed", e);
244 public DataAccessService getDataAccessService() {
245 return dataAccessService;