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.util.Date;
41 import java.util.HashMap;
43 import java.util.UUID;
45 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
46 import org.onap.portalsdk.core.restful.domain.EcompUser;
47 import org.onap.portalsdk.core.service.DataAccessService;
48 import org.onap.portalsdk.rnotebookintegration.domain.RNoteBookCredentials;
49 import org.onap.portalsdk.rnotebookintegration.exception.RNotebookIntegrationException;
50 import org.springframework.beans.factory.annotation.Autowired;
51 import org.springframework.stereotype.Service;
52 import org.springframework.transaction.annotation.Transactional;
54 import com.fasterxml.jackson.core.JsonProcessingException;
55 import com.fasterxml.jackson.databind.ObjectMapper;
57 @Service("RNoteBookIntegrationService")
59 public class RNoteBookIntegrationServiceImpl implements RNoteBookIntegrationService {
61 private static final EELFLoggerDelegate logger = EELFLoggerDelegate
62 .getLogger(RNoteBookIntegrationServiceImpl.class);
64 private static final long TOKEN_TTL = 50000L;
67 private DataAccessService dataAccessService;
69 public DataAccessService getDataAccessService() {
70 return dataAccessService;
73 public void setDataAccessService(DataAccessService dataAccessService) {
74 this.dataAccessService = dataAccessService;
78 public String getRNotebookCredentials(String token) throws RNotebookIntegrationException {
79 String retString = "";
82 RNoteBookCredentials notebookCredentials = (RNoteBookCredentials) this.getDataAccessService()
83 .getDomainObject(RNoteBookCredentials.class, token, new HashMap<String, String>());
84 if (notebookCredentials.getToken() == null || "".equals(notebookCredentials.getToken())) {
85 throw new RNotebookIntegrationException(RNotebookIntegrationException.ERROR_CODE_TOKEN_INVALID);
87 Date currDate = new Date();
88 if ((currDate.getTime() - notebookCredentials.getCreatedDate().getTime() > TOKEN_TTL)
89 || (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 (JsonProcessingException me) {
98 logger.error(EELFLoggerDelegate.errorLogger, "error converting string to user. from JSON", me);
102 Map<String, String> params = mapper.readValue(notebookCredentials.getParametersString(), Map.class);
103 notebookCredentials.setParameters(params);
104 } catch (JsonProcessingException me) {
105 logger.error(EELFLoggerDelegate.errorLogger, "error converting string to parameters. from JSON", me);
108 // expiring the token
110 notebookCredentials.setTokenReadDate(new Date());
111 this.getDataAccessService().saveDomainObject(notebookCredentials, null);
112 } catch (Exception e) {
113 logger.error(EELFLoggerDelegate.errorLogger, "Error while expiring the token", e);
114 throw new RNotebookIntegrationException(e);
116 retString = mapper.writeValueAsString(notebookCredentials);
117 } catch (RNotebookIntegrationException re) {
118 logger.error(EELFLoggerDelegate.errorLogger, "getRNotebookCredentials failed", re);
120 } catch (Exception e) {
121 logger.error(EELFLoggerDelegate.errorLogger, "Error while parsing the rcloud notebook credentials", e);
122 throw new RNotebookIntegrationException(e);
129 public String saveRNotebookCredentials(String notebookId, EcompUser user, Map<String, String> params)
130 throws RNotebookIntegrationException {
134 token = UUID.randomUUID().toString();
135 ObjectMapper mapper = new ObjectMapper();
136 RNoteBookCredentials rc = new RNoteBookCredentials();
138 rc.setCreatedDate(new Date());
139 rc.setNotebookID(notebookId);
140 rc.setParametersString(mapper.writeValueAsString(params));
141 rc.setUserString(mapper.writeValueAsString(user));
142 this.getDataAccessService().saveDomainObject(rc, null);
143 } catch (Exception e) {
144 logger.error(EELFLoggerDelegate.errorLogger, "Error while parsing the rcloud notebook credentials", e);
145 throw new RNotebookIntegrationException(e);