2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
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.models.base;
25 import java.util.concurrent.ConcurrentHashMap;
26 import javax.ws.rs.core.Response;
27 import lombok.NonNull;
30 * The model service makes Policy Framework 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 Policy Framework engine and editor. The model service makes the model and concept
34 * definitions available statically.
36 * <p>Note that the use of the model service means that only a single Policy Framework model of a particular type may
37 * exist in Policy Framework (particularly the engine) at any time. Of course the model in a JVM can be changed at any
38 * time provided all users of the model are stopped and restarted in an orderly manner.
40 public abstract class PfModelService {
41 // The map holding the models
42 private static Map<String, PfConcept> modelMap = new ConcurrentHashMap<>();
45 * This class is an abstract static class that cannot be extended.
47 private PfModelService() {
48 // Default constructor
52 * Register a model with the model service.
54 * @param <M> the generic type
55 * @param modelKey the key of the model, used to index the model
56 * @param model The model
58 public static <M extends PfConcept> void registerModel(@NonNull final String modelKey, @NonNull final M model) {
59 modelMap.put(modelKey, model);
63 * Remove a model from the model service.
65 * @param modelKey the key of the model, used to index the model
67 public static void deregisterModel(@NonNull final String modelKey) {
68 modelMap.remove(modelKey);
72 * Get a model from the model service.
74 * @param <M> the generic type
75 * @param modelKey the key of the model, used to index the model
78 @SuppressWarnings("unchecked")
79 public static <M extends PfConcept> M getModel(@NonNull final String modelKey) {
80 final var model = (M) modelMap.get(modelKey);
83 throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR,
84 "Model for name " + modelKey + " not found in model service");
91 * Check if a model is defined on the model service.
93 * @param modelKey the key of the model, used to index the model
94 * @return true if the model is defined
96 public static boolean existsModel(final String modelKey) {
97 return modelMap.get(modelKey) != null;
101 * Clear all models in the model service.
103 public static void clear() {