bb8e362756020fc0d0fdb38fe66b6c2bbb0eee93
[policy/apex-pdp.git] / core / core-engine / src / main / java / org / onap / policy / apex / core / engine / executor / context / AxStateFacade.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
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.apex.core.engine.executor.context;
22
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.List;
26 import java.util.Set;
27
28 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
29 import org.onap.policy.apex.model.basicmodel.service.ModelService;
30 import org.onap.policy.apex.model.policymodel.concepts.AxState;
31 import org.onap.policy.apex.model.policymodel.concepts.AxTasks;
32
33 /**
34  * The Class AxStateFacade acts as a facade into the AxState class so that task logic can easily
35  * access information in an AxState instance.
36  *
37  * @author Sven van der Meer (sven.van.der.meer@ericsson.com)
38  */
39 public class AxStateFacade {
40     // CHECKSTYLE:OFF: checkstyle:visibilityModifier Logic has access to this field
41
42     /** The full definition information for the state. */
43     public final AxState state;
44
45     // CHECKSTYLE:ON: checkstyle:visibilityModifier
46
47     /**
48      * Instantiates a new AxState facade.
49      *
50      * @param state the state for which a facade is being presented
51      */
52     public AxStateFacade(final AxState state) {
53         this.state = state;
54     }
55
56     /**
57      * Gets the default task key of the state.
58      *
59      * @return the default task key
60      */
61     public AxArtifactKey getDefaultTaskKey() {
62         return state.getDefaultTask();
63     }
64
65     /**
66      * Gets the ID of the state.
67      *
68      * @return the ID
69      */
70     public String getId() {
71         return state.getKey().getId();
72     }
73
74     /**
75      * Gets the name of the state.
76      *
77      * @return the state name
78      */
79     public String getStateName() {
80         return state.getKey().getLocalName();
81     }
82
83     /**
84      * Check if a task is defined for a given task name on a state and, if so, return its key.
85      *
86      * @param taskName the name of the task to get
87      * @return the task key or null if it does not exist
88      */
89     public AxArtifactKey getTaskKey(final String taskName) {
90         if (taskName == null) {
91             return null;
92         }
93
94         return ModelService.getModel(AxTasks.class).get(taskName).getKey();
95     }
96
97     /**
98      * Check if a task is defined for a given task name on a state and, if so, return its key.
99      *
100      * @return unmodifiable list of names of tasks available
101      */
102     public List<String> getTaskNames() {
103         final Set<AxArtifactKey> tasks = state.getTaskReferences().keySet();
104         final List<String> ret = new ArrayList<>(tasks.size());
105         for (final AxArtifactKey task : tasks) {
106             ret.add(task.getName());
107         }
108         return Collections.unmodifiableList(ret);
109     }
110 }