2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
26 * https://creativecommons.org/licenses/by/4.0/
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.
34 * ============LICENSE_END============================================
36 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
38 package org.onap.portalsdk.rnotebookintegration.service;
40 import java.security.SecureRandom;
41 import java.util.Date;
42 import java.util.HashMap;
44 import java.util.UUID;
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;
56 import com.fasterxml.jackson.core.JsonParseException;
57 import com.fasterxml.jackson.databind.JsonMappingException;
58 import com.fasterxml.jackson.databind.ObjectMapper;
60 @Service("RNoteBookIntegrationService")
62 public class RNoteBookIntegrationServiceImpl implements RNoteBookIntegrationService {
64 private final long tokenTTL = 50000L;
66 private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(RNoteBookIntegrationServiceImpl.class);
69 private DataAccessService dataAccessService;
71 public DataAccessService getDataAccessService() {
72 return dataAccessService;
75 public void setDataAccessService(DataAccessService dataAccessService) {
76 this.dataAccessService = dataAccessService;
80 public String getRNotebookCredentials(String token) throws RNotebookIntegrationException, Exception {
81 String retString = "";
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);
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);
92 ObjectMapper mapper = new ObjectMapper();
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());
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());
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();
121 //notebookCredentials.setUserString(null);
122 retString = mapper.writeValueAsString(notebookCredentials);
123 } catch(RNotebookIntegrationException re){
124 logger.error(re.getMessage());
126 } catch(Exception e){
127 logger.info("Error while parsing the rcloud notebook credentials");
128 logger.error(e.getMessage());
129 throw new Exception();
136 public String saveRNotebookCredentials(String notebookId, EcompUser user, Map<String, String> params) throws RNotebookIntegrationException, Exception {
140 token = UUID.randomUUID().toString();
142 ObjectMapper mapper = new ObjectMapper();
144 RNoteBookCredentials rc = new RNoteBookCredentials();
146 rc.setCreatedDate(new Date());
147 rc.setNotebookID(notebookId);
148 rc.setParametersString(mapper.writeValueAsString(params));
149 rc.setUserString(mapper.writeValueAsString(user));
151 this.getDataAccessService().saveDomainObject(rc, null);
153 } catch(Exception e){
154 logger.info("Error while parsing the rcloud notebook credentials");
155 logger.error(e.getMessage());
156 throw new Exception();