Topic names in PAP should be configurable from application.yaml
[policy/pap.git] / main / src / test / java / org / onap / policy / pap / main / rest / e2e / End2EndBase.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP PAP
4  * ================================================================================
5  * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2019-2020, 2022 Nordix Foundation.
7  * Modifications Copyright (C) 2021-2022 Bell Canada. All rights reserved.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.pap.main.rest.e2e;
24
25 import io.micrometer.core.instrument.MeterRegistry;
26 import java.io.File;
27 import java.io.FileInputStream;
28 import java.io.FileNotFoundException;
29 import java.io.IOException;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.Optional;
33 import lombok.Getter;
34 import org.junit.After;
35 import org.onap.policy.common.parameters.ValidationResult;
36 import org.onap.policy.common.utils.coder.Coder;
37 import org.onap.policy.common.utils.coder.CoderException;
38 import org.onap.policy.common.utils.coder.StandardCoder;
39 import org.onap.policy.common.utils.resources.PrometheusUtils;
40 import org.onap.policy.common.utils.resources.ResourceUtils;
41 import org.onap.policy.models.base.PfConceptKey;
42 import org.onap.policy.models.pdp.concepts.PdpGroup;
43 import org.onap.policy.models.pdp.concepts.PdpGroups;
44 import org.onap.policy.models.pdp.concepts.PdpPolicyStatus;
45 import org.onap.policy.models.pdp.concepts.PdpPolicyStatus.State;
46 import org.onap.policy.models.pdp.concepts.PdpStatistics;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
48 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
49 import org.onap.policy.pap.main.PolicyPapRuntimeException;
50 import org.onap.policy.pap.main.repository.ToscaServiceTemplateRepository;
51 import org.onap.policy.pap.main.rest.CommonPapRestServer;
52 import org.onap.policy.pap.main.service.PdpGroupService;
53 import org.onap.policy.pap.main.service.PdpStatisticsService;
54 import org.onap.policy.pap.main.service.PolicyStatusService;
55 import org.onap.policy.pap.main.service.ToscaServiceTemplateService;
56 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
58 import org.springframework.beans.factory.annotation.Autowired;
59 import org.springframework.test.context.ActiveProfiles;
60 import org.yaml.snakeyaml.Yaml;
61
62 @ActiveProfiles("test-e2e")
63 public abstract class End2EndBase extends CommonPapRestServer {
64     private static final Logger logger = LoggerFactory.getLogger(End2EndBase.class);
65
66     private static final Coder coder = new StandardCoder();
67     private static final Yaml yaml = new Yaml();
68
69     /**
70      * Context - should be initialized by setUp() method.
71      */
72     protected End2EndContext context = null;
73
74     @Autowired
75     public PdpGroupService pdpGroupService;
76
77     @Autowired
78     public PdpStatisticsService pdpStatisticsService;
79
80     @Autowired
81     private ToscaServiceTemplateRepository serviceTemplateRepository;
82
83     @Autowired
84     public PolicyStatusService policyStatusService;
85
86     @Autowired
87     public ToscaServiceTemplateService toscaService;
88
89     @Autowired
90     public MeterRegistry meterRegistry;
91
92     @Getter
93     private final String topicPolicyPdpPap = "pdp-pap-topic";
94
95     @Getter
96     private final String topicPolicyNotification = "notification-topic";
97
98     public String deploymentsCounterName = "pap_" + PrometheusUtils.POLICY_DEPLOYMENTS_METRIC;
99     public String[] deploymentSuccessTag = {PrometheusUtils.OPERATION_METRIC_LABEL, PrometheusUtils.DEPLOY_OPERATION,
100         PrometheusUtils.STATUS_METRIC_LABEL, State.SUCCESS.name()};
101     public String[] unDeploymentSuccessTag = {PrometheusUtils.OPERATION_METRIC_LABEL,
102         PrometheusUtils.UNDEPLOY_OPERATION, PrometheusUtils.STATUS_METRIC_LABEL, State.SUCCESS.name()};
103
104     /**
105      * Tears down.
106      */
107     @Override
108     @After
109     public void tearDown() {
110         if (context != null) {
111             try {
112                 context.stop();
113             } catch (final Exception e) {
114                 logger.warn("failed to stop end-to-end context", e);
115             }
116             context = null;
117         }
118         meterRegistry.clear();
119         super.tearDown();
120     }
121
122     /**
123      * Adds Tosca Policy Types to the DB.
124      *
125      * @param yamlFile name of the YAML file specifying the data to be loaded
126      */
127     public void addToscaPolicyTypes(final String yamlFile) {
128         final ToscaServiceTemplate serviceTemplate = loadYamlFile(yamlFile, ToscaServiceTemplate.class);
129         JpaToscaServiceTemplate jpaToscaServiceTemplate = mergeWithExistingTemplate(serviceTemplate);
130         serviceTemplateRepository.save(jpaToscaServiceTemplate);
131     }
132
133     /**
134      * Adds Tosca Policies to the DB.
135      *
136      * @param yamlFile name of the YAML file specifying the data to be loaded
137      */
138     public void addToscaPolicies(final String yamlFile) {
139         final ToscaServiceTemplate serviceTemplate = loadYamlFile(yamlFile, ToscaServiceTemplate.class);
140         JpaToscaServiceTemplate jpaToscaServiceTemplate = mergeWithExistingTemplate(serviceTemplate);
141         serviceTemplateRepository.save(jpaToscaServiceTemplate);
142     }
143
144     private JpaToscaServiceTemplate mergeWithExistingTemplate(ToscaServiceTemplate serviceTemplate) {
145         JpaToscaServiceTemplate jpaToscaServiceTemplate = new JpaToscaServiceTemplate(serviceTemplate);
146         Optional<JpaToscaServiceTemplate> dbServiceTemplateOpt = serviceTemplateRepository
147             .findById(new PfConceptKey(JpaToscaServiceTemplate.DEFAULT_NAME, JpaToscaServiceTemplate.DEFAULT_VERSION));
148         if (dbServiceTemplateOpt.isPresent()) {
149             JpaToscaServiceTemplate dbServiceTemplate = dbServiceTemplateOpt.get();
150             if (dbServiceTemplate.getPolicyTypes() != null) {
151                 jpaToscaServiceTemplate.setPolicyTypes(dbServiceTemplate.getPolicyTypes());
152             }
153             if (dbServiceTemplate.getDataTypes() != null) {
154                 jpaToscaServiceTemplate.setDataTypes(dbServiceTemplate.getDataTypes());
155             }
156             if (dbServiceTemplate.getTopologyTemplate() != null) {
157                 jpaToscaServiceTemplate.setTopologyTemplate(dbServiceTemplate.getTopologyTemplate());
158             }
159         }
160
161         return jpaToscaServiceTemplate;
162     }
163
164     /**
165      * Adds PDP groups to the DB.
166      *
167      * @param jsonFile name of the JSON file specifying the data to be loaded
168      */
169     public void addGroups(final String jsonFile) {
170         final PdpGroups groups = loadJsonFile(jsonFile, PdpGroups.class);
171
172         final ValidationResult result = groups.validatePapRest();
173         if (!result.isValid()) {
174             throw new PolicyPapRuntimeException("cannot init DB groups from " + jsonFile + ":\n" + result.getResult());
175         }
176
177         pdpGroupService.createPdpGroups(groups.getGroups());
178     }
179
180     /**
181      * Fetch PDP groups from the DB.
182      *
183      * @param name name of the pdpGroup
184      */
185     public List<PdpGroup> fetchGroups(final String name) {
186         return pdpGroupService.getPdpGroups(name);
187     }
188
189     /**
190      * Fetch PDP statistics from the DB.
191      *
192      * @param instanceId name of the pdpStatistics
193      * @param groupName name of the pdpGroup
194      * @param subGroupName name of the pdpSubGroup
195      */
196     public Map<String, Map<String, List<PdpStatistics>>> fetchPdpStatistics(final String instanceId,
197         final String groupName, final String subGroupName) {
198         return pdpStatisticsService.fetchDatabaseStatistics(groupName, subGroupName, instanceId, 100, null, null);
199     }
200
201     /**
202      * Adds PdpPolicyStatus records to the DB.
203      *
204      * @param jsonFile name of the JSON file specifying the data to be loaded
205      */
206     public void addPdpPolicyStatus(final String jsonFile) {
207         final PolicyStatusRecords data = loadJsonFile(jsonFile, PolicyStatusRecords.class);
208         policyStatusService.cudPolicyStatus(data.records, null, null);
209     }
210
211     /**
212      * Loads an object from a YAML file.
213      *
214      * @param fileName name of the file from which to load
215      * @param clazz the class of the object to be loaded
216      * @return the object that was loaded from the file
217      */
218     protected static <T> T loadYamlFile(final String fileName, final Class<T> clazz) {
219         final File propFile = new File(ResourceUtils.getFilePath4Resource("e2e/" + fileName));
220
221         try (FileInputStream input = new FileInputStream(propFile)) {
222             final Object yamlObject = yaml.load(input);
223             final String json = coder.encode(yamlObject);
224             final T result = coder.decode(json, clazz);
225
226             if (result == null) {
227                 throw new PolicyPapRuntimeException("cannot decode " + clazz.getSimpleName() + " from " + fileName);
228             }
229
230             return result;
231
232         } catch (final FileNotFoundException e) {
233             throw new PolicyPapRuntimeException("cannot find " + fileName, e);
234
235         } catch (IOException | CoderException e) {
236             throw new PolicyPapRuntimeException("cannot decode " + fileName, e);
237         }
238     }
239
240     /**
241      * Loads an object from a JSON file.
242      *
243      * @param fileName name of the file from which to load
244      * @param clazz the class of the object to be loaded
245      * @return the object that was loaded from the file
246      */
247     protected static <T> T loadJsonFile(final String fileName, final Class<T> clazz) {
248         final String fileName2 = (fileName.startsWith("src/") ? fileName : "e2e/" + fileName);
249         final File propFile = new File(ResourceUtils.getFilePath4Resource(fileName2));
250         try {
251             final T result = coder.decode(propFile, clazz);
252
253             if (result == null) {
254                 throw new PolicyPapRuntimeException("cannot decode " + clazz.getSimpleName() + " from " + fileName);
255             }
256
257             return result;
258
259         } catch (final CoderException e) {
260             throw new RuntimeException(e);
261         }
262     }
263
264     public class PolicyStatusRecords {
265         private List<PdpPolicyStatus> records;
266     }
267 }