2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
8 * Unless otherwise specified, all software contained herein is licensed
9 * under the Apache License, Version 2.0 (the "License");
10 * you may not use this software except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * Unless otherwise specified, all documentation contained herein is licensed
22 * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23 * you may not use this documentation except in compliance with the License.
24 * You may obtain a copy of the License at
26 * https://creativecommons.org/licenses/by/4.0/
28 * Unless required by applicable law or agreed to in writing, documentation
29 * distributed under the License is distributed on an "AS IS" BASIS,
30 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31 * See the License for the specific language governing permissions and
32 * limitations under the License.
34 * ============LICENSE_END============================================
38 package org.onap.portalapp.portal.service;
40 import java.util.ArrayList;
41 import java.util.HashMap;
42 import java.util.List;
45 import org.hibernate.criterion.Criterion;
46 import org.hibernate.criterion.Restrictions;
47 import org.onap.portalapp.portal.domain.MicroserviceData;
48 import org.onap.portalapp.portal.domain.MicroserviceParameter;
49 import org.onap.portalapp.portal.logging.aop.EPMetricsLog;
50 import org.onap.portalapp.portal.utils.EPCommonSystemProperties;
51 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
52 import org.onap.portalsdk.core.onboarding.util.CipherUtil;
53 import org.onap.portalsdk.core.service.DataAccessService;
54 import org.onap.portalsdk.core.util.SystemProperties;
55 import org.springframework.beans.factory.annotation.Autowired;
56 import org.springframework.context.annotation.EnableAspectJAutoProxy;
57 import org.springframework.stereotype.Service;
59 @Service("microserviceService")
60 @EnableAspectJAutoProxy
62 public class MicroserviceServiceImpl implements MicroserviceService {
64 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MicroserviceServiceImpl.class);
67 private DataAccessService dataAccessService;
69 public Long saveMicroservice(MicroserviceData newService) throws Exception {
70 if (newService.getPassword() != null)
71 newService.setPassword(encryptedPassword(newService.getPassword()));
72 getDataAccessService().saveDomainObject(newService, null);
73 return newService.getId();
76 public void saveServiceParameters(long serviceId, List<MicroserviceParameter> list) {
77 for (MicroserviceParameter para : list) {
78 para.setServiceId(serviceId);
79 getDataAccessService().saveDomainObject(para, null);
84 public MicroserviceData getMicroserviceDataById(long id) {
85 MicroserviceData data;
87 List<Criterion> restrictionsList = new ArrayList<>();
88 Criterion idCriterion = Restrictions.eq("id", id);
89 restrictionsList.add(idCriterion);
90 data = (MicroserviceData) dataAccessService.getList(MicroserviceData.class, null, restrictionsList, null).get(0);
92 data.setParameterList(getServiceParameters(id));
93 } catch (Exception e) {
94 logger.error(EELFLoggerDelegate.errorLogger, "getMicroserviceDataById failed", e);
100 @SuppressWarnings("unchecked")
102 public List<MicroserviceData> getMicroserviceData() {
103 List<MicroserviceData> list = (List<MicroserviceData>) dataAccessService.getList(MicroserviceData.class, null);
104 for (MicroserviceData microserviceData : list) {
105 if (microserviceData.getPassword() != null) {
107 .setPassword(EPCommonSystemProperties.APP_DISPLAY_PASSWORD); //to hide password from get request
109 microserviceData.setParameterList(getServiceParameters(microserviceData.getId()));
114 private List<MicroserviceParameter> getServiceParameters(long serviceId) {
115 return getMicroServiceParametersList(serviceId);
118 @SuppressWarnings("unchecked")
119 private List<MicroserviceParameter> getMicroServiceParametersList(long serviceId) {
120 List<Criterion> restrictionsList = new ArrayList<>();
121 Criterion serviceIdCriterion = Restrictions.eq("serviceId", serviceId);
122 restrictionsList.add(serviceIdCriterion);
123 return (List<MicroserviceParameter>) dataAccessService.getList(MicroserviceParameter.class, null, restrictionsList, null);
127 public void deleteMicroservice(long serviceId) {
130 Map<String, String> params = new HashMap<>();
131 params.put("serviceId", Long.toString(serviceId));
133 dataAccessService.executeNamedQuery("deleteMicroserviceParameter", params, null);
134 dataAccessService.executeNamedQuery("deleteMicroservice", params, null);
136 } catch (Exception e) {
138 logger.error(EELFLoggerDelegate.errorLogger, "deleteMicroservice failed", e);
144 public void updateMicroservice(long serviceId, MicroserviceData newService) throws Exception {
146 newService.setId(serviceId);
147 if (newService.getPassword() != null){
148 if(newService.getPassword().equals(EPCommonSystemProperties.APP_DISPLAY_PASSWORD)){
149 MicroserviceData oldMS = getMicroserviceDataById(serviceId);
150 newService.setPassword(oldMS.getPassword()); // keep the old password
152 newService.setPassword(encryptedPassword(newService.getPassword())); //new password
154 getDataAccessService().saveDomainObject(newService, null);
155 List<MicroserviceParameter> oldService = getServiceParameters(serviceId);
157 for (MicroserviceParameter microserviceParameter : oldService) {
159 for (int n = 0; n < newService.getParameterList().size(); n++) {
160 if (newService.getParameterList().get(n).getId().equals(microserviceParameter.getId())) {
166 getDataAccessService().deleteDomainObject(microserviceParameter, null);
169 for (int i = 0; i < newService.getParameterList().size(); i++) {
170 MicroserviceParameter param = newService.getParameterList().get(i);
171 param.setServiceId(serviceId);
172 getDataAccessService().saveDomainObject(param, null);
174 } catch (Exception e) {
175 logger.error(EELFLoggerDelegate.errorLogger, "updateMicroservice failed", e);
178 saveServiceParameters(serviceId, newService.getParameterList());
182 @SuppressWarnings("unchecked")
183 public List<MicroserviceParameter> getParametersById(long serviceId) {
184 List<Criterion> restrictionsList = new ArrayList<>();
185 Criterion contextIdCrit = Restrictions.eq("serviceId", serviceId);
186 restrictionsList.add(contextIdCrit);
187 List<MicroserviceParameter> list = (List<MicroserviceParameter>) dataAccessService
188 .getList(MicroserviceParameter.class, null, restrictionsList, null);
189 logger.debug(EELFLoggerDelegate.debugLogger,
190 "getParametersById: microservice parameters list size: " + list.size());
194 private String decryptedPassword(String encryptedPwd) throws Exception {
196 if (encryptedPwd != null && !encryptedPwd.isEmpty()) {
198 result = CipherUtil.decryptPKC(encryptedPwd,
199 SystemProperties.getProperty(SystemProperties.Decryption_Key));
200 } catch (Exception e) {
201 logger.error(EELFLoggerDelegate.errorLogger, "decryptedPassword failed", e);
208 private String encryptedPassword(String decryptedPwd) throws Exception {
210 if (decryptedPwd != null && !decryptedPwd.isEmpty()) {
212 result = CipherUtil.encryptPKC(decryptedPwd,
213 SystemProperties.getProperty(SystemProperties.Decryption_Key));
214 } catch (Exception e) {
215 logger.error(EELFLoggerDelegate.errorLogger, "encryptedPassword failed", e);
222 public DataAccessService getDataAccessService() {
223 return dataAccessService;