1 package org.onap.portal.service.microservice;
3 import java.util.Optional;
5 import org.onap.portal.domain.db.ep.EpMicroservice;
6 import org.onap.portal.domain.db.ep.EpMicroserviceParameter;
7 import org.onap.portal.service.microserviceParameter.EpMicroserviceParameterService;
8 import org.onap.portal.utils.EPCommonSystemProperties;
9 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
10 import org.onap.portalsdk.core.onboarding.util.CipherUtil;
11 import org.onap.portalsdk.core.util.SystemProperties;
12 import org.springframework.beans.factory.annotation.Autowired;
13 import org.springframework.stereotype.Service;
15 import java.util.List;
18 public class EpMicroserviceService {
20 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(EpMicroserviceService.class);
22 private final EpMicroserviceDao epMicroserviceDao;
23 private final EpMicroserviceParameterService epMicroserviceParameterService;
26 public EpMicroserviceService(EpMicroserviceDao epMicroserviceDao,
27 EpMicroserviceParameterService epMicroserviceParameterService) {
28 this.epMicroserviceDao = epMicroserviceDao;
29 this.epMicroserviceParameterService = epMicroserviceParameterService;
32 public List<EpMicroservice> saveAll(List<EpMicroservice> epMicroservices) {
33 return epMicroserviceDao.saveAll(epMicroservices);
36 public Optional<EpMicroservice> getById(long serviceId) {
37 return epMicroserviceDao.findById(serviceId);
40 public EpMicroservice saveOne(EpMicroservice newServiceData) {
41 return epMicroserviceDao.save(newServiceData);
44 public List<EpMicroservice> getAll() {
45 return epMicroserviceDao.findAll();
48 public void deleteById(long serviceId) {
49 epMicroserviceDao.deleteById(serviceId);
52 @SuppressWarnings("OptionalGetWithoutIsPresent")
53 public void updateMicroservice(long serviceId, EpMicroservice newServiceData) throws Exception {
54 EpMicroservice newService = getById(serviceId).get();
56 newService.setId(serviceId);
57 if (newService.getPassword() != null) {
58 if (newService.getPassword().equals(EPCommonSystemProperties.APP_DISPLAY_PASSWORD)) {
59 EpMicroservice oldMS = getById(serviceId).get();
60 newService.setPassword(oldMS.getPassword()); // keep the old password
62 newService.setPassword(encryptedPassword(newService.getPassword())); //new password
66 List<EpMicroserviceParameter> oldService = epMicroserviceParameterService.getByServiceId(serviceId);
68 for (EpMicroserviceParameter microserviceParameter : oldService) {
70 for (EpMicroserviceParameter service : newService.getEpMicroserviceParameters()) {
71 if (service.getId().equals(microserviceParameter.getId())) {
77 epMicroserviceParameterService.deleteOne(microserviceParameter);
80 for (EpMicroserviceParameter param : newService.getEpMicroserviceParameters()) {
81 param.setServiceId(newService);
82 epMicroserviceParameterService.save(param);
84 } catch (Exception e) {
85 logger.error(EELFLoggerDelegate.errorLogger, "updateMicroservice failed", e);
88 saveServiceParameters(newService.getId(), newService.getEpMicroserviceParameters());
91 public void saveServiceParameters(Long newServiceId, Set<EpMicroserviceParameter> list) {
92 EpMicroservice newService = getById(newServiceId).get();
93 for (EpMicroserviceParameter para : list) {
94 para.setServiceId(newService);
95 epMicroserviceParameterService.save(para);
99 private String encryptedPassword(String decryptedPwd) throws Exception {
101 if (decryptedPwd != null && !decryptedPwd.isEmpty()) {
103 result = CipherUtil.encryptPKC(decryptedPwd,
104 SystemProperties.getProperty(SystemProperties.Decryption_Key));
105 } catch (Exception e) {
106 logger.error(EELFLoggerDelegate.errorLogger, "encryptedPassword failed", e);