bac2370693b8e54bc43b8b80d3b17f47c3bad52b
[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.util.Date;
41 import java.util.HashMap;
42 import java.util.Map;
43 import java.util.UUID;
44
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;
53
54 import com.fasterxml.jackson.core.JsonProcessingException;
55 import com.fasterxml.jackson.databind.ObjectMapper;
56
57 @Service("RNoteBookIntegrationService")
58 @Transactional
59 public class RNoteBookIntegrationServiceImpl implements RNoteBookIntegrationService {
60
61         private static final EELFLoggerDelegate logger = EELFLoggerDelegate
62                         .getLogger(RNoteBookIntegrationServiceImpl.class);
63
64         private static final long TOKEN_TTL = 50000L;
65
66         @Autowired
67         private DataAccessService dataAccessService;
68
69         public DataAccessService getDataAccessService() {
70                 return dataAccessService;
71         }
72
73         public void setDataAccessService(DataAccessService dataAccessService) {
74                 this.dataAccessService = dataAccessService;
75         }
76
77         @Override
78         public String getRNotebookCredentials(String token) throws RNotebookIntegrationException {
79                 String retString = "";
80
81                 try {
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);
86                         }
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);
91                         }
92                         ObjectMapper mapper = new ObjectMapper();
93
94                         try {
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);
99                         }
100
101                         try {
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);
106                         }
107
108                         // expiring the token
109                         try {
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);
115                         }
116                         retString = mapper.writeValueAsString(notebookCredentials);
117                 } catch (RNotebookIntegrationException re) {
118                         logger.error(EELFLoggerDelegate.errorLogger, "getRNotebookCredentials failed", re);
119                         throw re;
120                 } catch (Exception e) {
121                         logger.error(EELFLoggerDelegate.errorLogger, "Error while parsing the rcloud notebook credentials", e);
122                         throw new RNotebookIntegrationException(e);
123                 }
124
125                 return retString;
126         }
127
128         @Override
129         public String saveRNotebookCredentials(String notebookId, EcompUser user, Map<String, String> params)
130                         throws RNotebookIntegrationException {
131
132                 String token = "";
133                 try {
134                         token = UUID.randomUUID().toString();
135                         ObjectMapper mapper = new ObjectMapper();
136                         RNoteBookCredentials rc = new RNoteBookCredentials();
137                         rc.setToken(token);
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);
146                 }
147                 return token;
148         }
149
150 }