1 package org.openecomp.portalapp.portal.service;
 
   3 import java.util.ArrayList;
 
   4 import java.util.HashMap;
 
   8 import org.hibernate.Session;
 
   9 import org.hibernate.criterion.Criterion;
 
  10 import org.hibernate.criterion.Restrictions;
 
  11 import org.openecomp.portalapp.portal.domain.BasicAuthCredentials;
 
  12 import org.openecomp.portalapp.portal.domain.EPEndpoint;
 
  13 import org.openecomp.portalapp.portal.domain.EPEndpointAccount;
 
  14 import org.openecomp.portalapp.portal.domain.MicroserviceData;
 
  15 import org.openecomp.portalapp.portal.domain.MicroserviceParameter;
 
  16 import org.openecomp.portalapp.portal.logging.aop.EPMetricsLog;
 
  17 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
 
  18 import org.openecomp.portalsdk.core.onboarding.util.CipherUtil;
 
  19 import org.openecomp.portalsdk.core.service.DataAccessService;
 
  20 import org.openecomp.portalsdk.core.util.SystemProperties;
 
  21 import org.springframework.beans.factory.annotation.Autowired;
 
  22 import org.springframework.context.annotation.EnableAspectJAutoProxy;
 
  23 import org.springframework.stereotype.Service;
 
  25 @Service("basicAuthAccountService")
 
  26 @EnableAspectJAutoProxy
 
  28 public class BasicAuthAccountServiceImpl implements BasicAuthAccountService{
 
  29         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MicroserviceServiceImpl.class);
 
  32         private DataAccessService dataAccessService;
 
  35         public Long saveBasicAuthAccount(BasicAuthCredentials newCredential) throws Exception {
 
  36                 if (newCredential.getPassword() != null)
 
  37                         newCredential.setPassword(encryptedPassword(newCredential.getPassword()));
 
  39                         getDataAccessService().saveDomainObject(newCredential, null);
 
  41                         logger.error(EELFLoggerDelegate.errorLogger, "saveBasicAuthAccount() failed", e);
 
  44                 return newCredential.getId();
 
  49         @SuppressWarnings("unchecked")
 
  50         public Long saveEndpoints(EPEndpoint endpoint) throws Exception {
 
  52                 List<Criterion> restrictionsList = new ArrayList<Criterion>();
 
  53                 Criterion NameCrit = Restrictions.eq("name", endpoint.getName());
 
  54                 restrictionsList.add(NameCrit);
 
  56                 List<EPEndpoint> tempList = (List<EPEndpoint>) dataAccessService.getList(EPEndpoint.class, null,
 
  57                                 restrictionsList, null);
 
  58                 if (tempList.size() != 0) {
 
  59                         return tempList.get(0).getId();
 
  61                         getDataAccessService().saveDomainObject(endpoint, null);
 
  62                         return endpoint.getId();
 
  68         public void saveEndpointAccount(Long accountId, Long endpointId) throws Exception {
 
  69                 EPEndpointAccount record = new EPEndpointAccount();
 
  70                 record.setAccount_id(accountId);
 
  71                 record.setEp_id(endpointId);
 
  73                         getDataAccessService().saveDomainObject(record, null);
 
  74                 } catch (Exception e) {
 
  75                         logger.error(EELFLoggerDelegate.errorLogger, "saveEndpointAccount() failed", e);
 
  82         @SuppressWarnings("unchecked")
 
  83         public void updateBasicAuthAccount(Long accountId, BasicAuthCredentials newCredential) throws Exception {
 
  85                         newCredential.setId(accountId);
 
  86                         if (newCredential.getPassword() != null)
 
  87                                 newCredential.setPassword(encryptedPassword(newCredential.getPassword()));
 
  88                         getDataAccessService().saveDomainObject(newCredential, null);
 
  90                         List<EPEndpoint> endpoints = newCredential.getEndpoints();
 
  91                         List<EPEndpoint> orig_points = getEPEndpoints(accountId);
 
  93                         for(EPEndpoint temp_ep: orig_points){
 
  95                                 for(EPEndpoint temp_ep2: endpoints){
 
  96                                         if(temp_ep2.getId() == temp_ep.getId())
 
 100                                         Map<String, String> params = new HashMap<String, String>();
 
 101                                         params.put("accountId", Long.toString(accountId));
 
 102                                         params.put("epId", Long.toString(temp_ep.getId()));
 
 103                                         dataAccessService.executeNamedQuery("deleteAccountEndpointRecord", params, null);
 
 108                         for(int i = 0; i < endpoints.size(); i++){
 
 110                                 List<Criterion> restrictionsList = new ArrayList<Criterion>();
 
 111                                 Criterion IdCrit = Restrictions.eq("id", endpoints.get(i).getId());
 
 112                                 restrictionsList.add(IdCrit);
 
 113                                 Criterion NameCrit = Restrictions.eq("name", endpoints.get(i).getName());
 
 114                                 restrictionsList.add(NameCrit);
 
 115                                 List<EPEndpoint> tempList = (List<EPEndpoint>) dataAccessService
 
 116                                                 .getList(EPEndpoint.class, null, restrictionsList, null);
 
 117                                 if(tempList.size() == 0){
 
 118                                         if(endpoints.get(i).getId() != null){
 
 119                                                 //delete the record endpoints.get(i).getId(), accountId                                         
 
 120                                                 Map<String, String> params = new HashMap<String, String>();
 
 121                                                 params.put("accountId", Long.toString(accountId));
 
 122                                                 params.put("epId", Long.toString(endpoints.get(i).getId()));
 
 123                                                 dataAccessService.executeNamedQuery("deleteAccountEndpointRecord", params, null);
 
 124                                                 endpoints.get(i).setId(null);
 
 126                                         //create a new endpoint
 
 127                                         Long ep_id = saveEndpoints(endpoints.get(i));
 
 128                                         saveEndpointAccount(accountId, ep_id);                                           
 
 131                 } catch (Exception e) {
 
 132                         logger.error(EELFLoggerDelegate.errorLogger, "updateBasicAuthAccount() failed", e);
 
 138         public List<BasicAuthCredentials> getAccountData() throws Exception {
 
 139                 List<BasicAuthCredentials> list = (List<BasicAuthCredentials>) dataAccessService.getList(BasicAuthCredentials.class, null);
 
 140                 for (int i = 0; i < list.size(); i++) {
 
 141                         if (list.get(i).getPassword() != null)
 
 142                                 list.get(i).setPassword(decryptedPassword(list.get(i).getPassword()));
 
 143                         list.get(i).setEndpoints(getEPEndpoints(list.get(i).getId()));
 
 148         @SuppressWarnings("unchecked")
 
 149         private List<EPEndpoint> getEPEndpoints(long accountId) {
 
 150                 List<EPEndpoint> result = new ArrayList<>();
 
 151                 List<EPEndpointAccount> list = (List<EPEndpointAccount>) dataAccessService
 
 152                                 .getList(EPEndpointAccount.class, " where account_id = '" + accountId + "'", null, null);
 
 153                 for(int i = 0; i < list.size(); i++){
 
 154                         result.add((EPEndpoint) dataAccessService.getDomainObject(EPEndpoint.class, list.get(i).getEp_id(), null));
 
 160         public void deleteEndpointAccout(Long accountId) throws Exception {
 
 162                         Map<String, String> params = new HashMap<String, String>();
 
 163                         params.put("accountId", Long.toString(accountId));
 
 165                         dataAccessService.executeNamedQuery("deleteAccountEndpoint", params, null);
 
 166                         dataAccessService.executeNamedQuery("deleteBasicAuthAccount", params, null);
 
 169                         logger.error(EELFLoggerDelegate.errorLogger, "deleteEndpointAccout() failed", e);
 
 174         private String decryptedPassword(String encryptedPwd) throws Exception {
 
 176                 if (encryptedPwd != null & encryptedPwd.length() > 0) {
 
 178                                 result = CipherUtil.decrypt(encryptedPwd,
 
 179                                                 SystemProperties.getProperty(SystemProperties.Decryption_Key));
 
 180                         } catch (Exception e) {
 
 181                                 logger.error(EELFLoggerDelegate.errorLogger, "decryptedPassword() failed", e);
 
 188         private String encryptedPassword(String decryptedPwd) throws Exception {
 
 190                 if (decryptedPwd != null & decryptedPwd.length() > 0) {
 
 192                                 result = CipherUtil.encrypt(decryptedPwd,
 
 193                                                 SystemProperties.getProperty(SystemProperties.Decryption_Key));
 
 194                         } catch (Exception e) {
 
 195                                 logger.error(EELFLoggerDelegate.errorLogger, "encryptedPassword() failed", e);
 
 202         public DataAccessService getDataAccessService() {
 
 203                 return dataAccessService;