2  * ================================================================================
\r 
   4  * ================================================================================
\r 
   5  * Copyright (C) 2017 AT&T Intellectual Property
\r 
   6  * ================================================================================
\r 
   7  * Licensed under the Apache License, Version 2.0 (the "License");
\r 
   8  * you may not use this file except in compliance with the License.
\r 
   9  * You may obtain a copy of the License at
\r 
  11  *      http://www.apache.org/licenses/LICENSE-2.0
\r 
  13  * Unless required by applicable law or agreed to in writing, software
\r 
  14  * distributed under the License is distributed on an "AS IS" BASIS,
\r 
  15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r 
  16  * See the License for the specific language governing permissions and
\r 
  17  * limitations under the License.
\r 
  18  * ================================================================================
\r 
  20 package org.openecomp.portalapp.portal.service;
\r 
  22 import java.util.ArrayList;
\r 
  23 import java.util.HashMap;
\r 
  24 import java.util.List;
\r 
  25 import java.util.Map;
\r 
  27 import org.hibernate.Session;
\r 
  28 import org.hibernate.SessionFactory;
\r 
  29 import org.hibernate.Transaction;
\r 
  30 import org.hibernate.criterion.Criterion;
\r 
  31 import org.hibernate.criterion.Restrictions;
\r 
  32 import org.openecomp.portalapp.portal.domain.BasicAuthCredentials;
\r 
  33 import org.openecomp.portalapp.portal.domain.EPUserAppRolesRequestDetail;
\r 
  34 import org.openecomp.portalapp.portal.domain.MicroserviceData;
\r 
  35 import org.openecomp.portalapp.portal.domain.MicroserviceParameter;
\r 
  36 import org.openecomp.portalapp.portal.domain.WidgetCatalog;
\r 
  37 import org.openecomp.portalapp.portal.domain.WidgetServiceHeaders;
\r 
  38 import org.openecomp.portalapp.portal.ecomp.model.PortalRestResponse;
\r 
  39 import org.openecomp.portalapp.portal.logging.aop.EPMetricsLog;
\r 
  40 import org.openecomp.portalapp.portal.transport.CommonWidgetMeta;
\r 
  41 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
\r 
  42 import org.openecomp.portalsdk.core.onboarding.util.CipherUtil;
\r 
  43 import org.openecomp.portalsdk.core.service.DataAccessService;
\r 
  44 import org.openecomp.portalsdk.core.util.SystemProperties;
\r 
  45 import org.springframework.beans.factory.annotation.Autowired;
\r 
  46 import org.springframework.context.annotation.EnableAspectJAutoProxy;
\r 
  47 import org.springframework.core.ParameterizedTypeReference;
\r 
  48 import org.springframework.http.HttpEntity;
\r 
  49 import org.springframework.http.HttpMethod;
\r 
  50 import org.springframework.http.ResponseEntity;
\r 
  51 import org.springframework.stereotype.Service;
\r 
  52 import org.springframework.web.client.RestTemplate;
\r 
  54 @Service("microserviceService")
\r 
  55 @EnableAspectJAutoProxy
\r 
  57 public class MicroserviceServiceImpl implements MicroserviceService {
\r 
  59         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MicroserviceServiceImpl.class);
\r 
  63         private DataAccessService dataAccessService;
\r 
  66         private SessionFactory sessionFactory;
