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