2 * ================================================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ================================================================================
20 package org.openecomp.portalapp.portal.service;
22 import java.util.ArrayList;
23 import java.util.HashMap;
24 import java.util.List;
27 import javax.crypto.BadPaddingException;
29 import org.hibernate.criterion.Criterion;
30 import org.hibernate.criterion.Restrictions;
31 import org.openecomp.portalapp.portal.domain.MicroserviceData;
32 import org.openecomp.portalapp.portal.domain.MicroserviceParameter;
33 import org.openecomp.portalapp.portal.logging.aop.EPMetricsLog;
34 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
35 import org.openecomp.portalsdk.core.onboarding.util.CipherUtil;
36 import org.openecomp.portalsdk.core.service.DataAccessService;
37 import org.openecomp.portalsdk.core.util.SystemProperties;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.context.annotation.EnableAspectJAutoProxy;
40 import org.springframework.stereotype.Service;
42 @Service("microserviceService")
43 @EnableAspectJAutoProxy
45 public class MicroserviceServiceImpl implements MicroserviceService {
47 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MicroserviceServiceImpl.class);
50 private DataAccessService dataAccessService;
52 public Long saveMicroservice(MicroserviceData newService) throws Exception {
53 if (newService.getPassword() != null)
54 newService.setPassword(encryptedPassword(newService.getPassword()));
55 getDataAccessService().saveDomainObject(newService, null);
56 return newService.getId();
59 public void saveServiceParameters(long serviceId, List<MicroserviceParameter> list) throws Exception {
60 for (int i = 0; i < list.size(); i++) {
61 MicroserviceParameter para = list.get(i);
62 para.setServiceId(serviceId);
63 getDataAccessService().saveDomainObject(para, null);
68 public MicroserviceData getMicroserviceDataById(long id) {
69 MicroserviceData data = null;
71 data = (MicroserviceData) dataAccessService
72 .getList(MicroserviceData.class, " where id = '" + id + "'", null, null).get(0);
73 data.setParameterList(getServiceParameters(id));
74 } catch (Exception e) {
75 logger.error(EELFLoggerDelegate.errorLogger, "getMicroserviceDataById failed", e);
81 @SuppressWarnings("unchecked")
83 public List<MicroserviceData> getMicroserviceData() throws Exception {
84 List<MicroserviceData> list = (List<MicroserviceData>) dataAccessService.getList(MicroserviceData.class, null);
85 for (int i = 0; i < list.size(); i++) {
86 if (list.get(i).getPassword() != null)
88 list.get(i).setPassword(decryptedPassword(list.get(i).getPassword()));
89 } catch(BadPaddingException bpe){
90 logger.error(EELFLoggerDelegate.errorLogger, "Couldn't decrypt - Check decryption key in system.properties - looks wrong. Still going ahead with list population though", bpe);
92 list.get(i).setParameterList(getServiceParameters(list.get(i).getId()));
97 @SuppressWarnings("unchecked")
98 private List<MicroserviceParameter> getServiceParameters(long serviceId) {
99 List<MicroserviceParameter> list = (List<MicroserviceParameter>) dataAccessService
100 .getList(MicroserviceParameter.class, " where service_id = '" + serviceId + "'", null, null);
105 public void deleteMicroservice(long serviceId) throws Exception {
108 Map<String, String> params = new HashMap<String, String>();
109 params.put("serviceId", Long.toString(serviceId));
111 dataAccessService.executeNamedQuery("deleteMicroserviceParameter", params, null);
112 dataAccessService.executeNamedQuery("deleteMicroservice", params, null);
114 } catch (Exception e) {
116 logger.error(EELFLoggerDelegate.errorLogger, "deleteMicroservice failed", e);
121 @SuppressWarnings("unchecked")
123 public void updateMicroservice(long serviceId, MicroserviceData newService) throws Exception {
125 newService.setId(serviceId);
126 if (newService.getPassword() != null)
127 newService.setPassword(encryptedPassword(newService.getPassword()));
128 getDataAccessService().saveDomainObject(newService, null);
129 List<MicroserviceParameter> oldService = getServiceParameters(serviceId);
131 for (int i = 0; i < oldService.size(); i++) {
133 for (int n = 0; n < newService.getParameterList().size(); n++) {
134 if (newService.getParameterList().get(n).getId().equals(oldService.get(i).getId())) {
139 if (foundParam == false) {
140 MicroserviceParameter pd = oldService.get(i);
141 getDataAccessService().deleteDomainObject(pd, null);
144 for (int i = 0; i < newService.getParameterList().size(); i++) {
145 MicroserviceParameter param = newService.getParameterList().get(i);
146 param.setServiceId(serviceId);
147 getDataAccessService().saveDomainObject(param, null);
149 } catch (Exception e) {
150 logger.error(EELFLoggerDelegate.errorLogger, "updateMicroservice failed", e);
153 saveServiceParameters(serviceId, newService.getParameterList());
157 @SuppressWarnings("unchecked")
158 public List<MicroserviceParameter> getParametersById(long serviceId) {
159 List<Criterion> restrictionsList = new ArrayList<Criterion>();
160 Criterion contextIdCrit = Restrictions.eq("serviceId", serviceId);
161 restrictionsList.add(contextIdCrit);
162 List<MicroserviceParameter> list = (List<MicroserviceParameter>) dataAccessService
163 .getList(MicroserviceParameter.class, null, restrictionsList, null);
164 logger.debug(EELFLoggerDelegate.debugLogger,
165 "getParametersById: microservice parameters list size: " + list.size());
169 private String decryptedPassword(String encryptedPwd) throws Exception {
171 if (encryptedPwd != null & encryptedPwd.length() > 0) {
173 result = CipherUtil.decrypt(encryptedPwd,
174 SystemProperties.getProperty(SystemProperties.Decryption_Key));
175 } catch (Exception e) {
176 logger.error(EELFLoggerDelegate.errorLogger, "decryptedPassword failed", e);
183 private String encryptedPassword(String decryptedPwd) throws Exception {
185 if (decryptedPwd != null & decryptedPwd.length() > 0) {
187 result = CipherUtil.encrypt(decryptedPwd,
188 SystemProperties.getProperty(SystemProperties.Decryption_Key));
189 } catch (Exception e) {
190 logger.error(EELFLoggerDelegate.errorLogger, "encryptedPassword failed", e);
197 public DataAccessService getDataAccessService() {
198 return dataAccessService;