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