MicroserviceController up
[portal.git] / portal-BE / src / main / java / org / onap / portal / service / microservice / EpMicroserviceService.java
1 package org.onap.portal.service.microservice;
2
3 import java.util.Optional;
4 import java.util.Set;
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;
14
15 import java.util.List;
16
17 @Service
18 public class EpMicroserviceService {
19
20     EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(EpMicroserviceService.class);
21
22     private final EpMicroserviceDao epMicroserviceDao;
23     private final EpMicroserviceParameterService epMicroserviceParameterService;
24
25     @Autowired
26     public EpMicroserviceService(EpMicroserviceDao epMicroserviceDao,
27         EpMicroserviceParameterService epMicroserviceParameterService) {
28         this.epMicroserviceDao = epMicroserviceDao;
29         this.epMicroserviceParameterService = epMicroserviceParameterService;
30     }
31
32     public List<EpMicroservice> saveAll(List<EpMicroservice> epMicroservices) {
33         return epMicroserviceDao.saveAll(epMicroservices);
34     }
35
36     public Optional<EpMicroservice> getById(long serviceId) {
37         return epMicroserviceDao.findById(serviceId);
38     }
39
40     public EpMicroservice saveOne(EpMicroservice newServiceData) {
41         return epMicroserviceDao.save(newServiceData);
42     }
43
44     public List<EpMicroservice> getAll() {
45         return epMicroserviceDao.findAll();
46     }
47
48     public void deleteById(long serviceId) {
49         epMicroserviceDao.deleteById(serviceId);
50     }
51
52     @SuppressWarnings("OptionalGetWithoutIsPresent")
53     public void updateMicroservice(long serviceId, EpMicroservice newServiceData) throws Exception {
54         EpMicroservice newService = getById(serviceId).get();
55         try {
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
61                 } else {
62                     newService.setPassword(encryptedPassword(newService.getPassword())); //new password
63                 }
64             }
65             saveOne(newService);
66             List<EpMicroserviceParameter> oldService = epMicroserviceParameterService.getByServiceId(serviceId);
67             boolean foundParam;
68             for (EpMicroserviceParameter microserviceParameter : oldService) {
69                 foundParam = false;
70                 for (EpMicroserviceParameter service : newService.getEpMicroserviceParameters()) {
71                     if (service.getId().equals(microserviceParameter.getId())) {
72                         foundParam = true;
73                         break;
74                     }
75                 }
76                 if (!foundParam) {
77                     epMicroserviceParameterService.deleteOne(microserviceParameter);
78                 }
79             }
80             for (EpMicroserviceParameter param : newService.getEpMicroserviceParameters()) {
81                 param.setServiceId(newService);
82                 epMicroserviceParameterService.save(param);
83             }
84         } catch (Exception e) {
85             logger.error(EELFLoggerDelegate.errorLogger, "updateMicroservice failed", e);
86             throw e;
87         }
88         saveServiceParameters(newService.getId(), newService.getEpMicroserviceParameters());
89     }
90
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);
96         }
97     }
98
99     private String encryptedPassword(String decryptedPwd) throws Exception {
100         String result = "";
101         if (decryptedPwd != null && !decryptedPwd.isEmpty()) {
102             try {
103                 result = CipherUtil.encryptPKC(decryptedPwd,
104                     SystemProperties.getProperty(SystemProperties.Decryption_Key));
105             } catch (Exception e) {
106                 logger.error(EELFLoggerDelegate.errorLogger, "encryptedPassword failed", e);
107                 throw e;
108             }
109         }
110         return result;
111     }
112 }