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 javax.crypto.BadPaddingException;
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) throws Exception {
79 for (int i = 0; i < list.size(); i++) {
80 MicroserviceParameter para = list.get(i);
81 para.setServiceId(serviceId);
82 getDataAccessService().saveDomainObject(para, null);
87 public MicroserviceData getMicroserviceDataById(long id) {
88 MicroserviceData data = null;
90 List<Criterion> restrictionsList = new ArrayList<Criterion>();
91 Criterion idCriterion = Restrictions.eq("id", id);
92 restrictionsList.add(idCriterion);
93 data = (MicroserviceData) dataAccessService.getList(MicroserviceData.class, null, restrictionsList, null).get(0);
95 data.setParameterList(getServiceParameters(id));
96 } catch (Exception e) {
97 logger.error(EELFLoggerDelegate.errorLogger, "getMicroserviceDataById failed", e);
103 @SuppressWarnings("unchecked")
105 public List<MicroserviceData> getMicroserviceData() throws Exception {
106 List<MicroserviceData> list = (List<MicroserviceData>) dataAccessService.getList(MicroserviceData.class, null);
107 for (int i = 0; i < list.size(); i++) {
108 if (list.get(i).getPassword() != null)
109 list.get(i).setPassword(EPCommonSystemProperties.APP_DISPLAY_PASSWORD); //to hide password from get request
110 list.get(i).setParameterList(getServiceParameters(list.get(i).getId()));
115 private List<MicroserviceParameter> getServiceParameters(long serviceId) {
116 List<MicroserviceParameter> list = getMicroServiceParametersList(serviceId);
120 @SuppressWarnings("unchecked")
121 private List<MicroserviceParameter> getMicroServiceParametersList(long serviceId) {
122 List<Criterion> restrictionsList = new ArrayList<Criterion>();
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) throws Exception {
132 Map<String, String> params = new HashMap<String, String>();
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 (int i = 0; i < oldService.size(); i++) {
161 for (int n = 0; n < newService.getParameterList().size(); n++) {
162 if (newService.getParameterList().get(n).getId().equals(oldService.get(i).getId())) {
167 if (foundParam == false) {
168 MicroserviceParameter pd = oldService.get(i);
169 getDataAccessService().deleteDomainObject(pd, 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<Criterion>();
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.length() > 0) {
201 result = CipherUtil.decryptPKC(encryptedPwd,
202 SystemProperties.getProperty(SystemProperties.Decryption_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.length() > 0) {
215 result = CipherUtil.encryptPKC(decryptedPwd,
216 SystemProperties.getProperty(SystemProperties.Decryption_Key));
217 } catch (Exception e) {
218 logger.error(EELFLoggerDelegate.errorLogger, "encryptedPassword failed", e);
225 public DataAccessService getDataAccessService() {
226 return dataAccessService;