43c0a95da3c4eeba2eb942b6208d56c0554adcaa
[policy/gui.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2020-2022 Nordix Foundation.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.gui.editors.apex.rest.handling;
23
24 import java.util.Map;
25 import java.util.TreeMap;
26 import java.util.concurrent.atomic.AtomicInteger;
27 import org.onap.policy.apex.model.modelapi.ApexApiResult;
28 import org.onap.policy.apex.model.modelapi.ApexApiResult.Result;
29 import org.slf4j.ext.XLogger;
30 import org.slf4j.ext.XLoggerFactory;
31 import org.springframework.stereotype.Service;
32
33 /**
34  * This class carries out session handling for Apex REST editor sessions.
35  */
36 @Service
37 public class RestSessionHandler {
38     // Get a reference to the logger
39     private static final XLogger LOGGER = XLoggerFactory.getXLogger(RestSessionHandler.class);
40
41     // The next session will have this number, stating at 0
42     private AtomicInteger nextSessionId = new AtomicInteger();
43
44     // All REST editor sessions being handled by this handler
45     private final Map<Integer, RestSession> sessionMap = new TreeMap<>();
46
47     /**
48      * Create a new session.
49      *
50      * @param result the result of session creation
51      * @return the new session object
52      */
53     public RestSession createSession(ApexApiResult result) {
54         LOGGER.entry("creating session");
55
56         // Create the session with the next session ID
57         final int newSessionId = nextSessionId.getAndIncrement();
58         sessionMap.put(newSessionId, new RestSession(newSessionId));
59
60         result.addMessage(Integer.toString(newSessionId));
61
62         LOGGER.exit("created session with ID: " + newSessionId);
63         return sessionMap.get(newSessionId);
64     }
65
66     /**
67      * Get a session for the given session ID.
68      *
69      * @param sessionId the session ID of the session we require
70      * @param result    the result of the session get
71      * @return the session
72      */
73     public RestSession getSession(final int sessionId, ApexApiResult result) {
74         LOGGER.entry("finding session: " + sessionId);
75
76         // Check for valid session IDs
77         if (sessionId < 0) {
78             result.setResult(Result.FAILED);
79             result.addMessage("Session ID  \"" + sessionId + "\" is negative");
80             LOGGER.exit(result.getMessage());
81             return null;
82         }
83
84         // Check if session exits
85         if (!sessionMap.containsKey(sessionId)) {
86             result.setResult(Result.FAILED);
87             result.addMessage("A session with session ID \"" + sessionId + "\" does not exist");
88             LOGGER.exit(result.getMessage());
89             return null;
90         }
91
92         RestSession session = sessionMap.get(sessionId);
93
94         // Check if session is valid
95         if (session == null) {
96             result.setResult(Result.FAILED);
97             result.addMessage("The session with session ID \"" + sessionId + "\" is corrupt");
98             LOGGER.exit(result.getMessage());
99             return null;
100         }
101
102         // Return the session
103         LOGGER.exit("session found: " + sessionId);
104         return session;
105     }
106
107     /*
108      * This is a test method to set a corrupt session ID in the session map
109      *
110      * @param corruptSessionId the ID of the corrupt session
111      */
112     protected void setCorruptSession(int corruptSessionId) {
113         sessionMap.put(corruptSessionId, null);
114     }
115 }