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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.model.basicmodel.service;
25 import java.util.concurrent.ConcurrentHashMap;
27 import org.onap.policy.apex.model.basicmodel.concepts.ApexRuntimeException;
28 import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
31 * The model service makes Apex models available to all classes in a JVM.
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.
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.
41 * @author Liam Fallon (liam.fallon@ericsson.com)
43 public abstract class ModelService {
44 // The map holding the models
45 private static Map<Class<?>, AxConcept> modelMap = new ConcurrentHashMap<>();
48 * This class is an abstract static class that cannot be extended.
50 private ModelService() {
54 * Register a model with the model service.
56 * @param <M> the generic type
57 * @param modelClass the class of the model, used to index the model
58 * @param model The model
60 public static <M extends AxConcept> void registerModel(final Class<M> modelClass, final M model) {
61 modelMap.put(modelClass, model);
65 * Remove a model from the model service.
67 * @param <M> the generic type
68 * @param modelClass the class of the model, used to index the model
70 public static <M extends AxConcept> void deregisterModel(final Class<M> modelClass) {
71 modelMap.remove(modelClass);
75 * Get a model from the model service.
77 * @param <M> the generic type
78 * @param modelClass the class of the model, used to index the model
81 @SuppressWarnings("unchecked")
82 public static <M extends AxConcept> M getModel(final Class<M> modelClass) {
83 final M model = (M) modelMap.get(modelClass);
86 throw new ApexRuntimeException("Model for " + modelClass.getName() + " not found in model service");
93 * Check if a model is defined on the model service.
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
99 public static <M extends AxConcept> boolean existsModel(final Class<M> modelClass) {
100 return modelMap.get(modelClass) != null;
104 * Clear all models in the model service.
106 public static void clear() {