251d92af5ad9f2c30472f2cf335e6b56dfe341bd
[policy/apex-pdp.git] / model / basic-model / src / main / java / org / onap / policy / apex / model / basicmodel / service / ModelService.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2019 Nordix Foundation.
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.apex.model.basicmodel.service;
23
24 import java.util.Map;
25 import java.util.concurrent.ConcurrentHashMap;
26
27 import org.onap.policy.apex.model.basicmodel.concepts.ApexRuntimeException;
28 import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
29
30 /**
31  * The model service makes Apex 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 down long call
34  * chains in modules such as the Apex engine and editor. The model service makes the model and concept definitions
35  * available statically.
36  *
37  * <p>Note that the use of the model service means that only a single Apex model of a particular type may exist in Apex
38  * (particularly the engine) at any time. Of course the model in a JVM can be changed at any time provided all users of
39  * the model are stopped and restarted in an orderly manner.
40  *
41  * @author Liam Fallon (liam.fallon@ericsson.com)
42  */
43 public abstract class ModelService {
44     // The map holding the models
45     private static Map<Class<?>, AxConcept> modelMap = new ConcurrentHashMap<>();
46
47     /**
48      * This class is an abstract static class that cannot be extended.
49      */
50     private ModelService() {
51     }
52
53     /**
54      * Register a model with the model service.
55      *
56      * @param <M> the generic type
57      * @param modelClass the class of the model, used to index the model
58      * @param model The model
59      */
60     public static <M extends AxConcept> void registerModel(final Class<M> modelClass, final M model) {
61         modelMap.put(modelClass, model);
62     }
63
64     /**
65      * Remove a model from the model service.
66      *
67      * @param <M> the generic type
68      * @param modelClass the class of the model, used to index the model
69      */
70     public static <M extends AxConcept> void deregisterModel(final Class<M> modelClass) {
71         modelMap.remove(modelClass);
72     }
73
74     /**
75      * Get a model from the model service.
76      *
77      * @param <M> the generic type
78      * @param modelClass the class of the model, used to index the model
79      * @return The model
80      */
81     @SuppressWarnings("unchecked")
82     public static <M extends AxConcept> M getModel(final Class<M> modelClass) {
83         final M model = (M) modelMap.get(modelClass);
84
85         if (model == null) {
86             throw new ApexRuntimeException("Model for " + modelClass.getName() + " not found in model service");
87         }
88
89         return model;
90     }
91
92     /**
93      * Check if a model is defined on the model service.
94      *
95      * @param <M> the generic type
96      * @param modelClass the class of the model, used to index the model
97      * @return true if the model is defined
98      */
99     public static <M extends AxConcept> boolean existsModel(final Class<M> modelClass) {
100         return modelMap.get(modelClass) != null;
101     }
102
103     /**
104      * Clear all models in the model service.
105      */
106     public static void clear() {
107         modelMap.clear();
108     }
109 }