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