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.onboarding.util.KeyConstants;
56 import org.onap.portalsdk.core.onboarding.util.KeyProperties;
57 import org.onap.portalsdk.core.service.DataAccessService;
58 import org.onap.portalsdk.core.util.SystemProperties;
59 import org.springframework.beans.factory.annotation.Autowired;
60 import org.springframework.context.annotation.EnableAspectJAutoProxy;
61 import org.springframework.stereotype.Service;
63 @Service("microserviceService")
64 @EnableAspectJAutoProxy
66 public class MicroserviceServiceImpl implements MicroserviceService {
68 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MicroserviceServiceImpl.class);
71 private DataAccessService dataAccessService;
73 public Long saveMicroservice(MicroserviceData newService) throws Exception {
74 if (newService.getPassword() != null)
75 newService.setPassword(encryptedPassword(newService.getPassword()));
76 getDataAccessService().saveDomainObject(newService, null);
77 return newService.getId();
80 public void saveServiceParameters(long serviceId, List<MicroserviceParameter> list) {
81 for (MicroserviceParameter para : list) {
82 para.setServiceId(serviceId);
83 getDataAccessService().saveDomainObject(para, null);
88 public MicroserviceData getMicroserviceDataById(long id) {
89 MicroserviceData data;
91 List<Criterion> restrictionsList = new ArrayList<>();
92 Criterion idCriterion = Restrictions.eq("id", id);
93 restrictionsList.add(idCriterion);
94 data = (MicroserviceData) dataAccessService.getList(MicroserviceData.class, null, restrictionsList, null).get(0);
96 data.setParameterList(getServiceParameters(id));
97 } catch (Exception e) {
98 logger.error(EELFLoggerDelegate.errorLogger, "getMicroserviceDataById failed", e);
104 @SuppressWarnings("unchecked")
106 public List<MicroserviceData> getMicroserviceData() {
107 List<MicroserviceData> list = (List<MicroserviceData>) dataAccessService.getList(MicroserviceData.class, null);
108 for (MicroserviceData microserviceData : list) {
109 if (microserviceData.getPassword() != null) {
111 .setPassword(EPCommonSystemProperties.APP_DISPLAY_PASSWORD); //to hide password from get request
113 microserviceData.setParameterList(getServiceParameters(microserviceData.getId()));
118 private List<MicroserviceParameter> getServiceParameters(long serviceId) {
119 return getMicroServiceParametersList(serviceId);
122 @SuppressWarnings("unchecked")
123 private List<MicroserviceParameter> getMicroServiceParametersList(long serviceId) {
124 List<Criterion> restrictionsList = new ArrayList<>();
125 Criterion serviceIdCriterion = Restrictions.eq("serviceId", serviceId);
126 restrictionsList.add(serviceIdCriterion);
127 return (List<MicroserviceParameter>) dataAccessService.getList(MicroserviceParameter.class, null, restrictionsList, null);
131 public void deleteMicroservice(long serviceId) {
134 Map<String, String> params = new HashMap<>();
135 params.put("serviceId", Long.toString(serviceId));
137 dataAccessService.executeNamedQuery("deleteMicroserviceParameter", params, null);
138 dataAccessService.executeNamedQuery("deleteMicroservice", params, null);
140 } catch (Exception e) {
141 logger.error(EELFLoggerDelegate.errorLogger, "deleteMicroservice failed", e);
147 public void updateMicroservice(long serviceId, MicroserviceData newService) throws Exception {
149 newService.setId(serviceId);
150 if (newService.getPassword() != null){
151 if(newService.getPassword().equals(EPCommonSystemProperties.APP_DISPLAY_PASSWORD)){
152 MicroserviceData oldMS = getMicroserviceDataById(serviceId);
153 newService.setPassword(oldMS.getPassword()); // keep the old password
155 newService.setPassword(encryptedPassword(newService.getPassword())); //new password
157 getDataAccessService().saveDomainObject(newService, null);
158 List<MicroserviceParameter> oldService = getServiceParameters(serviceId);
160 for (MicroserviceParameter microserviceParameter : oldService) {
162 for (int n = 0; n < newService.getParameterList().size(); n++) {
163 if (newService.getParameterList().get(n).getId().equals(microserviceParameter.getId())) {
169 getDataAccessService().deleteDomainObject(microserviceParameter, null);
172 for (int i = 0; i < newService.getParameterList().size(); i++) {
173 MicroserviceParameter param = newService.getParameterList().get(i);
174 param.setServiceId(serviceId);
175 getDataAccessService().saveDomainObject(param, null);
177 } catch (Exception e) {
178 logger.error(EELFLoggerDelegate.errorLogger, "updateMicroservice failed", e);
181 saveServiceParameters(serviceId, newService.getParameterList());
185 @SuppressWarnings("unchecked")
186 public List<MicroserviceParameter> getParametersById(long serviceId) {
187 List<Criterion> restrictionsList = new ArrayList<>();
188 Criterion contextIdCrit = Restrictions.eq("serviceId", serviceId);
189 restrictionsList.add(contextIdCrit);
190 List<MicroserviceParameter> list = (List<MicroserviceParameter>) dataAccessService
191 .getList(MicroserviceParameter.class, null, restrictionsList, null);
192 logger.debug(EELFLoggerDelegate.debugLogger,
193 "getParametersById: microservice parameters list size: " + list.size());
197 private String decryptedPassword(String encryptedPwd) throws Exception {
199 if (encryptedPwd != null && !encryptedPwd.isEmpty()) {
201 result = CipherUtil.decryptPKC(encryptedPwd,
202 KeyProperties.getProperty(KeyConstants.CIPHER_ENCRYPTION_KEY));
203 } catch (Exception e) {
204 logger.error(EELFLoggerDelegate.errorLogger, "decryptedPassword failed", e);
211 private String encryptedPassword(String decryptedPwd) throws Exception {
213 if (decryptedPwd != null && !decryptedPwd.isEmpty()) {
215 result = CipherUtil.encryptPKC(decryptedPwd,
216 KeyProperties.getProperty(KeyConstants.CIPHER_ENCRYPTION_KEY));
217 } catch (Exception e) {
218 logger.error(EELFLoggerDelegate.errorLogger, "encryptedPassword failed", e);
225 public DataAccessService getDataAccessService() {
226 return dataAccessService;