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