2 * ================================================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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 * ================================================================================
20 package org.openecomp.portalsdk.rnotebookintegration.service;
22 import java.security.SecureRandom;
23 import java.util.Date;
24 import java.util.HashMap;
26 import java.util.UUID;
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;
38 import com.fasterxml.jackson.core.JsonParseException;
39 import com.fasterxml.jackson.databind.JsonMappingException;
40 import com.fasterxml.jackson.databind.ObjectMapper;
42 @Service("RNoteBookIntegrationService")
44 public class RNoteBookIntegrationServiceImpl implements RNoteBookIntegrationService {
46 private final long tokenTTL = 50000L;
48 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(RNoteBookIntegrationServiceImpl.class);
52 private DataAccessService dataAccessService;
54 public DataAccessService getDataAccessService() {
55 return dataAccessService;
58 public void setDataAccessService(DataAccessService dataAccessService) {
59 this.dataAccessService = dataAccessService;
63 public String getRNotebookCredentials(String token) throws RNotebookIntegrationException, Exception {
64 String retString = "";
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);
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);
75 ObjectMapper mapper = new ObjectMapper();
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());
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());
97 notebookCredentials.setTokenReadDate(new Date());
98 this.getDataAccessService().saveDomainObject(notebookCredentials, null);
100 logger.info("Error while expiring the token");
101 logger.error(e.getMessage());
102 throw new Exception();
104 //notebookCredentials.setUserString(null);
105 retString = mapper.writeValueAsString(notebookCredentials);
106 } catch(RNotebookIntegrationException re){
107 logger.error(re.getMessage());
109 } catch(Exception e){
110 logger.info("Error while parsing the rcloud notebook credentials");
111 logger.error(e.getMessage());
112 throw new Exception();
119 public String saveRNotebookCredentials(String notebookId, EcompUser user, Map<String, String> params) throws RNotebookIntegrationException, Exception {
123 token = UUID.randomUUID().toString();
125 ObjectMapper mapper = new ObjectMapper();
127 RNoteBookCredentials rc = new RNoteBookCredentials();
129 rc.setCreatedDate(new Date());
130 rc.setNotebookID(notebookId);
131 rc.setParametersString(mapper.writeValueAsString(params));
132 rc.setUserString(mapper.writeValueAsString(user));
134 this.getDataAccessService().saveDomainObject(rc, null);
136 } catch(Exception e){
137 logger.info("Error while parsing the rcloud notebook credentials");
138 logger.error(e.getMessage());
139 throw new Exception();