2  * ================================================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2017 AT&T Intellectual Property
 
   6  * ================================================================================
 
   7  * Licensed under the Apache License, Version 2.0 (the "License");
 
   8  * you may not use this file except in compliance with the License.
 
   9  * You may obtain a copy of the License at
 
  11  *      http://www.apache.org/licenses/LICENSE-2.0
 
  13  * Unless required by applicable law or agreed to in writing, software
 
  14  * distributed under the License is distributed on an "AS IS" BASIS,
 
  15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  16  * See the License for the specific language governing permissions and
 
  17  * limitations under the License.
 
  18  * ================================================================================
 
  20 package org.openecomp.portalapp.portal.service;
 
  22 import java.util.ArrayList;
 
  23 import java.util.HashMap;
 
  24 import java.util.List;
 
  27 import org.hibernate.criterion.Criterion;
 
  28 import org.hibernate.criterion.Restrictions;
 
  29 import org.openecomp.portalapp.portal.domain.MicroserviceData;
 
  30 import org.openecomp.portalapp.portal.domain.MicroserviceParameter;
 
  31 import org.openecomp.portalapp.portal.logging.aop.EPMetricsLog;
 
  32 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
 
  33 import org.openecomp.portalsdk.core.onboarding.util.CipherUtil;
 
  34 import org.openecomp.portalsdk.core.service.DataAccessService;
 
  35 import org.openecomp.portalsdk.core.util.SystemProperties;
 
  36 import org.springframework.beans.factory.annotation.Autowired;
 
  37 import org.springframework.context.annotation.EnableAspectJAutoProxy;
 
  38 import org.springframework.stereotype.Service;
 
  40 @Service("microserviceService")
 
  41 @EnableAspectJAutoProxy
 
  43 public class MicroserviceServiceImpl implements MicroserviceService {
 
  45         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MicroserviceServiceImpl.class);
 
  48         private DataAccessService dataAccessService;
 
  50         public Long saveMicroservice(MicroserviceData newService) throws Exception {
 
  51                 if (newService.getPassword() != null)
 
  52                         newService.setPassword(encryptedPassword(newService.getPassword()));
 
  53                 getDataAccessService().saveDomainObject(newService, null);
 
  54                 return newService.getId();
 
  57         public void saveServiceParameters(long serviceId, List<MicroserviceParameter> list) throws Exception {
 
  58                 for (int i = 0; i < list.size(); i++) {
 
  59                         MicroserviceParameter para = list.get(i);
 
  60                         para.setServiceId(serviceId);
 
  61                         getDataAccessService().saveDomainObject(para, null);
 
  66         public MicroserviceData getMicroserviceDataById(long id) {
 
  67                 MicroserviceData data = null;
 
  69                         data = (MicroserviceData) dataAccessService
 
  70                                         .getList(MicroserviceData.class, " where id = '" + id + "'", null, null).get(0);
 
  71                         data.setParameterList(getServiceParameters(id));
 
  72                 } catch (Exception e) {
 
  73                         logger.error(EELFLoggerDelegate.errorLogger, "getMicroserviceDataById failed", e);
 
  79         @SuppressWarnings("unchecked")
 
  81         public List<MicroserviceData> getMicroserviceData() throws Exception {
 
  82                 List<MicroserviceData> list = (List<MicroserviceData>) dataAccessService.getList(MicroserviceData.class, null);
 
  83                 for (int i = 0; i < list.size(); i++) {
 
  84                         if (list.get(i).getPassword() != null)
 
  85                                 list.get(i).setPassword(decryptedPassword(list.get(i).getPassword()));
 
  86                         list.get(i).setParameterList(getServiceParameters(list.get(i).getId()));
 
  91         @SuppressWarnings("unchecked")
 
  92         private List<MicroserviceParameter> getServiceParameters(long serviceId) {
 
  93                 List<MicroserviceParameter> list = (List<MicroserviceParameter>) dataAccessService
 
  94                                 .getList(MicroserviceParameter.class, " where service_id = '" + serviceId + "'", null, null);
 
  99         public void deleteMicroservice(long serviceId) throws Exception {
 
 102                         Map<String, String> params = new HashMap<String, String>();
 
 103                         params.put("serviceId", Long.toString(serviceId));
 
 105                         dataAccessService.executeNamedQuery("deleteMicroserviceParameter", params, null);
 
 106                         dataAccessService.executeNamedQuery("deleteMicroservice", params, null);
 
 108                 } catch (Exception e) {
 
 110                         logger.error(EELFLoggerDelegate.errorLogger, "deleteMicroservice failed", e);
 
 115         @SuppressWarnings("unchecked")
 
 117         public void updateMicroservice(long serviceId, MicroserviceData newService) throws Exception {
 
 119                         newService.setId(serviceId);
 
 120                         if (newService.getPassword() != null)
 
 121                                 newService.setPassword(encryptedPassword(newService.getPassword()));
 
 122                         getDataAccessService().saveDomainObject(newService, null);
 
 123                         List<MicroserviceParameter> oldService = getServiceParameters(serviceId);
 
 125                         for (int i = 0; i < oldService.size(); i++) {
 
 127                                 for (int n = 0; n < newService.getParameterList().size(); n++) {
 
 128                                         if (newService.getParameterList().get(n).getId().equals(oldService.get(i).getId())) {
 
 133                                 if (foundParam == false) {
 
 134                                         MicroserviceParameter pd = oldService.get(i);
 
 135                                         getDataAccessService().deleteDomainObject(pd, null);
 
 138                         for (int i = 0; i < newService.getParameterList().size(); i++) {
 
 139                                 MicroserviceParameter param = newService.getParameterList().get(i);
 
 140                                 param.setServiceId(serviceId);
 
 141                                 getDataAccessService().saveDomainObject(param, null);
 
 143                 } catch (Exception e) {
 
 144                         logger.error(EELFLoggerDelegate.errorLogger, "updateMicroservice failed", e);
 
 147                 saveServiceParameters(serviceId, newService.getParameterList());
 
 151         @SuppressWarnings("unchecked")
 
 152         public List<MicroserviceParameter> getParametersById(long serviceId) {
 
 153                 List<Criterion> restrictionsList = new ArrayList<Criterion>();
 
 154                 Criterion contextIdCrit = Restrictions.eq("serviceId", serviceId);
 
 155                 restrictionsList.add(contextIdCrit);
 
 156                 List<MicroserviceParameter> list = (List<MicroserviceParameter>) dataAccessService
 
 157                                 .getList(MicroserviceParameter.class, null, restrictionsList, null);
 
 158                 logger.debug(EELFLoggerDelegate.debugLogger,
 
 159                                 "getParametersById: microservice parameters list size: " + list.size());
 
 163         private String decryptedPassword(String encryptedPwd) throws Exception {
 
 165                 if (encryptedPwd != null & encryptedPwd.length() > 0) {
 
 167                                 result = CipherUtil.decrypt(encryptedPwd,
 
 168                                                 SystemProperties.getProperty(SystemProperties.Decryption_Key));
 
 169                         } catch (Exception e) {
 
 170                                 logger.error(EELFLoggerDelegate.errorLogger, "decryptedPassword failed", e);
 
 177         private String encryptedPassword(String decryptedPwd) throws Exception {
 
 179                 if (decryptedPwd != null & decryptedPwd.length() > 0) {
 
 181                                 result = CipherUtil.encrypt(decryptedPwd,
 
 182                                                 SystemProperties.getProperty(SystemProperties.Decryption_Key));
 
 183                         } catch (Exception e) {
 
 184                                 logger.error(EELFLoggerDelegate.errorLogger, "encryptedPassword failed", e);
 
 191         public DataAccessService getDataAccessService() {
 
 192                 return dataAccessService;