\r 
  68         public Long saveMicroservice(MicroserviceData newService) throws Exception {
\r 
  69                 if (newService.getPassword() != null)
\r 
  70                         newService.setPassword(encryptedPassword(newService.getPassword()));
\r 
  71                 getDataAccessService().saveDomainObject(newService, null);
\r 
  72                 return newService.getId();
\r 
  75         public void saveServiceParameters(long serviceId, List<MicroserviceParameter> list) throws Exception {
\r 
  76                 for (int i = 0; i < list.size(); i++) {
\r 
  77                         MicroserviceParameter para = list.get(i);
\r 
  78                         para.setServiceId(serviceId);
\r 
  79                         getDataAccessService().saveDomainObject(para, null);
\r 
  84         public MicroserviceData getMicroserviceDataById(long id) {
\r 
  85                 MicroserviceData data = null;
\r 
  87                         data = (MicroserviceData) dataAccessService
\r 
  88                                         .getList(MicroserviceData.class, " where id = '" + id + "'", null, null).get(0);
\r 
  89                         data.setParameterList(getServiceParameters(id));
\r 
  90                 } catch (Exception e) {
\r 
  91                         logger.error(EELFLoggerDelegate.errorLogger, "getMicroserviceDataById failed", e);
\r 
  97         @SuppressWarnings("unchecked")
\r 
  99         public List<MicroserviceData> getMicroserviceData() throws Exception {
\r 
 100                 List<MicroserviceData> list = (List<MicroserviceData>) dataAccessService.getList(MicroserviceData.class, null);
\r 
 101                 for (int i = 0; i < list.size(); i++) {
\r 
 102                         if (list.get(i).getPassword() != null)
\r 
 103                                 list.get(i).setPassword(decryptedPassword(list.get(i).getPassword()));
\r 
 104                         list.get(i).setParameterList(getServiceParameters(list.get(i).getId()));
\r 
 109         @SuppressWarnings("unchecked")
\r 
 110         private List<MicroserviceParameter> getServiceParameters(long serviceId) {
\r 
 111                 List<MicroserviceParameter> list = (List<MicroserviceParameter>) dataAccessService
\r 
 112                                 .getList(MicroserviceParameter.class, " where service_id = '" + serviceId + "'", null, null);
\r 
 117         public void deleteMicroservice(long serviceId) throws Exception {
\r 
 120                         Map<String, String> params = new HashMap<String, String>();
\r 
 121                         params.put("serviceId", Long.toString(serviceId));
\r 
 123                         dataAccessService.executeNamedQuery("deleteMicroserviceParameter", params, null);
\r 
 124                         dataAccessService.executeNamedQuery("deleteMicroservice", params, null);
\r 
 126                 } catch (Exception e) {
\r 
 127                         e.printStackTrace();
\r 
 128                         logger.error(EELFLoggerDelegate.errorLogger, "deleteMicroservice failed", e);
\r 
 133         @SuppressWarnings("unchecked")
\r 
 135         public void updateMicroservice(long serviceId, MicroserviceData newService) throws Exception {
\r 
 137                         newService.setId(serviceId);
\r 
 138                         if (newService.getPassword() != null)
\r 
 139                                 newService.setPassword(encryptedPassword(newService.getPassword()));
\r 
 140                         getDataAccessService().saveDomainObject(newService, null);
\r 
 141                         List<MicroserviceParameter> oldService = getServiceParameters(serviceId);
\r 
 142                         boolean foundParam;
\r 
 143                         for (int i = 0; i < oldService.size(); i++) {
\r 
 144                                 foundParam = false;
\r 
 145                                 for (int n = 0; n < newService.getParameterList().size(); n++) {
\r 
 146                                         if (newService.getParameterList().get(n).getId() == oldService.get(i).getId()) {
\r 
 151                                 if (foundParam == false) {
\r 
 152                                         MicroserviceParameter pd = oldService.get(i);
\r 
 153                                         Session localSession = sessionFactory.openSession();
\r 
 154                                         localSession.delete(pd);
\r 
 155                                         localSession.flush();
\r 
 156                                         localSession.clear();
\r 
 160                 } catch (Exception e) {
\r 
 161                         logger.error(EELFLoggerDelegate.errorLogger, "updateMicroservice failed", e);
\r 
 164                 saveServiceParameters(serviceId, newService.getParameterList());
\r 
 168         @SuppressWarnings("unchecked")
\r 
 169         public List<MicroserviceParameter> getParametersById(long serviceId) {
\r 
 170                 List<Criterion> restrictionsList = new ArrayList<Criterion>();
\r 
 171                 Criterion contextIdCrit = Restrictions.eq("serviceId", serviceId);
\r 
 172                 restrictionsList.add(contextIdCrit);
\r 
 173                 List<MicroserviceParameter> list = (List<MicroserviceParameter>) dataAccessService
\r 
 174                                 .getList(MicroserviceParameter.class, null, restrictionsList, null);
\r 
 175                 logger.debug(EELFLoggerDelegate.debugLogger,
\r 
 176                                 "getParametersById: microservice parameters list size: " + list.size());
\r 
 180         private String decryptedPassword(String encryptedPwd) throws Exception {
\r 
 181                 String result = "";
\r 
 182                 if (encryptedPwd != null & encryptedPwd.length() > 0) {
\r 
 184                                 result = CipherUtil.decrypt(encryptedPwd,
\r 
 185                                                 SystemProperties.getProperty(SystemProperties.Decryption_Key));
\r 
 186                         } catch (Exception e) {
\r 
 187                                 logger.error(EELFLoggerDelegate.errorLogger, "decryptedPassword failed", e);
\r 
 194         private String encryptedPassword(String decryptedPwd) throws Exception {
\r 
 195                 String result = "";
\r 
 196                 if (decryptedPwd != null & decryptedPwd.length() > 0) {
\r 
 198                                 result = CipherUtil.encrypt(decryptedPwd,
\r 
 199                                                 SystemProperties.getProperty(SystemProperties.Decryption_Key));
\r 
 200                         } catch (Exception e) {
\r 
 201                                 logger.error(EELFLoggerDelegate.errorLogger, "encryptedPassword failed", e);
\r 
 208         public DataAccessService getDataAccessService() {
\r 
 209                 return dataAccessService;
\r