2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2020 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.comm;
23 import com.google.gson.JsonObject;
25 import java.util.LinkedHashMap;
26 import java.util.List;
28 import org.onap.policy.common.utils.coder.CoderException;
29 import org.onap.policy.common.utils.coder.StandardCoder;
30 import org.onap.policy.models.pdp.concepts.PdpStateChange;
31 import org.onap.policy.models.pdp.concepts.PdpStatus;
32 import org.onap.policy.models.pdp.concepts.PdpUpdate;
33 import org.onap.policy.models.pdp.enums.PdpState;
34 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
36 public class TestListenerUtils {
39 * Method to create PdpUpdate message from the arguments passed.
41 * @param pdpStatus pdp status
42 * @param toscaPolicies list of tosca policies
44 * @return PdpUpdate message
46 public static PdpUpdate createPdpUpdateMsg(final PdpStatus pdpStatus, List<ToscaPolicy> toscaPolicies) {
47 final PdpUpdate pdpUpdateMsg = new PdpUpdate();
48 pdpUpdateMsg.setDescription("dummy pdp status for test");
49 pdpUpdateMsg.setPdpGroup("pdpGroup");
50 pdpUpdateMsg.setPdpSubgroup("pdpSubgroup");
51 pdpUpdateMsg.setName(pdpStatus.getName());
52 pdpUpdateMsg.setPdpHeartbeatIntervalMs(Long.valueOf(3000));
53 pdpUpdateMsg.setPolicies(toscaPolicies);
58 * Method to create ToscaPolicy using the arguments passed.
60 * @param policyName the name of the policy
61 * @param policyVersion the version of the policy
62 * @param policyFilePath the path of the policy content
64 * @return PdpUpdate message
65 * @throws CoderException exception while reading the file to object
67 public static ToscaPolicy createToscaPolicy(String policyName, String policyVersion, String policyFilePath)
68 throws CoderException {
69 final ToscaPolicy toscaPolicy = new ToscaPolicy();
70 toscaPolicy.setType("apexpolicytype");
71 toscaPolicy.setVersion(policyVersion);
72 toscaPolicy.setName(policyName);
73 final Map<String, Object> propertiesMap = new LinkedHashMap<>();
74 JsonObject properties = new StandardCoder().decode(new File(policyFilePath), JsonObject.class);
75 properties.entrySet().forEach(entry -> {
76 propertiesMap.put(entry.getKey(), entry.getValue());
78 toscaPolicy.setProperties(propertiesMap);
83 * Method to create PdpStateChange message from the arguments passed.
85 * @param state the new pdp state
86 * @param pdpGroup name of the pdpGroup
87 * @param pdpSubgroup name of the pdpSubGroup
88 * @param name the name of the message
90 * @return PdpStateChange message
92 public static PdpStateChange createPdpStateChangeMsg(PdpState state, String pdpGroup, String pdpSubgroup,
94 final PdpStateChange pdpStateChangeMsg = new PdpStateChange();
95 pdpStateChangeMsg.setState(state);
96 pdpStateChangeMsg.setPdpGroup(pdpGroup);
97 pdpStateChangeMsg.setPdpSubgroup(pdpSubgroup);
98 pdpStateChangeMsg.setName(name);
99 return pdpStateChangeMsg;