Security/ Package Name changes
[portal.git] / ecomp-portal-BE-common / src / main / java / org / onap / portalapp / portal / service / MicroserviceServiceImpl.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 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.onap.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.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;
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                         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);
93                         
94                         data.setParameterList(getServiceParameters(id));
95                 } catch (Exception e) {
96                         logger.error(EELFLoggerDelegate.errorLogger, "getMicroserviceDataById failed", e);
97                         throw e;
98                 }
99                 return data;
100         }
101
102         @SuppressWarnings("unchecked")
103         @Override
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)
108                                 try{
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);
112                                 }
113                         list.get(i).setParameterList(getServiceParameters(list.get(i).getId()));
114                 }
115                 return list;
116         }
117
118         private List<MicroserviceParameter> getServiceParameters(long serviceId) {
119                 List<MicroserviceParameter> list = getMicroServiceParametersList(serviceId);
120                 return list;
121         }
122
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);
129         }
130
131         @Override
132         public void deleteMicroservice(long serviceId) throws Exception {
133
134                 try {
135                         Map<String, String> params = new HashMap<String, String>();
136                         params.put("serviceId", Long.toString(serviceId));
137
138                         dataAccessService.executeNamedQuery("deleteMicroserviceParameter", params, null);
139                         dataAccessService.executeNamedQuery("deleteMicroservice", params, null);
140
141                 } catch (Exception e) {
142                         e.printStackTrace();
143                         logger.error(EELFLoggerDelegate.errorLogger, "deleteMicroservice failed", e);
144                         throw e;
145                 }
146         }
147
148         @Override
149         public void updateMicroservice(long serviceId, MicroserviceData newService) throws Exception {
150                 try {
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);
156                         boolean foundParam;
157                         for (int i = 0; i < oldService.size(); i++) {
158                                 foundParam = false;
159                                 for (int n = 0; n < newService.getParameterList().size(); n++) {
160                                         if (newService.getParameterList().get(n).getId().equals(oldService.get(i).getId())) {
161                                                 foundParam = true;
162                                                 break;
163                                         }
164                                 }
165                                 if (foundParam == false) {
166                                         MicroserviceParameter pd = oldService.get(i);
167                                         getDataAccessService().deleteDomainObject(pd, null);
168                                 }
169                         }
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);
174                         }
175                 } catch (Exception e) {
176                         logger.error(EELFLoggerDelegate.errorLogger, "updateMicroservice failed", e);
177                         throw e;
178                 }
179                 saveServiceParameters(serviceId, newService.getParameterList());
180         }
181
182         @Override
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());
192                 return list;
193         }
194
195         private String decryptedPassword(String encryptedPwd) throws Exception {
196                 String result = "";
197                 if (encryptedPwd != null & encryptedPwd.length() > 0) {
198                         try {
199                                 result = CipherUtil.decryptPKC(encryptedPwd,
200                                                 SystemProperties.getProperty(SystemProperties.Decryption_Key));
201                         } catch (Exception e) {
202                                 logger.error(EELFLoggerDelegate.errorLogger, "decryptedPassword failed", e);
203                                 throw e;
204                         }
205                 }
206                 return result;
207         }
208
209         private String encryptedPassword(String decryptedPwd) throws Exception {
210                 String result = "";
211                 if (decryptedPwd != null & decryptedPwd.length() > 0) {
212                         try {
213                                 result = CipherUtil.encryptPKC(decryptedPwd,
214                                                 SystemProperties.getProperty(SystemProperties.Decryption_Key));
215                         } catch (Exception e) {
216                                 logger.error(EELFLoggerDelegate.errorLogger, "encryptedPassword failed", e);
217                                 throw e;
218                         }
219                 }
220                 return result;
221         }
222
223         public DataAccessService getDataAccessService() {
224                 return dataAccessService;
225         }
226
227 }