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============================================
36 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
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.portalsdk.core.logging.logic.EELFLoggerDelegate;
53 import org.onap.portalsdk.core.onboarding.util.CipherUtil;
54 import org.onap.portalsdk.core.service.DataAccessService;
55 import org.onap.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 List<Criterion> restrictionsList = new ArrayList<Criterion>();
90 Criterion idCriterion = Restrictions.eq("id", id);
91 restrictionsList.add(idCriterion);
92 data = (MicroserviceData) dataAccessService.getList(MicroserviceData.class, null, restrictionsList, null).get(0);
94 data.setParameterList(getServiceParameters(id));
95 } catch (Exception e) {
96 logger.error(EELFLoggerDelegate.errorLogger, "getMicroserviceDataById failed", e);
102 @SuppressWarnings("unchecked")
104 public List<MicroserviceData> getMicroserviceData() throws Exception {
105 List<MicroserviceData> list = (List<MicroserviceData>) dataAccessService.getList(MicroserviceData.class, null);
106 for (int i = 0; i < list.size(); i++) {
107 if (list.get(i).getPassword() != null)
109 list.get(i).setPassword(decryptedPassword(list.get(i).getPassword()));
110 } catch(BadPaddingException bpe){
111 logger.error(EELFLoggerDelegate.errorLogger, "Couldn't decrypt - Check decryption key in system.properties - looks wrong. Still going ahead with list population though", bpe);
113 list.get(i).setParameterList(getServiceParameters(list.get(i).getId()));
118 private List<MicroserviceParameter> getServiceParameters(long serviceId) {
119 List<MicroserviceParameter> list = getMicroServiceParametersList(serviceId);
123 @SuppressWarnings("unchecked")
124 private List<MicroserviceParameter> getMicroServiceParametersList(long serviceId) {
125 List<Criterion> restrictionsList = new ArrayList<Criterion>();
126 Criterion serviceIdCriterion = Restrictions.eq("serviceId", serviceId);
127 restrictionsList.add(serviceIdCriterion);
128 return (List<MicroserviceParameter>) dataAccessService.getList(MicroserviceParameter.class, null, restrictionsList, null);
132 public void deleteMicroservice(long serviceId) throws Exception {
135 Map<String, String> params = new HashMap<String, String>();
136 params.put("serviceId", Long.toString(serviceId));
138 dataAccessService.executeNamedQuery("deleteMicroserviceParameter", params, null);
139 dataAccessService.executeNamedQuery("deleteMicroservice", params, null);
141 } catch (Exception e) {
143 logger.error(EELFLoggerDelegate.errorLogger, "deleteMicroservice failed", e);
149 public void updateMicroservice(long serviceId, MicroserviceData newService) throws Exception {
151 newService.setId(serviceId);
152 if (newService.getPassword() != null)
153 newService.setPassword(encryptedPassword(newService.getPassword()));
154 getDataAccessService().saveDomainObject(newService, null);
155 List<MicroserviceParameter> oldService = getServiceParameters(serviceId);
157 for (int i = 0; i < oldService.size(); i++) {
159 for (int n = 0; n < newService.getParameterList().size(); n++) {
160 if (newService.getParameterList().get(n).getId().equals(oldService.get(i).getId())) {
165 if (foundParam == false) {
166 MicroserviceParameter pd = oldService.get(i);
167 getDataAccessService().deleteDomainObject(pd, null);
170 for (int i = 0; i < newService.getParameterList().size(); i++) {
171 MicroserviceParameter param = newService.getParameterList().get(i);
172 param.setServiceId(serviceId);
173 getDataAccessService().saveDomainObject(param, null);
175 } catch (Exception e) {
176 logger.error(EELFLoggerDelegate.errorLogger, "updateMicroservice failed", e);
179 saveServiceParameters(serviceId, newService.getParameterList());
183 @SuppressWarnings("unchecked")
184 public List<MicroserviceParameter> getParametersById(long serviceId) {
185 List<Criterion> restrictionsList = new ArrayList<Criterion>();
186 Criterion contextIdCrit = Restrictions.eq("serviceId", serviceId);
187 restrictionsList.add(contextIdCrit);
188 List<MicroserviceParameter> list = (List<MicroserviceParameter>) dataAccessService
189 .getList(MicroserviceParameter.class, null, restrictionsList, null);
190 logger.debug(EELFLoggerDelegate.debugLogger,
191 "getParametersById: microservice parameters list size: " + list.size());
195 private String decryptedPassword(String encryptedPwd) throws Exception {
197 if (encryptedPwd != null & encryptedPwd.length() > 0) {
199 result = CipherUtil.decryptPKC(encryptedPwd,
200 SystemProperties.getProperty(SystemProperties.Decryption_Key));
201 } catch (Exception e) {
202 logger.error(EELFLoggerDelegate.errorLogger, "decryptedPassword failed", e);
209 private String encryptedPassword(String decryptedPwd) throws Exception {
211 if (decryptedPwd != null & decryptedPwd.length() > 0) {
213 result = CipherUtil.encryptPKC(decryptedPwd,
214 SystemProperties.getProperty(SystemProperties.Decryption_Key));
215 } catch (Exception e) {
216 logger.error(EELFLoggerDelegate.errorLogger, "encryptedPassword failed", e);
223 public DataAccessService getDataAccessService() {
224 return dataAccessService;