2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2020 Nordix Foundation.
4 * Modifications Copyright (C) 2019 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.sim.pdp.handler;
24 import java.util.ArrayList;
25 import java.util.List;
27 import org.onap.policy.common.utils.services.Registry;
28 import org.onap.policy.models.pdp.concepts.PdpResponseDetails;
29 import org.onap.policy.models.pdp.concepts.PdpStatus;
30 import org.onap.policy.models.pdp.enums.PdpHealthStatus;
31 import org.onap.policy.models.pdp.enums.PdpResponseStatus;
32 import org.onap.policy.models.pdp.enums.PdpState;
33 import org.onap.policy.models.sim.pdp.PdpSimulatorConstants;
34 import org.onap.policy.models.sim.pdp.parameters.PdpStatusParameters;
35 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
36 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier;
39 * This class supports the handling of pdp messages.
41 * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
43 public class PdpMessageHandler {
46 * Method to create PdpStatus message from the parameters which will be saved to the context.
48 * @param instanceId instance id of the pdp
49 * @param pdpStatusParameters pdp status parameters read from the configuration file
51 * @return pdpStatus the pdp status message
53 public PdpStatus createPdpStatusFromParameters(final String instanceId,
54 final PdpStatusParameters pdpStatusParameters) {
55 final PdpStatus pdpStatus = new PdpStatus();
56 pdpStatus.setPdpType(pdpStatusParameters.getPdpType());
57 pdpStatus.setState(PdpState.PASSIVE);
58 pdpStatus.setHealthy(PdpHealthStatus.HEALTHY);
59 pdpStatus.setDescription(pdpStatusParameters.getDescription());
60 pdpStatus.setName(instanceId);
65 * Method to create PdpStatus message from the context, which is to be sent by pdp simulator to pap.
67 * @return PdpStatus the pdp status message
69 public PdpStatus createPdpStatusFromContext() {
70 final PdpStatus pdpStatusContext = Registry.get(PdpSimulatorConstants.REG_PDP_STATUS_OBJECT, PdpStatus.class);
71 final PdpStatus pdpStatus = new PdpStatus();
72 pdpStatus.setName(pdpStatusContext.getName());
73 pdpStatus.setPdpType(pdpStatusContext.getPdpType());
74 pdpStatus.setState(pdpStatusContext.getState());
75 pdpStatus.setHealthy(pdpStatusContext.getHealthy());
76 pdpStatus.setDescription(pdpStatusContext.getDescription());
77 pdpStatus.setPolicies(pdpStatusContext.getPolicies());
78 pdpStatus.setPdpGroup(pdpStatusContext.getPdpGroup());
79 pdpStatus.setPdpSubgroup(pdpStatusContext.getPdpSubgroup());
84 * Method to get a final pdp status when the simulator is shut down.
86 * @return PdpStatus the pdp status message
88 public PdpStatus getTerminatedPdpStatus() {
89 final PdpStatus pdpStatusInContext = Registry.get(PdpSimulatorConstants.REG_PDP_STATUS_OBJECT, PdpStatus.class);
90 pdpStatusInContext.setState(PdpState.TERMINATED);
91 pdpStatusInContext.setDescription("Pdp Simulator shutting down.");
92 return createPdpStatusFromContext();
96 * Method create PdpResponseDetails which will be sent as part of pdp status to PAP.
98 * @param requestId request id of the PdpUpdate message from pap
99 * @param status response status to be sent back
100 * @param responseMessage response message to be sent back
102 * @return PdpResponseDetails
104 public PdpResponseDetails createPdpResonseDetails(final String requestId, final PdpResponseStatus status,
105 final String responseMessage) {
106 final PdpResponseDetails pdpResponseDetails = new PdpResponseDetails();
107 pdpResponseDetails.setResponseTo(requestId);
108 pdpResponseDetails.setResponseStatus(status);
109 pdpResponseDetails.setResponseMessage(responseMessage);
110 return pdpResponseDetails;
114 * Method to retrieve list of ToscaPolicyIdentifier from the list of ToscaPolicy.
116 * @param policies list of ToscaPolicy
118 * @return policyTypeIdentifiers
120 public List<ToscaPolicyIdentifier> getToscaPolicyIdentifiers(final List<ToscaPolicy> policies) {
121 final List<ToscaPolicyIdentifier> policyIdentifiers = new ArrayList<>(policies.size());
122 for (final ToscaPolicy policy : policies) {
123 if (null != policy.getName() && null != policy.getVersion()) {
124 policyIdentifiers.add(new ToscaPolicyIdentifier(policy.getName(), policy.getVersion()));
127 return policyIdentifiers;