b717944ed5238055806cb5a1c475dad4d260eb64
[policy/apex-pdp.git] / client / client-editor / src / main / java / org / onap / policy / apex / client / editor / rest / handling / RestSessionHandler.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.client.editor.rest.handling;
22
23 import java.util.Map;
24 import java.util.TreeMap;
25 import java.util.concurrent.atomic.AtomicInteger;
26
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      * @param result the result of session creation
48      * @return the new session object
49      */
50     public RestSession createSession(ApexApiResult result) {
51         LOGGER.entry("creating session");
52         
53         // Create the session with the next session ID
54         final int newSessionId = nextSessionId.getAndIncrement();
55         sessionMap.put(newSessionId, new RestSession(newSessionId));
56
57         result.addMessage(Integer.toString(newSessionId));
58         
59         LOGGER.exit("created session with ID: " + newSessionId);
60         return sessionMap.get(newSessionId);
61     }
62
63     /**
64      * Get a session for the given session ID.
65      * @param sessionId the session ID of the session we require
66      * @param result the result of the session get
67      * @return the session
68      */
69     public RestSession getSession(final int sessionId, ApexApiResult result) {
70         LOGGER.entry("finding session: " + sessionId);
71
72         // Check for valid session IDs
73         if (sessionId < 0) {
74             result.setResult(Result.FAILED);
75             result.addMessage("Session ID  \"" + sessionId + "\" is negative");
76             LOGGER.exit(result.getMessage());
77             return null;
78         }
79
80         // Check if session exits
81         if (!sessionMap.containsKey(sessionId)) {
82             result.setResult(Result.FAILED);
83             result.addMessage("A session with session ID \"" + sessionId + "\" does not exist");
84             LOGGER.exit(result.getMessage());
85             return null;
86         }
87         
88         RestSession session = sessionMap.get(sessionId);
89         
90         // Check if session is valid
91         if (session == null) {
92             result.setResult(Result.FAILED);
93             result.addMessage("The session with session ID \"" + sessionId + "\" is corrupt");
94             LOGGER.exit(result.getMessage());
95             return null;
96         }
97         
98         // Return the session
99         LOGGER.exit("session found: " + sessionId);
100         return session;
101     }
102
103     /*
104      * This is a test method to set a corrupt session ID in the session map
105      * @param corruptSessionId the ID of the corrupt session
106      */
107     protected void setCorruptSession(int corruptSessionId) {
108         sessionMap.put(corruptSessionId, null);
109     }
110 }