fe21cd1304bd5009c9ed51c73c0b692545c99033
[portal/sdk.git] /
1 /*-
2  * ================================================================================
3  * eCOMP Portal SDK
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ================================================================================
19  */
20 package org.openecomp.portalsdk.rnotebookintegration.service;
21
22 import java.security.SecureRandom;
23 import java.util.Date;
24 import java.util.HashMap;
25 import java.util.Map;
26 import java.util.UUID;
27
28 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
29 import org.openecomp.portalsdk.core.restful.domain.EcompUser;
30 import org.openecomp.portalsdk.core.service.DataAccessService;
31 import org.openecomp.portalsdk.core.web.support.UserUtils;
32 import org.openecomp.portalsdk.rnotebookintegration.domain.RNoteBookCredentials;
33 import org.openecomp.portalsdk.rnotebookintegration.exception.RNotebookIntegrationException;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.stereotype.Service;
36 import org.springframework.transaction.annotation.Transactional;
37
38 import com.fasterxml.jackson.core.JsonParseException;
39 import com.fasterxml.jackson.databind.JsonMappingException;
40 import com.fasterxml.jackson.databind.ObjectMapper;
41
42 @Service("RNoteBookIntegrationService")
43 @Transactional
44 public class RNoteBookIntegrationServiceImpl implements RNoteBookIntegrationService {
45         
46         private final long tokenTTL = 50000L;
47         
48         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(RNoteBookIntegrationServiceImpl.class);
49         
50         
51         @Autowired
52         private DataAccessService  dataAccessService;
53         
54         public DataAccessService getDataAccessService() {
55                 return dataAccessService;
56         }
57
58         public void setDataAccessService(DataAccessService dataAccessService) {
59                 this.dataAccessService = dataAccessService;
60         }
61         
62         @Override
63         public String getRNotebookCredentials(String token) throws RNotebookIntegrationException, Exception {
64                 String retString = "";
65                 
66                 try{
67                         RNoteBookCredentials notebookCredentials = (RNoteBookCredentials) this.getDataAccessService().getDomainObject(RNoteBookCredentials.class, token, new HashMap<String, String>());
68                         if (notebookCredentials.getToken() == null || notebookCredentials.getToken().equals("")){
69                                 throw new RNotebookIntegrationException(RNotebookIntegrationException.ERROR_CODE_TOKEN_INVALID);
70                         }
71                         Date currDate = new Date();
72                         if ((currDate.getTime() - notebookCredentials.getCreatedDate().getTime() > tokenTTL) || (notebookCredentials.getTokenReadDate() != null)){
73                                 throw new RNotebookIntegrationException(RNotebookIntegrationException.ERROR_CODE_TOKEN_EXPIRED);
74                         }
75                         ObjectMapper mapper = new ObjectMapper();
76                         
77                         try{
78                                 EcompUser userInfo = mapper.readValue(notebookCredentials.getUserString(), EcompUser.class);
79                                 notebookCredentials.setUserInfo(userInfo);                      
80                         } catch(JsonMappingException me){
81                                 logger.error("error converting string to user. from JSON" + me.getMessage());
82                         } catch(JsonParseException pe){
83                                 logger.error("error converting string to user. from JSON" + pe.getMessage());
84                         }
85                         
86                         try{
87                                 Map<String, String> params = mapper.readValue(notebookCredentials.getParametersString(), HashMap.class);
88                                 notebookCredentials.setParameters(params);
89                         } catch(JsonMappingException me){
90                                 logger.error("error converting string to parameters. from JSON" + me.getMessage());
91                         } catch(JsonParseException pe){
92                                 logger.error("error converting string to parameters. from JSON" + pe.getMessage());
93                         }
94                         
95                         //expiring the token
96                         try{
97                                 notebookCredentials.setTokenReadDate(new Date());
98                                 this.getDataAccessService().saveDomainObject(notebookCredentials, null);
99                         } catch(Exception e){
100                                 logger.info("Error while expiring the token");
101                                 logger.error(e.getMessage());
102                                 throw new Exception();
103                         }
104                         //notebookCredentials.setUserString(null);
105                         retString = mapper.writeValueAsString(notebookCredentials);
106                 } catch(RNotebookIntegrationException re){
107                         logger.error(re.getMessage());
108                         throw re;
109                 } catch(Exception e){
110                         logger.info("Error while parsing the rcloud notebook credentials");
111                         logger.error(e.getMessage());
112                         throw new Exception();
113                 }
114                 
115                 return  retString;
116         }
117         
118         @Override
119         public String saveRNotebookCredentials(String notebookId, EcompUser user, Map<String, String> params) throws RNotebookIntegrationException, Exception {
120                 
121                 String token = "";
122                 try{
123                         token = UUID.randomUUID().toString();
124                         
125                         ObjectMapper mapper = new ObjectMapper();
126                         ;
127                         RNoteBookCredentials rc = new RNoteBookCredentials();
128                         rc.setToken(token);
129                         rc.setCreatedDate(new Date());
130                         rc.setNotebookID(notebookId);
131                         rc.setParametersString(mapper.writeValueAsString(params));
132                         rc.setUserString(mapper.writeValueAsString(user));
133                         
134                         this.getDataAccessService().saveDomainObject(rc, null);
135                         
136                 } catch(Exception e){
137                         logger.info("Error while parsing the rcloud notebook credentials");
138                         logger.error(e.getMessage());
139                         throw new Exception();
140                 }
141                 
142                 return  token;
143         }
144
145
146 }