23a8c504bb6fa133b92114c767d9afbc0e940c29
[portal.git] / ecomp-portal-BE-common / src / main / java / org / openecomp / portalapp / portal / service / MicroserviceServiceImpl.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
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
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
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.
20  *
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
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
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.
33  *
34  * ============LICENSE_END============================================
35  *
36  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38 package org.openecomp.portalapp.portal.service;
39
40 import java.util.ArrayList;
41 import java.util.HashMap;
42 import java.util.List;
43 import java.util.Map;
44
45 import javax.crypto.BadPaddingException;
46
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;
59
60 @Service("microserviceService")
61 @EnableAspectJAutoProxy
62 @EPMetricsLog
63 public class MicroserviceServiceImpl implements MicroserviceService {
64
65         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MicroserviceServiceImpl.class);
66
67         @Autowired
68         private DataAccessService dataAccessService;
69
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();
75         }
76
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);
82                 }
83         }
84
85         @Override
86         public MicroserviceData getMicroserviceDataById(long id) {
87                 MicroserviceData data = null;
88                 try {
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);
94                         throw e;
95                 }
96                 return data;
97         }
98
99         @SuppressWarnings("unchecked")
100         @Override
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)
105                                 try{
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);
109                                 }
110                         list.get(i).setParameterList(getServiceParameters(list.get(i).getId()));
111                 }
112                 return list;
113         }
114
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);
119                 return list;
120         }
121
122         @Override
123         public void deleteMicroservice(long serviceId) throws Exception {
124
125                 try {
126                         Map<String, String> params = new HashMap<String, String>();
127                         params.put("serviceId", Long.toString(serviceId));
128
129                         dataAccessService.executeNamedQuery("deleteMicroserviceParameter", params, null);
130                         dataAccessService.executeNamedQuery("deleteMicroservice", params, null);
131
132                 } catch (Exception e) {
133                         e.printStackTrace();
134                         logger.error(EELFLoggerDelegate.errorLogger, "deleteMicroservice failed", e);
135                         throw e;
136                 }
137         }
138
139         @SuppressWarnings("unchecked")
140         @Override
141         public void updateMicroservice(long serviceId, MicroserviceData newService) throws Exception {
142                 try {
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);
148                         boolean foundParam;
149                         for (int i = 0; i < oldService.size(); i++) {
150                                 foundParam = false;
151                                 for (int n = 0; n < newService.getParameterList().size(); n++) {
152                                         if (newService.getParameterList().get(n).getId().equals(oldService.get(i).getId())) {
153                                                 foundParam = true;
154                                                 break;
155                                         }
156                                 }
157                                 if (foundParam == false) {
158                                         MicroserviceParameter pd = oldService.get(i);
159                                         getDataAccessService().deleteDomainObject(pd, null);
160                                 }
161                         }
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);
166                         }
167                 } catch (Exception e) {
168                         logger.error(EELFLoggerDelegate.errorLogger, "updateMicroservice failed", e);
169                         throw e;
170                 }
171                 saveServiceParameters(serviceId, newService.getParameterList());
172         }
173
174         @Override
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());
184                 return list;
185         }
186
187         private String decryptedPassword(String encryptedPwd) throws Exception {
188                 String result = "";
189                 if (encryptedPwd != null & encryptedPwd.length() > 0) {
190                         try {
191                                 result = CipherUtil.decrypt(encryptedPwd,
192                                                 SystemProperties.getProperty(SystemProperties.Decryption_Key));
193                         } catch (Exception e) {
194                                 logger.error(EELFLoggerDelegate.errorLogger, "decryptedPassword failed", e);
195                                 throw e;
196                         }
197                 }
198                 return result;
199         }
200
201         private String encryptedPassword(String decryptedPwd) throws Exception {
202                 String result = "";
203                 if (decryptedPwd != null & decryptedPwd.length() > 0) {
204                         try {
205                                 result = CipherUtil.encrypt(decryptedPwd,
206                                                 SystemProperties.getProperty(SystemProperties.Decryption_Key));
207                         } catch (Exception e) {
208                                 logger.error(EELFLoggerDelegate.errorLogger, "encryptedPassword failed", e);
209                                 throw e;
210                         }
211                 }
212                 return result;
213         }
214
215         public DataAccessService getDataAccessService() {
216                 return dataAccessService;
217         }
218
219 }