Null check for ClientResponse in PolicyUril.java
[portal.git] / ecomp-portal-widget-ms / widget-ms / src / main / java / org / openecomp / portalapp / widget / service / impl / 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.widget.service.impl;
39
40 import java.util.List;
41
42 import javax.transaction.Transactional;
43
44 import org.hibernate.Criteria;
45 import org.hibernate.Session;
46 import org.hibernate.SessionFactory;
47 import org.hibernate.Transaction;
48 import org.hibernate.criterion.Restrictions;
49 import org.openecomp.portalapp.widget.domain.MicroserviceData;
50 import org.openecomp.portalapp.widget.domain.MicroserviceParameter;
51 import org.openecomp.portalapp.widget.service.MicroserviceService;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
54 import org.springframework.beans.factory.annotation.Autowired;
55 import org.springframework.context.annotation.EnableAspectJAutoProxy;
56 import org.springframework.stereotype.Service;
57
58 /**
59  * TODO: moved all microservice-related code (domain, controller, service)
60  * from ecomp portal Backend to widget microservice
61  */
62 @Service("microserviceService")
63 @Transactional
64 @org.springframework.context.annotation.Configuration
65 @EnableAspectJAutoProxy
66 public class MicroserviceServiceImpl implements MicroserviceService{
67
68         private static final Logger logger = LoggerFactory.getLogger(MicroserviceServiceImpl.class);
69         
70         @Autowired
71         private SessionFactory sessionFactory;
72         
73         @Override
74         public Long saveMicroserivce(MicroserviceData newService) {
75                 try{
76                         logger.debug("MicroserviceServiceImpl.saveMicroserivce: microservice={}", newService);
77                         Session session = sessionFactory.openSession();
78                         Transaction tx = session.beginTransaction();            
79                         session.save(newService);
80                         tx.commit();
81                         session.flush();
82                         session.close();
83                 }
84                 catch(Exception e){
85                         logger.error("Exception occurred while performing MicroserviceServiceImpl.saveMicroserivce in widget microservices. Details:" + e.getMessage());
86                 }
87                 return newService.getId();
88         }
89
90         @Override
91         public void saveMicroserviceParameter(MicroserviceParameter newParameter) {
92                 try{
93                         logger.debug("MicroserviceServiceImpl.saveMicroserviceData: microservice={}", newParameter);
94                         Session session = sessionFactory.openSession();
95                         Transaction tx = session.beginTransaction();            
96                         session.save(newParameter);
97                         tx.commit();
98                         session.flush();
99                         session.close();
100                 }
101                 catch(Exception e){
102                         logger.error("Exception occurred while performing MicroserviceServiceImpl.saveMicroserviceData in widget microservices. Details:" + e.getMessage());
103                 }
104         }
105         
106         @SuppressWarnings("unchecked")
107         @Override
108         public Long getMicroserviceIdByName(String newServiceName) {
109                 
110                 Session session = sessionFactory.openSession();
111                 Criteria criteria = session.createCriteria(MicroserviceData.class)
112                                 .add(Restrictions.eq("name", newServiceName))
113                                 .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
114                 
115                 List<MicroserviceData> services = criteria.list();
116                 logger.debug("MicroserviceServiceImpl.getMicroserviceByName: result={}", services);
117                 session.flush();
118                 session.close();
119                 
120                 return (services.size() > 0) ? services.get(0).getId() : null;
121         }
122         
123 }