Java 17 Upgrade
[policy/models.git] / models-base / src / main / java / org / onap / policy / models / base / PfModelService.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
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.models.base;
23
24 import jakarta.ws.rs.core.Response;
25 import java.util.Map;
26 import java.util.concurrent.ConcurrentHashMap;
27 import lombok.AccessLevel;
28 import lombok.NoArgsConstructor;
29 import lombok.NonNull;
30
31 /**
32  * The model service makes Policy Framework models available to all classes in a JVM.
33  *
34  * <p>The reason for having a model service is to avoid having to pass concept and model definitions down long call
35  * chains in modules such as the Policy Framework engine and editor. The model service makes the model and concept
36  * definitions available statically.
37  *
38  * <p>Note that the use of the model service means that only a single Policy Framework model of a particular type may
39  * exist in Policy Framework (particularly the engine) at any time. Of course the model in a JVM can be changed at any
40  * time provided all users of the model are stopped and restarted in an orderly manner.
41  */
42 @NoArgsConstructor(access = AccessLevel.PRIVATE)
43 public final class PfModelService {
44     // The map holding the models
45     private static final Map<String, PfConcept> modelMap = new ConcurrentHashMap<>();
46
47     /**
48      * Register a model with the model service.
49      *
50      * @param <M>      the generic type
51      * @param modelKey the key of the model, used to index the model
52      * @param model    The model
53      */
54     public static <M extends PfConcept> void registerModel(@NonNull final String modelKey, @NonNull final M model) {
55         modelMap.put(modelKey, model);
56     }
57
58     /**
59      * Remove a model from the model service.
60      *
61      * @param modelKey the key of the model, used to index the model
62      */
63     public static void deregisterModel(@NonNull final String modelKey) {
64         modelMap.remove(modelKey);
65     }
66
67     /**
68      * Get a model from the model service.
69      *
70      * @param <M>      the generic type
71      * @param modelKey the key of the model, used to index the model
72      * @return The model
73      */
74     @SuppressWarnings("unchecked")
75     public static <M extends PfConcept> M getModel(@NonNull final String modelKey) {
76         final var model = (M) modelMap.get(modelKey);
77
78         if (model == null) {
79             throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR,
80                 "Model for name " + modelKey + " not found in model service");
81         }
82
83         return model;
84     }
85
86     /**
87      * Check if a model is defined on the model service.
88      *
89      * @param modelKey the key of the model, used to index the model
90      * @return true if the model is defined
91      */
92     public static boolean existsModel(final String modelKey) {
93         return modelMap.get(modelKey) != null;
94     }
95
96     /**
97      * Clear all models in the model service.
98      */
99     public static void clear() {
100         modelMap.clear();
101     }
102 }