2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
7 * Modifications Copyright (c) 2019 Samsung
8 * ===================================================================
10 * Unless otherwise specified, all software contained herein is licensed
11 * under the Apache License, Version 2.0 (the "License");
12 * you may not use this software except in compliance with the License.
13 * You may obtain a copy of the License at
15 * http://www.apache.org/licenses/LICENSE-2.0
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
23 * Unless otherwise specified, all documentation contained herein is licensed
24 * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
25 * you may not use this documentation except in compliance with the License.
26 * You may obtain a copy of the License at
28 * https://creativecommons.org/licenses/by/4.0/
30 * Unless required by applicable law or agreed to in writing, documentation
31 * distributed under the License is distributed on an "AS IS" BASIS,
32 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
33 * See the License for the specific language governing permissions and
34 * limitations under the License.
36 * ============LICENSE_END============================================
40 package org.onap.portalapp.portal.service;
42 import java.util.ArrayList;
43 import java.util.HashMap;
44 import java.util.List;
47 import org.hibernate.criterion.Criterion;
48 import org.hibernate.criterion.Restrictions;
49 import org.onap.portalapp.portal.domain.MicroserviceData;
50 import org.onap.portalapp.portal.domain.MicroserviceParameter;
51 import org.onap.portalapp.portal.logging.aop.EPMetricsLog;
52 import org.onap.portalapp.portal.utils.EPCommonSystemProperties;
53 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
54 import org.onap.portalsdk.core.onboarding.util.CipherUtil;
55 import org.onap.portalsdk.core.service.DataAccessService;
56 import org.onap.portalsdk.core.util.SystemProperties;
57 import org.springframework.beans.factory.annotation.Autowired;
58 import org.springframework.context.annotation.EnableAspectJAutoProxy;
59 import org.springframework.stereotype.Service;
61 @Service("microserviceService")
62 @EnableAspectJAutoProxy
64 public class MicroserviceServiceImpl implements MicroserviceService {
66 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MicroserviceServiceImpl.class);
69 private DataAccessService dataAccessService;
71 public Long saveMicroservice(MicroserviceData newService) throws Exception {
72 if (newService.getPassword() != null)
73 newService.setPassword(encryptedPassword(newService.getPassword()));
74 getDataAccessService().saveDomainObject(newService, null);
75 return newService.getId();
78 public void saveServiceParameters(long serviceId, List<MicroserviceParameter> list) {
79 for (MicroserviceParameter para : list) {
80 para.setServiceId(serviceId);
81 getDataAccessService().saveDomainObject(para, null);
86 public MicroserviceData getMicroserviceDataById(long id) {
87 MicroserviceData data;
89 List<Criterion> restrictionsList = new ArrayList<>();
90 Criterion idCriterion = Restrictions.eq("id", id);
91 restrictionsList.add(idCriterion);
92 data = (MicroserviceData) dataAccessService.getList(MicroserviceData.class, null, restrictionsList, null).get(0);
94 data.setParameterList(getServiceParameters(id));
95 } catch (Exception e) {
96 logger.error(EELFLoggerDelegate.errorLogger, "getMicroserviceDataById failed", e);
102 @SuppressWarnings("unchecked")
104 public List<MicroserviceData> getMicroserviceData() {
105 List<MicroserviceData> list = (List<MicroserviceData>) dataAccessService.getList(MicroserviceData.class, null);
106 for (MicroserviceData microserviceData : list) {
107 if (microserviceData.getPassword() != null) {
109 .setPassword(EPCommonSystemProperties.APP_DISPLAY_PASSWORD); //to hide password from get request
111 microserviceData.setParameterList(getServiceParameters(microserviceData.getId()));
116 private List<MicroserviceParameter> getServiceParameters(long serviceId) {
117 return getMicroServiceParametersList(serviceId);
120 @SuppressWarnings("unchecked")
121 private List<MicroserviceParameter> getMicroServiceParametersList(long serviceId) {
122 List<Criterion> restrictionsList = new ArrayList<>();
123 Criterion serviceIdCriterion = Restrictions.eq("serviceId", serviceId);
124 restrictionsList.add(serviceIdCriterion);
125 return (List<MicroserviceParameter>) dataAccessService.getList(MicroserviceParameter.class, null, restrictionsList, null);
129 public void deleteMicroservice(long serviceId) {
132 Map<String, String> params = new HashMap<>();
133 params.put("serviceId", Long.toString(serviceId));
135 dataAccessService.executeNamedQuery("deleteMicroserviceParameter", params, null);
136 dataAccessService.executeNamedQuery("deleteMicroservice", params, null);
138 } catch (Exception e) {
140 logger.error(EELFLoggerDelegate.errorLogger, "deleteMicroservice failed", e);
146 public void updateMicroservice(long serviceId, MicroserviceData newService) throws Exception {
148 newService.setId(serviceId);
149 if (newService.getPassword() != null){
150 if(newService.getPassword().equals(EPCommonSystemProperties.APP_DISPLAY_PASSWORD)){
151 MicroserviceData oldMS = getMicroserviceDataById(serviceId);
152 newService.setPassword(oldMS.getPassword()); // keep the old password
154 newService.setPassword(encryptedPassword(newService.getPassword())); //new password
156 getDataAccessService().saveDomainObject(newService, null);
157 List<MicroserviceParameter> oldService = getServiceParameters(serviceId);
159 for (MicroserviceParameter microserviceParameter : oldService) {
161 for (int n = 0; n < newService.getParameterList().size(); n++) {
162 if (newService.getParameterList().get(n).getId().equals(microserviceParameter.getId())) {
168 getDataAccessService().deleteDomainObject(microserviceParameter, null);
171 for (int i = 0; i < newService.getParameterList().size(); i++) {
172 MicroserviceParameter param = newService.getParameterList().get(i);
173 param.setServiceId(serviceId);
174 getDataAccessService().saveDomainObject(param, null);
176 } catch (Exception e) {
177 logger.error(EELFLoggerDelegate.errorLogger, "updateMicroservice failed", e);
180 saveServiceParameters(serviceId, newService.getParameterList());
184 @SuppressWarnings("unchecked")
185 public List<MicroserviceParameter> getParametersById(long serviceId) {
186 List<Criterion> restrictionsList = new ArrayList<>();
187 Criterion contextIdCrit = Restrictions.eq("serviceId", serviceId);
188 restrictionsList.add(contextIdCrit);
189 List<MicroserviceParameter> list = (List<MicroserviceParameter>) dataAccessService
190 .getList(MicroserviceParameter.class, null, restrictionsList, null);
191 logger.debug(EELFLoggerDelegate.debugLogger,
192 "getParametersById: microservice parameters list size: " + list.size());
196 private String decryptedPassword(String encryptedPwd) throws Exception {
198 if (encryptedPwd != null && !encryptedPwd.isEmpty()) {
200 result = CipherUtil.decryptPKC(encryptedPwd,
201 SystemProperties.getProperty(SystemProperties.Decryption_Key));
202 } catch (Exception e) {
203 logger.error(EELFLoggerDelegate.errorLogger, "decryptedPassword failed", e);
210 private String encryptedPassword(String decryptedPwd) throws Exception {
212 if (decryptedPwd != null && !decryptedPwd.isEmpty()) {
214 result = CipherUtil.encryptPKC(decryptedPwd,
215 SystemProperties.getProperty(SystemProperties.Decryption_Key));
216 } catch (Exception e) {
217 logger.error(EELFLoggerDelegate.errorLogger, "encryptedPassword failed", e);
224 public DataAccessService getDataAccessService() {
225 return dataAccessService;