2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 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============================================
36 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
38 package org.openecomp.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.openecomp.portalapp.portal.domain.MicroserviceData;
50 import org.openecomp.portalapp.portal.domain.MicroserviceParameter;
51 import org.openecomp.portalapp.portal.logging.aop.EPMetricsLog;
52 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
53 import org.openecomp.portalsdk.core.onboarding.util.CipherUtil;
54 import org.openecomp.portalsdk.core.service.DataAccessService;
55 import org.openecomp.portalsdk.core.util.SystemProperties;
56 import org.springframework.beans.factory.annotation.Autowired;
57 import org.springframework.context.annotation.EnableAspectJAutoProxy;
58 import org.springframework.stereotype.Service;
60 @Service("microserviceService")
61 @EnableAspectJAutoProxy
63 public class MicroserviceServiceImpl implements MicroserviceService {
65 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MicroserviceServiceImpl.class);
68 private DataAccessService dataAccessService;
70 public Long saveMicroservice(MicroserviceData newService) throws Exception {
71 if (newService.getPassword() != null)
72 newService.setPassword(encryptedPassword(newService.getPassword()));
73 getDataAccessService().saveDomainObject(newService, null);
74 return newService.getId();
77 public void saveServiceParameters(long serviceId, List<MicroserviceParameter> list) throws Exception {
78 for (int i = 0; i < list.size(); i++) {
79 MicroserviceParameter para = list.get(i);
80 para.setServiceId(serviceId);
81 getDataAccessService().saveDomainObject(para, null);
86 public MicroserviceData getMicroserviceDataById(long id) {
87 MicroserviceData data = null;
89 data = (MicroserviceData) dataAccessService
90 .getList(MicroserviceData.class, " where id = '" + id + "'", null, null).get(0);
91 data.setParameterList(getServiceParameters(id));
92 } catch (Exception e) {
93 logger.error(EELFLoggerDelegate.errorLogger, "getMicroserviceDataById failed", e);
99 @SuppressWarnings("unchecked")
101 public List<MicroserviceData> getMicroserviceData() throws Exception {
102 List<MicroserviceData> list = (List<MicroserviceData>) dataAccessService.getList(MicroserviceData.class, null);
103 for (int i = 0; i < list.size(); i++) {
104 if (list.get(i).getPassword() != null)
106 list.get(i).setPassword(decryptedPassword(list.get(i).getPassword()));
107 } catch(BadPaddingException bpe){
108 logger.error(EELFLoggerDelegate.errorLogger, "Couldn't decrypt - Check decryption key in system.properties - looks wrong. Still going ahead with list population though", bpe);
110 list.get(i).setParameterList(getServiceParameters(list.get(i).getId()));
115 @SuppressWarnings("unchecked")
116 private List<MicroserviceParameter> getServiceParameters(long serviceId) {
117 List<MicroserviceParameter> list = (List<MicroserviceParameter>) dataAccessService
118 .getList(MicroserviceParameter.class, " where service_id = '" + serviceId + "'", null, null);
123 public void deleteMicroservice(long serviceId) throws Exception {
126 Map<String, String> params = new HashMap<String, String>();
127 params.put("serviceId", Long.toString(serviceId));
129 dataAccessService.executeNamedQuery("deleteMicroserviceParameter", params, null);
130 dataAccessService.executeNamedQuery("deleteMicroservice", params, null);
132 } catch (Exception e) {
134 logger.error(EELFLoggerDelegate.errorLogger, "deleteMicroservice failed", e);
139 @SuppressWarnings("unchecked")
141 public void updateMicroservice(long serviceId, MicroserviceData newService) throws Exception {
143 newService.setId(serviceId);
144 if (newService.getPassword() != null)
145 newService.setPassword(encryptedPassword(newService.getPassword()));
146 getDataAccessService().saveDomainObject(newService, null);
147 List<MicroserviceParameter> oldService = getServiceParameters(serviceId);
149 for (int i = 0; i < oldService.size(); i++) {
151 for (int n = 0; n < newService.getParameterList().size(); n++) {
152 if (newService.getParameterList().get(n).getId().equals(oldService.get(i).getId())) {
157 if (foundParam == false) {
158 MicroserviceParameter pd = oldService.get(i);
159 getDataAccessService().deleteDomainObject(pd, null);
162 for (int i = 0; i < newService.getParameterList().size(); i++) {
163 MicroserviceParameter param = newService.getParameterList().get(i);
164 param.setServiceId(serviceId);
165 getDataAccessService().saveDomainObject(param, null);
167 } catch (Exception e) {
168 logger.error(EELFLoggerDelegate.errorLogger, "updateMicroservice failed", e);
171 saveServiceParameters(serviceId, newService.getParameterList());
175 @SuppressWarnings("unchecked")
176 public List<MicroserviceParameter> getParametersById(long serviceId) {
177 List<Criterion> restrictionsList = new ArrayList<Criterion>();
178 Criterion contextIdCrit = Restrictions.eq("serviceId", serviceId);
179 restrictionsList.add(contextIdCrit);
180 List<MicroserviceParameter> list = (List<MicroserviceParameter>) dataAccessService
181 .getList(MicroserviceParameter.class, null, restrictionsList, null);
182 logger.debug(EELFLoggerDelegate.debugLogger,
183 "getParametersById: microservice parameters list size: " + list.size());
187 private String decryptedPassword(String encryptedPwd) throws Exception {
189 if (encryptedPwd != null & encryptedPwd.length() > 0) {
191 result = CipherUtil.decrypt(encryptedPwd,
192 SystemProperties.getProperty(SystemProperties.Decryption_Key));
193 } catch (Exception e) {
194 logger.error(EELFLoggerDelegate.errorLogger, "decryptedPassword failed", e);
201 private String encryptedPassword(String decryptedPwd) throws Exception {
203 if (decryptedPwd != null & decryptedPwd.length() > 0) {
205 result = CipherUtil.encrypt(decryptedPwd,
206 SystemProperties.getProperty(SystemProperties.Decryption_Key));
207 } catch (Exception e) {
208 logger.error(EELFLoggerDelegate.errorLogger, "encryptedPassword failed", e);
215 public DataAccessService getDataAccessService() {
216 return dataAccessService;