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