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