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;
26 import org.onap.policy.apex.model.basicmodel.concepts.ApexRuntimeException;
27 import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
30 * The model service makes Apex models available to all classes in a JVM.
32 * <p>The reason for having a model service is to avoid having to pass concept and model definitions down long call
33 * chains in modules such as the Apex engine and editor. The model service makes the model and concept definitions
34 * available statically.
36 * <p>Note that the use of the model service means that only a single Apex model of a particular type may exist in Apex
37 * (particularly the engine) at any time. Of course the model in a JVM can be changed at any time provided all users of
38 * the model are stopped and restarted in an orderly manner.
40 * @author Liam Fallon (liam.fallon@ericsson.com)
42 public abstract class ModelService {
43 // The map holding the models
44 private static Map<Class<?>, AxConcept> modelMap = new ConcurrentHashMap<>();
47 * This class is an abstract static class that cannot be extended.
49 private ModelService() {
53 * Register a model with the model service.
55 * @param <M> the generic type
56 * @param modelClass the class of the model, used to index the model
57 * @param model The model
59 public static <M extends AxConcept> void registerModel(final Class<M> modelClass, final M model) {
60 modelMap.put(modelClass, model);
64 * Remove a model from the model service.
66 * @param <M> the generic type
67 * @param modelClass the class of the model, used to index the model
69 public static <M extends AxConcept> void deregisterModel(final Class<M> modelClass) {
70 modelMap.remove(modelClass);
74 * Get a model from the model service.
76 * @param <M> the generic type
77 * @param modelClass the class of the model, used to index the model
80 @SuppressWarnings("unchecked")
81 public static <M extends AxConcept> M getModel(final Class<M> modelClass) {
82 final M model = (M) modelMap.get(modelClass);
85 throw new ApexRuntimeException("Model for " + modelClass.getName() + " not found in model service");
92 * Check if a model is defined on the model service.
94 * @param <M> the generic type
95 * @param modelClass the class of the model, used to index the model
96 * @return true if the model is defined
98 public static <M extends AxConcept> boolean existsModel(final Class<M> modelClass) {
99 return modelMap.get(modelClass) != null;
103 * Clear all models in the model service.
105 public static void clear() {