2c0c450ed440ea4664e308d36bcc538d080bb8ce
[policy/clamp.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.controlloop.participant.simulator.main.rest;
22
23 import static org.junit.Assert.assertTrue;
24
25 import java.io.File;
26 import java.io.FileNotFoundException;
27 import java.time.Instant;
28 import java.util.LinkedHashMap;
29 import java.util.Map;
30 import java.util.Set;
31 import java.util.UUID;
32 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop;
33 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
34 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState;
35 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
36 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantState;
37 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopUpdate;
38 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantControlLoopStateChange;
39 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantStateChange;
40 import org.onap.policy.clamp.controlloop.participant.simulator.main.parameters.CommonTestData;
41 import org.onap.policy.common.utils.coder.Coder;
42 import org.onap.policy.common.utils.coder.CoderException;
43 import org.onap.policy.common.utils.coder.StandardCoder;
44 import org.onap.policy.common.utils.coder.YamlJsonTranslator;
45 import org.onap.policy.common.utils.resources.ResourceUtils;
46 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51
52 public class TestListenerUtils {
53
54     private static final YamlJsonTranslator yamlTranslator = new YamlJsonTranslator();
55     private static final Coder CODER = new StandardCoder();
56     static CommonTestData commonTestData = new CommonTestData();
57     private static final Logger LOGGER = LoggerFactory.getLogger(TestListenerUtils.class);
58
59     private TestListenerUtils() {}
60
61     /**
62      * Method to create a controlLoop from a yaml file.
63      *
64      * @return ControlLoop controlloop
65      */
66     public static ControlLoop createControlLoop() {
67         ControlLoop controlLoop = new ControlLoop();
68         Map<UUID, ControlLoopElement> elements = new LinkedHashMap<>();
69         ToscaServiceTemplate toscaServiceTemplate = testControlLoopRead();
70         Map<String, ToscaNodeTemplate> nodeTemplatesMap =
71                 toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates();
72         for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : nodeTemplatesMap.entrySet()) {
73             ControlLoopElement clElement = new ControlLoopElement();
74             clElement.setId(UUID.randomUUID());
75
76             ToscaConceptIdentifier clElementParticipantId = new ToscaConceptIdentifier();
77             clElementParticipantId.setName(toscaInputEntry.getKey());
78             clElementParticipantId.setVersion(toscaInputEntry.getValue().getVersion());
79             clElement.setParticipantId(clElementParticipantId);
80             clElement.setParticipantType(clElementParticipantId);
81
82             clElement.setDefinition(clElementParticipantId);
83             clElement.setState(ControlLoopState.UNINITIALISED);
84             clElement.setDescription(toscaInputEntry.getValue().getDescription());
85             clElement.setOrderedState(ControlLoopOrderedState.UNINITIALISED);
86             elements.put(clElement.getId(), clElement);
87         }
88         controlLoop.setElements(elements);
89         controlLoop.setName("PMSHInstance0");
90         controlLoop.setVersion("1.0.0");
91
92         ToscaConceptIdentifier definition = new ToscaConceptIdentifier();
93         definition.setName("PMSHInstance0");
94         definition.setVersion("1.0.0");
95         controlLoop.setDefinition(definition);
96
97         return controlLoop;
98     }
99
100     /**
101      * Method to create ParticipantStateChange message from the arguments passed.
102      *
103      * @param participantState participant State
104      *
105      * @return ParticipantStateChange message
106      */
107     public static ParticipantStateChange createParticipantStateChangeMsg(final ParticipantState participantState) {
108         final ParticipantStateChange participantStateChangeMsg = new ParticipantStateChange();
109         ToscaConceptIdentifier participantId = new ToscaConceptIdentifier("org.onap.PM_CDS_Blueprint", "1.0.0");
110
111         participantStateChangeMsg.setParticipantId(participantId);
112         participantStateChangeMsg.setTimestamp(Instant.now());
113         participantStateChangeMsg.setState(participantState);
114
115         return participantStateChangeMsg;
116     }
117
118     /**
119      * Method to create ControlLoopStateChange message from the arguments passed.
120      *
121      * @param controlLoopOrderedState controlLoopOrderedState
122      *
123      * @return ParticipantControlLoopStateChange message
124      */
125     public static ParticipantControlLoopStateChange createControlLoopStateChangeMsg(
126             final ControlLoopOrderedState controlLoopOrderedState) {
127         final ParticipantControlLoopStateChange participantClStateChangeMsg = new ParticipantControlLoopStateChange();
128
129         ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier("PMSHInstance0", "1.0.0");
130         ToscaConceptIdentifier participantId = new ToscaConceptIdentifier("org.onap.PM_CDS_Blueprint", "1.0.0");
131
132         participantClStateChangeMsg.setControlLoopId(controlLoopId);
133         participantClStateChangeMsg.setParticipantId(participantId);
134         participantClStateChangeMsg.setTimestamp(Instant.now());
135         participantClStateChangeMsg.setOrderedState(controlLoopOrderedState);
136
137         return participantClStateChangeMsg;
138     }
139
140     /**
141      * Method to create ControlLoopUpdateMsg.
142      *
143      * @return ControlLoopUpdate message
144      */
145     public static ControlLoopUpdate createControlLoopUpdateMsg() {
146         final ControlLoopUpdate clUpdateMsg = new ControlLoopUpdate();
147         ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier("PMSHInstance0", "1.0.0");
148         ToscaConceptIdentifier participantId = new ToscaConceptIdentifier("org.onap.PM_CDS_Blueprint", "1.0.0");
149
150         clUpdateMsg.setControlLoopId(controlLoopId);
151         clUpdateMsg.setParticipantId(participantId);
152         clUpdateMsg.setParticipantType(participantId);
153
154         ControlLoop controlLoop = new ControlLoop();
155         Map<UUID, ControlLoopElement> elements = new LinkedHashMap<>();
156         ToscaServiceTemplate toscaServiceTemplate = testControlLoopRead();
157         Map<String, ToscaNodeTemplate> nodeTemplatesMap =
158                 toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates();
159         for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : nodeTemplatesMap.entrySet()) {
160             ControlLoopElement clElement = new ControlLoopElement();
161             clElement.setId(UUID.randomUUID());
162
163             ToscaConceptIdentifier clElementParticipantId = new ToscaConceptIdentifier();
164             clElementParticipantId.setName(toscaInputEntry.getKey());
165             clElementParticipantId.setVersion(toscaInputEntry.getValue().getVersion());
166             clElement.setParticipantId(clElementParticipantId);
167             clElement.setParticipantType(clElementParticipantId);
168
169             clElement.setDefinition(clElementParticipantId);
170             clElement.setState(ControlLoopState.UNINITIALISED);
171             clElement.setDescription(toscaInputEntry.getValue().getDescription());
172             clElement.setOrderedState(ControlLoopOrderedState.UNINITIALISED);
173             elements.put(clElement.getId(), clElement);
174         }
175         controlLoop.setElements(elements);
176         controlLoop.setName("PMSHInstance0");
177         controlLoop.setVersion("1.0.0");
178         controlLoop.setDefinition(controlLoopId);
179         clUpdateMsg.setControlLoop(controlLoop);
180         clUpdateMsg.setControlLoopDefinition(toscaServiceTemplate);
181
182         return clUpdateMsg;
183     }
184
185     /**
186      * Method to create ControlLoopUpdate using the arguments passed.
187      *
188      * @param jsonFilePath the path of the controlloop content
189      *
190      * @return ControlLoopUpdate message
191      * @throws CoderException exception while reading the file to object
192      */
193     public static ControlLoopUpdate createParticipantClUpdateMsgFromJson(String jsonFilePath)
194             throws CoderException {
195         ControlLoopUpdate controlLoopUpdateMsg =
196                 CODER.decode(new File(jsonFilePath), ControlLoopUpdate.class);
197         return controlLoopUpdateMsg;
198     }
199
200     private static ToscaServiceTemplate testControlLoopRead() {
201         Set<String> controlLoopDirectoryContents =
202                 ResourceUtils.getDirectoryContents("src/test/resources/rest/servicetemplates");
203
204         boolean atLeastOneControlLoopTested = false;
205         ToscaServiceTemplate toscaServiceTemplate = null;
206
207         for (String controlLoopFilePath : controlLoopDirectoryContents) {
208             if (!controlLoopFilePath.endsWith(".yaml")) {
209                 continue;
210             }
211             atLeastOneControlLoopTested = true;
212             toscaServiceTemplate = testControlLoopYamlSerialization(controlLoopFilePath);
213         }
214
215         assertTrue(atLeastOneControlLoopTested);
216         return toscaServiceTemplate;
217     }
218
219     private static ToscaServiceTemplate testControlLoopYamlSerialization(String controlLoopFilePath) {
220         try {
221             String controlLoopString = ResourceUtils.getResourceAsString(controlLoopFilePath);
222             if (controlLoopString == null) {
223                 throw new FileNotFoundException(controlLoopFilePath);
224             }
225
226             ToscaServiceTemplate serviceTemplate = yamlTranslator.fromYaml(
227                             controlLoopString, ToscaServiceTemplate.class);
228             return serviceTemplate;
229         } catch (FileNotFoundException e) {
230             LOGGER.error("cannot find YAML file", controlLoopFilePath);
231             throw new IllegalArgumentException(e);
232         }
233     }
234 }