2 * ============LICENSE_START=======================================================
3 * Copyright (C) 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.clamp.controlloop.participant.simulator.main.rest;
23 import static org.junit.Assert.assertTrue;
25 import java.io.FileNotFoundException;
26 import java.util.ArrayList;
27 import java.util.List;
30 import java.util.UUID;
31 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop;
32 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
33 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState;
34 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
35 import org.onap.policy.clamp.controlloop.participant.simulator.main.parameters.CommonTestData;
36 import org.onap.policy.common.utils.coder.Coder;
37 import org.onap.policy.common.utils.coder.StandardCoder;
38 import org.onap.policy.common.utils.coder.YamlJsonTranslator;
39 import org.onap.policy.common.utils.resources.ResourceUtils;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
46 public class TestListenerUtils {
48 private static final YamlJsonTranslator yamlTranslator = new YamlJsonTranslator();
49 private static final Coder CODER = new StandardCoder();
50 static CommonTestData commonTestData = new CommonTestData();
51 private static RestController restController;
52 private static final Logger LOGGER = LoggerFactory.getLogger(TestListenerUtils.class);
54 private TestListenerUtils() {}
57 * Method to create a controlLoop from a yaml file.
59 * @return ControlLoop controlloop
61 public static ControlLoop createControlLoop() {
62 ControlLoop controlLoop = new ControlLoop();
63 List<ControlLoopElement> elements = new ArrayList<>();
64 ToscaServiceTemplate toscaServiceTemplate = testControlLoopRead();
65 Map<String, ToscaNodeTemplate> nodeTemplatesMap = toscaServiceTemplate
66 .getToscaTopologyTemplate().getNodeTemplates();
67 for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : nodeTemplatesMap.entrySet()) {
68 ControlLoopElement clElement = new ControlLoopElement();
69 clElement.setId(UUID.randomUUID());
71 ToscaConceptIdentifier clElementParticipantId = new ToscaConceptIdentifier();
72 clElementParticipantId.setName(toscaInputEntry.getKey());
73 clElementParticipantId.setVersion(toscaInputEntry.getValue().getVersion());
74 clElement.setParticipantId(clElementParticipantId);
76 clElement.setDefinition(clElementParticipantId);
77 clElement.setState(ControlLoopState.UNINITIALISED);
78 clElement.setDescription(toscaInputEntry.getValue().getDescription());
79 clElement.setOrderedState(ControlLoopOrderedState.UNINITIALISED);
80 elements.add(clElement);
82 controlLoop.setElements(elements);
83 controlLoop.setName("PMSHInstance0");
84 controlLoop.setVersion("1.0.0");
86 ToscaConceptIdentifier definition = new ToscaConceptIdentifier();
87 definition.setName("PMSHInstance0");
88 definition.setVersion("1.0.0");
89 controlLoop.setDefinition(definition);
94 private static ToscaServiceTemplate testControlLoopRead() {
95 Set<String> controlLoopDirectoryContents = ResourceUtils
96 .getDirectoryContents("src/test/resources/rest/servicetemplates");
98 boolean atLeastOneControlLoopTested = false;
99 ToscaServiceTemplate toscaServiceTemplate = null;
101 for (String controlLoopFilePath : controlLoopDirectoryContents) {
102 if (!controlLoopFilePath.endsWith(".yaml")) {
105 atLeastOneControlLoopTested = true;
106 toscaServiceTemplate = testControlLoopYamlSerialization(controlLoopFilePath);
109 assertTrue(atLeastOneControlLoopTested);
110 return toscaServiceTemplate;
113 private static ToscaServiceTemplate testControlLoopYamlSerialization(String controlLoopFilePath) {
115 String controlLoopString = ResourceUtils.getResourceAsString(controlLoopFilePath);
116 if (controlLoopString == null) {
117 throw new FileNotFoundException(controlLoopFilePath);
120 ToscaServiceTemplate serviceTemplate = yamlTranslator.fromYaml(
121 controlLoopString, ToscaServiceTemplate.class);
122 return serviceTemplate;
123 } catch (FileNotFoundException e) {
124 LOGGER.error("cannot find YAML file", controlLoopFilePath);
125 throw new IllegalArgumentException(e);