8a21b71a9d255d1868c8432e29461085d5bb41d9
[portal/sdk.git] /
1 /*
2  * ============LICENSE_START==========================================
3  * ONAP Portal SDK
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.onap.portalsdk.rnotebookintegration.service;
39
40 import java.security.SecureRandom;
41 import java.util.Date;
42 import java.util.HashMap;
43 import java.util.Map;
44 import java.util.UUID;
45
46 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
47 import org.onap.portalsdk.core.restful.domain.EcompUser;
48 import org.onap.portalsdk.core.service.DataAccessService;
49 import org.onap.portalsdk.core.web.support.UserUtils;
50 import org.onap.portalsdk.rnotebookintegration.domain.RNoteBookCredentials;
51 import org.onap.portalsdk.rnotebookintegration.exception.RNotebookIntegrationException;
52 import org.springframework.beans.factory.annotation.Autowired;
53 import org.springframework.stereotype.Service;
54 import org.springframework.transaction.annotation.Transactional;
55
56 import com.fasterxml.jackson.core.JsonParseException;
57 import com.fasterxml.jackson.databind.JsonMappingException;
58 import com.fasterxml.jackson.databind.ObjectMapper;
59
60 @Service("RNoteBookIntegrationService")
61 @Transactional
62 public class RNoteBookIntegrationServiceImpl implements RNoteBookIntegrationService {
63         
64         private final long tokenTTL = 50000L;
65         
66         private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(RNoteBookIntegrationServiceImpl.class);
67         
68         @Autowired
69         private DataAccessService  dataAccessService;
70         
71         public DataAccessService getDataAccessService() {
72                 return dataAccessService;
73         }
74
75         public void setDataAccessService(DataAccessService dataAccessService) {
76                 this.dataAccessService = dataAccessService;
77         }
78         
79         @Override
80         public String getRNotebookCredentials(String token) throws RNotebookIntegrationException, Exception {
81                 String retString = "";
82                 
83                 try{
84                         RNoteBookCredentials notebookCredentials = (RNoteBookCredentials) this.getDataAccessService().getDomainObject(RNoteBookCredentials.class, token, new HashMap<String, String>());
85                         if (notebookCredentials.getToken() == null || notebookCredentials.getToken().equals("")){
86                                 throw new RNotebookIntegrationException(RNotebookIntegrationException.ERROR_CODE_TOKEN_INVALID);
87                         }
88                         Date currDate = new Date();
89                         if ((currDate.getTime() - notebookCredentials.getCreatedDate().getTime() > tokenTTL) || (notebookCredentials.getTokenReadDate() != null)){
90                                 throw new RNotebookIntegrationException(RNotebookIntegrationException.ERROR_CODE_TOKEN_EXPIRED);
91                         }
92                         ObjectMapper mapper = new ObjectMapper();
93                         
94                         try{
95                                 EcompUser userInfo = mapper.readValue(notebookCredentials.getUserString(), EcompUser.class);
96                                 notebookCredentials.setUserInfo(userInfo);                      
97                         } catch(JsonMappingException me){
98                                 logger.error("error converting string to user. from JSON" + me.getMessage());
99                         } catch(JsonParseException pe){
100                                 logger.error("error converting string to user. from JSON" + pe.getMessage());
101                         }
102                         
103                         try{
104                                 Map<String, String> params = mapper.readValue(notebookCredentials.getParametersString(), HashMap.class);
105                                 notebookCredentials.setParameters(params);
106                         } catch(JsonMappingException me){
107                                 logger.error("error converting string to parameters. from JSON" + me.getMessage());
108                         } catch(JsonParseException pe){
109                                 logger.error("error converting string to parameters. from JSON" + pe.getMessage());
110                         }
111                         
112                         //expiring the token
113                         try{
114                                 notebookCredentials.setTokenReadDate(new Date());
115                                 this.getDataAccessService().saveDomainObject(notebookCredentials, null);
116                         } catch(Exception e){
117                                 logger.info("Error while expiring the token");
118                                 logger.error(e.getMessage());
119                                 throw new Exception();
120                         }
121                         //notebookCredentials.setUserString(null);
122                         retString = mapper.writeValueAsString(notebookCredentials);
123                 } catch(RNotebookIntegrationException re){
124                         logger.error(re.getMessage());
125                         throw re;
126                 } catch(Exception e){
127                         logger.info("Error while parsing the rcloud notebook credentials");
128                         logger.error(e.getMessage());
129                         throw new Exception();
130                 }
131                 
132                 return  retString;
133         }
134         
135         @Override
136         public String saveRNotebookCredentials(String notebookId, EcompUser user, Map<String, String> params) throws RNotebookIntegrationException, Exception {
137                 
138                 String token = "";
139                 try{
140                         token = UUID.randomUUID().toString();
141                         
142                         ObjectMapper mapper = new ObjectMapper();
143                         ;
144                         RNoteBookCredentials rc = new RNoteBookCredentials();
145                         rc.setToken(token);
146                         rc.setCreatedDate(new Date());
147                         rc.setNotebookID(notebookId);
148                         rc.setParametersString(mapper.writeValueAsString(params));
149                         rc.setUserString(mapper.writeValueAsString(user));
150                         
151                         this.getDataAccessService().saveDomainObject(rc, null);
152                         
153                 } catch(Exception e){
154                         logger.info("Error while parsing the rcloud notebook credentials");
155                         logger.error(e.getMessage());
156                         throw new Exception();
157                 }
158                 
159                 return  token;
160         }
161
162
163 }