2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2021 Nordix Foundation.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.services.onappf.handler;
23 import java.time.Instant;
24 import java.util.ArrayList;
25 import java.util.List;
26 import lombok.NonNull;
27 import org.onap.policy.apex.model.enginemodel.concepts.AxEngineModel;
28 import org.onap.policy.apex.model.enginemodel.concepts.AxEngineState;
29 import org.onap.policy.apex.service.engine.main.ApexPolicyStatisticsManager;
30 import org.onap.policy.apex.services.onappf.ApexStarterConstants;
31 import org.onap.policy.apex.services.onappf.parameters.PdpStatusParameters;
32 import org.onap.policy.apex.services.onappf.parameters.ToscaPolicyTypeIdentifierParameters;
33 import org.onap.policy.common.utils.services.Registry;
34 import org.onap.policy.models.pdp.concepts.PdpEngineWorkerStatistics;
35 import org.onap.policy.models.pdp.concepts.PdpResponseDetails;
36 import org.onap.policy.models.pdp.concepts.PdpStatistics;
37 import org.onap.policy.models.pdp.concepts.PdpStatus;
38 import org.onap.policy.models.pdp.enums.PdpEngineWorkerState;
39 import org.onap.policy.models.pdp.enums.PdpHealthStatus;
40 import org.onap.policy.models.pdp.enums.PdpResponseStatus;
41 import org.onap.policy.models.pdp.enums.PdpState;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
49 * This class supports the handling of pdp messages.
51 * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
53 public class PdpMessageHandler {
54 private static final Logger LOGGER = LoggerFactory.getLogger(PdpMessageHandler.class);
57 * Method to create PdpStatus message from the parameters which will be saved to the context.
59 * @param instanceId instance id of apex pdp
60 * @param pdpStatusParameters pdp status parameters read from the configuration file
62 * @return pdpStatus the pdp status message
64 public PdpStatus createPdpStatusFromParameters(final String instanceId,
65 final PdpStatusParameters pdpStatusParameters) {
66 final PdpStatus pdpStatus = new PdpStatus();
67 pdpStatus.setPdpGroup(pdpStatusParameters.getPdpGroup());
68 pdpStatus.setPdpType(pdpStatusParameters.getPdpType());
69 pdpStatus.setState(PdpState.PASSIVE);
70 pdpStatus.setHealthy(PdpHealthStatus.HEALTHY);
71 pdpStatus.setDescription(pdpStatusParameters.getDescription());
72 pdpStatus.setName(instanceId);
77 * Method to get supported policy types from the parameters.
79 * @param pdpStatusParameters pdp status parameters
80 * @return supportedPolicyTypes list of PolicyTypeIdent
82 public List<ToscaConceptIdentifier> getSupportedPolicyTypesFromParameters(
83 final PdpStatusParameters pdpStatusParameters) {
84 final List<ToscaConceptIdentifier> supportedPolicyTypes =
85 new ArrayList<>(pdpStatusParameters.getSupportedPolicyTypes().size());
86 for (final ToscaPolicyTypeIdentifierParameters policyTypeIdentParameters : pdpStatusParameters
87 .getSupportedPolicyTypes()) {
88 supportedPolicyTypes.add(new ToscaConceptIdentifier(policyTypeIdentParameters.getName(),
89 policyTypeIdentParameters.getVersion()));
91 return supportedPolicyTypes;
95 * Method to create PdpStatus message from the context, which is to be sent by apex-pdp to pap.
97 * @return PdpStatus the pdp status message
99 public PdpStatus createPdpStatusFromContext() {
100 final PdpStatus pdpStatusContext = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT, PdpStatus.class);
101 final PdpStatus pdpStatus = new PdpStatus();
102 pdpStatus.setName(pdpStatusContext.getName());
103 pdpStatus.setPdpType(pdpStatusContext.getPdpType());
104 pdpStatus.setState(pdpStatusContext.getState());
105 pdpStatus.setHealthy(pdpStatusContext.getHealthy());
106 pdpStatus.setDescription(pdpStatusContext.getDescription());
107 pdpStatus.setPolicies(pdpStatusContext.getPolicies());
108 pdpStatus.setPdpGroup(pdpStatusContext.getPdpGroup());
109 pdpStatus.setPdpSubgroup(pdpStatusContext.getPdpSubgroup());
111 ApexEngineHandler apexEngineHandler = null;
113 apexEngineHandler = Registry.get(ApexStarterConstants.REG_APEX_ENGINE_HANDLER);
114 } catch (IllegalArgumentException e) {
115 LOGGER.warn(e.getMessage());
118 pdpStatus.setStatistics(getStatistics(pdpStatus, apexEngineHandler));
125 * Method to get the statistics.
127 * @return PdpStatistics the pdp status message
130 private PdpStatistics getStatistics(final PdpStatus pdpStatusContext, final ApexEngineHandler apexEngineHandler) {
131 PdpStatistics pdpStatistics = new PdpStatistics();
132 pdpStatistics.setPdpInstanceId(pdpStatusContext.getName());
133 pdpStatistics.setTimeStamp(Instant.now());
134 pdpStatistics.setPdpGroupName(pdpStatusContext.getPdpGroup());
135 pdpStatistics.setPdpSubGroupName(pdpStatusContext.getPdpSubgroup());
136 if (apexEngineHandler != null) {
137 pdpStatistics.setEngineStats(getEngineWorkerStats(apexEngineHandler));
139 final ApexPolicyStatisticsManager apexPolicyCounter = ApexPolicyStatisticsManager.getInstanceFromRegistry();
140 if (apexPolicyCounter != null) {
141 pdpStatistics.setPolicyDeploySuccessCount(apexPolicyCounter.getPolicyDeploySuccessCount());
142 pdpStatistics.setPolicyDeployFailCount(apexPolicyCounter.getPolicyDeployFailCount());
143 pdpStatistics.setPolicyDeployCount(apexPolicyCounter.getPolicyDeployCount());
144 pdpStatistics.setPolicyExecutedCount(apexPolicyCounter.getPolicyExecutedCount());
145 pdpStatistics.setPolicyExecutedSuccessCount(apexPolicyCounter.getPolicyExecutedSuccessCount());
146 pdpStatistics.setPolicyExecutedFailCount(apexPolicyCounter.getPolicyExecutedFailCount());
148 return pdpStatistics;
151 private List<PdpEngineWorkerStatistics> getEngineWorkerStats(@NonNull final ApexEngineHandler apexEngineHandler) {
152 List<PdpEngineWorkerStatistics> pdpEngineWorkerStats = new ArrayList<>();
153 List<AxEngineModel> engineModels = apexEngineHandler.getEngineStats();
154 if (engineModels != null) {
155 engineModels.forEach(engineModel -> {
156 PdpEngineWorkerStatistics workerStatistics = new PdpEngineWorkerStatistics();
157 workerStatistics.setEngineWorkerState(transferEngineState(engineModel.getState()));
158 workerStatistics.setEngineId(engineModel.getId());
159 workerStatistics.setEventCount(engineModel.getStats().getEventCount());
160 workerStatistics.setAverageExecutionTime(engineModel.getStats().getAverageExecutionTime());
161 workerStatistics.setEngineTimeStamp(engineModel.getStats().getTimeStamp());
162 workerStatistics.setLastEnterTime(engineModel.getStats().getLastEnterTime());
163 workerStatistics.setLastExecutionTime(engineModel.getStats().getLastExecutionTime());
164 workerStatistics.setLastStart(engineModel.getStats().getLastStart());
165 workerStatistics.setUpTime(engineModel.getStats().getUpTime());
166 pdpEngineWorkerStats.add(workerStatistics);
169 return pdpEngineWorkerStats;
172 private PdpEngineWorkerState transferEngineState(@NonNull final AxEngineState state) {
175 return PdpEngineWorkerState.STOPPING;
177 return PdpEngineWorkerState.STOPPED;
179 return PdpEngineWorkerState.READY;
181 return PdpEngineWorkerState.EXECUTING;
183 return PdpEngineWorkerState.UNDEFINED;
188 * Method to get a final pdp status when the apex started is shutting down.
190 * @return PdpStatus the pdp status message
192 public PdpStatus getTerminatedPdpStatus() {
193 final PdpStatus pdpStatusInContext = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT, PdpStatus.class);
194 pdpStatusInContext.setState(PdpState.TERMINATED);
195 pdpStatusInContext.setDescription("Apex pdp shutting down.");
196 return createPdpStatusFromContext();
200 * Method create PdpResponseDetails which will be sent as part of pdp status to PAP.
202 * @param requestId request id of the PdpUpdate message from pap
203 * @param status response status to be sent back
204 * @param responseMessage response message to be sent back
206 * @return PdpResponseDetails
208 public PdpResponseDetails createPdpResonseDetails(final String requestId, final PdpResponseStatus status,
209 final String responseMessage) {
210 final PdpResponseDetails pdpResponseDetails = new PdpResponseDetails();
211 pdpResponseDetails.setResponseTo(requestId);
212 pdpResponseDetails.setResponseStatus(status);
213 pdpResponseDetails.setResponseMessage(responseMessage);
214 return pdpResponseDetails;
218 * Method to retrieve list of ToscaPolicyIdentifier from the list of ToscaPolicy.
220 * @param policies list of ToscaPolicy
222 * @return policyTypeIdentifiers
224 public List<ToscaConceptIdentifier> getToscaPolicyIdentifiers(final List<ToscaPolicy> policies) {
225 final List<ToscaConceptIdentifier> policyIdentifiers = new ArrayList<>(policies.size());
226 for (final ToscaPolicy policy : policies) {
227 if (null != policy.getName() && null != policy.getVersion()) {
228 policyIdentifiers.add(new ToscaConceptIdentifier(policy.getName(), policy.getVersion()));
231 return policyIdentifiers;