Remove db based statistics from apex-pdp
[policy/apex-pdp.git] / services / services-onappf / src / main / java / org / onap / policy / apex / services / onappf / handler / PdpMessageHandler.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019-2021 Nordix Foundation.
4  *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
5  *  Modifications Copyright (C) 2023 Bell Canada. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.apex.services.onappf.handler;
24
25 import java.util.ArrayList;
26 import java.util.List;
27 import org.onap.policy.apex.services.onappf.ApexStarterConstants;
28 import org.onap.policy.apex.services.onappf.parameters.PdpStatusParameters;
29 import org.onap.policy.apex.services.onappf.parameters.ToscaPolicyTypeIdentifierParameters;
30 import org.onap.policy.common.utils.services.Registry;
31 import org.onap.policy.models.pdp.concepts.PdpResponseDetails;
32 import org.onap.policy.models.pdp.concepts.PdpStatus;
33 import org.onap.policy.models.pdp.enums.PdpHealthStatus;
34 import org.onap.policy.models.pdp.enums.PdpResponseStatus;
35 import org.onap.policy.models.pdp.enums.PdpState;
36 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
37 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
38
39
40 /**
41  * This class supports the handling of pdp messages.
42  *
43  * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
44  */
45 public class PdpMessageHandler {
46
47     /**
48      * Method to create PdpStatus message from the parameters which will be saved to the context.
49      *
50      * @param instanceId instance id of apex pdp
51      * @param pdpStatusParameters pdp status parameters read from the configuration file
52      *
53      * @return pdpStatus the pdp status message
54      */
55     public PdpStatus createPdpStatusFromParameters(final String instanceId,
56             final PdpStatusParameters pdpStatusParameters) {
57         final var pdpStatus = new PdpStatus();
58         pdpStatus.setPdpGroup(pdpStatusParameters.getPdpGroup());
59         pdpStatus.setPdpType(pdpStatusParameters.getPdpType());
60         pdpStatus.setState(PdpState.PASSIVE);
61         pdpStatus.setHealthy(PdpHealthStatus.HEALTHY);
62         pdpStatus.setDescription(pdpStatusParameters.getDescription());
63         pdpStatus.setName(instanceId);
64         return pdpStatus;
65     }
66
67     /**
68      * Method to get supported policy types from the parameters.
69      *
70      * @param pdpStatusParameters pdp status parameters
71      * @return supportedPolicyTypes list of PolicyTypeIdent
72      */
73     public List<ToscaConceptIdentifier> getSupportedPolicyTypesFromParameters(
74             final PdpStatusParameters pdpStatusParameters) {
75         final List<ToscaConceptIdentifier> supportedPolicyTypes =
76                 new ArrayList<>(pdpStatusParameters.getSupportedPolicyTypes().size());
77         for (final ToscaPolicyTypeIdentifierParameters policyTypeIdentParameters : pdpStatusParameters
78                 .getSupportedPolicyTypes()) {
79             supportedPolicyTypes.add(new ToscaConceptIdentifier(policyTypeIdentParameters.getName(),
80                     policyTypeIdentParameters.getVersion()));
81         }
82         return supportedPolicyTypes;
83     }
84
85     /**
86      * Method to create PdpStatus message from the context, which is to be sent by apex-pdp to pap.
87      *
88      * @return PdpStatus the pdp status message
89      */
90     public PdpStatus createPdpStatusFromContext() {
91         final var pdpStatusContext = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT, PdpStatus.class);
92         final var pdpStatus = new PdpStatus();
93         pdpStatus.setName(pdpStatusContext.getName());
94         pdpStatus.setPdpType(pdpStatusContext.getPdpType());
95         pdpStatus.setState(pdpStatusContext.getState());
96         pdpStatus.setHealthy(pdpStatusContext.getHealthy());
97         pdpStatus.setDescription(pdpStatusContext.getDescription());
98         pdpStatus.setPolicies(pdpStatusContext.getPolicies());
99         pdpStatus.setPdpGroup(pdpStatusContext.getPdpGroup());
100         pdpStatus.setPdpSubgroup(pdpStatusContext.getPdpSubgroup());
101         return pdpStatus;
102     }
103
104     /**
105      * Method to get a final pdp status when the apex started is shutting down.
106      *
107      * @return PdpStatus the pdp status message
108      */
109     public PdpStatus getTerminatedPdpStatus() {
110         final var pdpStatusInContext = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT, PdpStatus.class);
111         pdpStatusInContext.setState(PdpState.TERMINATED);
112         pdpStatusInContext.setDescription("Apex pdp shutting down.");
113         return createPdpStatusFromContext();
114     }
115
116     /**
117      * Method create PdpResponseDetails which will be sent as part of pdp status to PAP.
118      *
119      * @param requestId request id of the PdpUpdate message from pap
120      * @param status response status to be sent back
121      * @param responseMessage response message to be sent back
122      *
123      * @return PdpResponseDetails
124      */
125     public PdpResponseDetails createPdpResonseDetails(final String requestId, final PdpResponseStatus status,
126             final String responseMessage) {
127         final var pdpResponseDetails = new PdpResponseDetails();
128         pdpResponseDetails.setResponseTo(requestId);
129         pdpResponseDetails.setResponseStatus(status);
130         pdpResponseDetails.setResponseMessage(responseMessage);
131         return pdpResponseDetails;
132     }
133
134     /**
135      * Method to retrieve list of ToscaPolicyIdentifier from the list of ToscaPolicy.
136      *
137      * @param policies list of ToscaPolicy
138      *
139      * @return policyTypeIdentifiers
140      */
141     public List<ToscaConceptIdentifier> getToscaPolicyIdentifiers(final List<ToscaPolicy> policies) {
142         final List<ToscaConceptIdentifier> policyIdentifiers = new ArrayList<>(policies.size());
143         for (final ToscaPolicy policy : policies) {
144             if (null != policy.getName() && null != policy.getVersion()) {
145                 policyIdentifiers.add(new ToscaConceptIdentifier(policy.getName(), policy.getVersion()));
146             }
147         }
148         return policyIdentifiers;
149     }
150 }