2afd5bd71e91b278d5783f5908ecd281b75ca7f2
[policy/gui.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2020 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
32 /**
33  * This class carries out session handling for Apex REST editor sessions.
34  */
35 public class RestSessionHandler {
36     // Get a reference to the logger
37     private static final XLogger LOGGER = XLoggerFactory.getXLogger(RestSessionHandler.class);
38
39     // The next session will have this number, stating at 0
40     private AtomicInteger nextSessionId = new AtomicInteger();
41
42     // All REST editor sessions being handled by this handler
43     private final Map<Integer, RestSession> sessionMap = new TreeMap<>();
44
45     /**
46      * Create a new session.
47      *
48      * @param result the result of session creation
49      * @return the new session object
50      */
51     public RestSession createSession(ApexApiResult result) {
52         LOGGER.entry("creating session");
53
54         // Create the session with the next session ID
55         final int newSessionId = nextSessionId.getAndIncrement();
56         sessionMap.put(newSessionId, new RestSession(newSessionId));
57
58         result.addMessage(Integer.toString(newSessionId));
59
60         LOGGER.exit("created session with ID: " + newSessionId);
61         return sessionMap.get(newSessionId);
62     }
63
64     /**
65      * Get a session for the given session ID.
66      *
67      * @param sessionId the session ID of the session we require
68      * @param result    the result of the session get
69      * @return the session
70      */
71     public RestSession getSession(final int sessionId, ApexApiResult result) {
72         LOGGER.entry("finding session: " + sessionId);
73
74         // Check for valid session IDs
75         if (sessionId < 0) {
76             result.setResult(Result.FAILED);
77             result.addMessage("Session ID  \"" + sessionId + "\" is negative");
78             LOGGER.exit(result.getMessage());
79             return null;
80         }
81
82         // Check if session exits
83         if (!sessionMap.containsKey(sessionId)) {
84             result.setResult(Result.FAILED);
85             result.addMessage("A session with session ID \"" + sessionId + "\" does not exist");
86             LOGGER.exit(result.getMessage());
87             return null;
88         }
89
90         RestSession session = sessionMap.get(sessionId);
91
92         // Check if session is valid
93         if (session == null) {
94             result.setResult(Result.FAILED);
95             result.addMessage("The session with session ID \"" + sessionId + "\" is corrupt");
96             LOGGER.exit(result.getMessage());
97             return null;
98         }
99
100         // Return the session
101         LOGGER.exit("session found: " + sessionId);
102         return session;
103     }
104
105     /*
106      * This is a test method to set a corrupt session ID in the session map
107      *
108      * @param corruptSessionId the ID of the corrupt session
109      */
110     protected void setCorruptSession(int corruptSessionId) {
111         sessionMap.put(corruptSessionId, null);
112     }
113 }