05839c1a3ad59f53ef15ac0b3a6927acc2b468b8
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 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.so.bpmn.infrastructure.process;
22
23 import com.google.protobuf.Struct;
24 import org.camunda.bpm.engine.runtime.ProcessInstance;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.onap.ccsdk.cds.controllerblueprints.common.api.ActionIdentifiers;
28 import org.onap.ccsdk.cds.controllerblueprints.common.api.CommonHeader;
29 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceInput;
30 import org.onap.so.BaseBPMNTest;
31 import org.onap.so.GrpcNettyServer;
32 import org.onap.so.bpmn.mock.FileUtil;
33 import org.onap.aaiclient.client.aai.AAIVersion;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import java.io.IOException;
38 import java.util.HashMap;
39 import java.util.List;
40 import java.util.Map;
41 import java.util.UUID;
42 import static com.github.tomakehurst.wiremock.client.WireMock.okJson;
43 import static com.github.tomakehurst.wiremock.client.WireMock.get;
44 import static com.github.tomakehurst.wiremock.client.WireMock.put;
45 import static com.github.tomakehurst.wiremock.client.WireMock.post;
46 import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
47 import static com.github.tomakehurst.wiremock.client.WireMock.ok;
48 import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching;
49 import static org.assertj.core.api.Assertions.assertThat;
50 import static org.assertj.core.api.Assertions.fail;
51 import static org.camunda.bpm.engine.test.assertions.bpmn.BpmnAwareAssertions.assertThat;
52
53
54 /**
55  * Basic Integration test for createVcpeResCustService_Simplified.bpmn workflow.
56  */
57 public class CreateVcpeResCustServiceSimplifiedTest extends BaseBPMNTest {
58
59     private Logger logger = LoggerFactory.getLogger(getClass());
60
61     private static final long WORKFLOW_WAIT_TIME = 1000L;
62     private static final int DMAAP_DELAY_TIME_MS = 2000;
63
64     private static final String TEST_PROCESSINSTANCE_KEY = "CreateVcpeResCustService_simplified";
65     private static final AAIVersion VERSION = AAIVersion.LATEST;
66     private static final Map<String, Object> executionVariables = new HashMap();
67     private String testBusinessKey;
68     private String requestObject;
69     private String responseObject;
70
71     @Autowired
72     private GrpcNettyServer grpcNettyServer;
73
74     @Before
75     public void setUp() throws IOException {
76
77         requestObject = FileUtil.readResourceFile("request/" + getClass().getSimpleName() + ".json");
78         responseObject = FileUtil.readResourceFile("response/" + getClass().getSimpleName() + ".json");
79
80         executionVariables.clear();
81
82         executionVariables.put("bpmnRequest", requestObject);
83
84         /**
85          * This variable indicates that the flow was invoked asynchronously. It's injected by {@link WorkflowProcessor}.
86          */
87         executionVariables.put("isAsyncProcess", "true");
88
89         /**
90          * Temporary solution to add pnfCorrelationId to context. this value is getting from the request to SO api
91          * handler and then convert to CamudaInput
92          */
93         executionVariables.put("pnfCorrelationId", "PNFDemo");
94
95         /**
96          * Create mso-request-id.
97          */
98
99
100         /**
101          * Create Business key for the process instance
102          */
103         testBusinessKey = UUID.randomUUID().toString();
104
105         logger.info("Test the process instance: {} with business key: {}", TEST_PROCESSINSTANCE_KEY, testBusinessKey);
106
107     }
108
109     @Test
110     public void workflow_validInput_expectedOuput() throws InterruptedException {
111
112         mockCatalogDb();
113         mockRequestDb();
114         mockAai();
115         mockDmaapForPnf();
116
117         final String msoRequestId = UUID.randomUUID().toString();
118         executionVariables.put("mso-request-id", msoRequestId);
119
120         ProcessInstance pi =
121                 runtimeService.startProcessInstanceByKey(TEST_PROCESSINSTANCE_KEY, testBusinessKey, executionVariables);
122
123         int waitCount = 10;
124         while (!isProcessInstanceEnded() && waitCount >= 0) {
125             Thread.sleep(WORKFLOW_WAIT_TIME);
126             waitCount--;
127         }
128
129         assertThat(pi).isEnded().hasPassedInOrder("createVCPE_startEvent", "preProcessRequest_ScriptTask",
130                 "sendSyncAckResponse_ScriptTask", "ScriptTask_0cdtchu", "DecomposeService", "ScriptTask_0lpv2da",
131                 "ScriptTask_1y241p8", "CallActivity_1vc4jeh", "ScriptTask_1y5lvl7", "GeneratePnfUuid", "Task_14l19kv",
132                 "Pnf_Con", "setPONR_ScriptTask", "postProcessAndCompletionRequest_ScriptTask",
133                 "callCompleteMsoProcess_CallActivity", "ScriptTask_2", "CreateVCPE_EndEvent");
134
135         List<ExecutionServiceInput> detailedMessages = grpcNettyServer.getDetailedMessages();
136         assertThat(detailedMessages.size() == 2);
137         int count = 0;
138         try {
139             for (ExecutionServiceInput eSI : detailedMessages) {
140                 if ("config-assign".equals(eSI.getActionIdentifiers().getActionName())) {
141                     checkConfigAssign(eSI, msoRequestId);
142                     count++;
143                 }
144                 if ("config-deploy".equals(eSI.getActionIdentifiers().getActionName())) {
145                     checkConfigDeploy(eSI, msoRequestId);
146                     count++;
147                 }
148             }
149         } catch (Exception e) {
150             e.printStackTrace();
151             fail("ConfigAssign/deploy request exception", e);
152         }
153         assertThat(count == 2);
154     }
155
156     private boolean isProcessInstanceEnded() {
157         return runtimeService.createProcessInstanceQuery().processDefinitionKey(TEST_PROCESSINSTANCE_KEY)
158                 .singleResult() == null;
159     }
160
161     private void checkConfigAssign(ExecutionServiceInput executionServiceInput, String msoRequestId) {
162
163         logger.info("Checking the configAssign request");
164         ActionIdentifiers actionIdentifiers = executionServiceInput.getActionIdentifiers();
165
166         /**
167          * the fields of actionIdentifiers should match the one in the
168          * response/createVcpeResCustServiceSimplifiedTest_catalogdb.json.
169          */
170         assertThat(actionIdentifiers.getBlueprintName()).isEqualTo("test_configuration_restconf");
171         assertThat(actionIdentifiers.getBlueprintVersion()).isEqualTo("1.0.0");
172         assertThat(actionIdentifiers.getActionName()).isEqualTo("config-assign");
173         assertThat(actionIdentifiers.getMode()).isEqualTo("sync");
174
175         CommonHeader commonHeader = executionServiceInput.getCommonHeader();
176         assertThat(commonHeader.getOriginatorId()).isEqualTo("SO");
177         assertThat(commonHeader.getRequestId()).isEqualTo(msoRequestId);
178
179         Struct payload = executionServiceInput.getPayload();
180         Struct requeststruct = payload.getFieldsOrThrow("config-assign-request").getStructValue();
181
182         assertThat(requeststruct.getFieldsOrThrow("resolution-key").getStringValue()).isEqualTo("PNFDemo");
183         Struct propertiesStruct = requeststruct.getFieldsOrThrow("config-assign-properties").getStructValue();
184
185         assertThat(propertiesStruct.getFieldsOrThrow("pnf-name").getStringValue()).isEqualTo("PNFDemo");
186         assertThat(propertiesStruct.getFieldsOrThrow("service-model-uuid").getStringValue())
187                 .isEqualTo("f2daaac6-5017-4e1e-96c8-6a27dfbe1421");
188         assertThat(propertiesStruct.getFieldsOrThrow("pnf-customization-uuid").getStringValue())
189                 .isEqualTo("68dc9a92-214c-11e7-93ae-92361f002680");
190     }
191
192     private void checkConfigDeploy(ExecutionServiceInput executionServiceInput, String msoRequestId) {
193
194         logger.info("Checking the configDeploy request");
195         ActionIdentifiers actionIdentifiers = executionServiceInput.getActionIdentifiers();
196
197         /**
198          * the fields of actionIdentifiers should match the one in the
199          * response/createVcpeResCustServiceSimplifiedTest_catalogdb.json.
200          */
201         assertThat(actionIdentifiers.getBlueprintName()).isEqualTo("test_configuration_restconf");
202         assertThat(actionIdentifiers.getBlueprintVersion()).isEqualTo("1.0.0");
203         assertThat(actionIdentifiers.getActionName()).isEqualTo("config-deploy");
204         assertThat(actionIdentifiers.getMode()).isEqualTo("async");
205
206         CommonHeader commonHeader = executionServiceInput.getCommonHeader();
207         assertThat(commonHeader.getOriginatorId()).isEqualTo("SO");
208         assertThat(commonHeader.getRequestId()).isEqualTo(msoRequestId);
209
210         Struct payload = executionServiceInput.getPayload();
211         Struct requeststruct = payload.getFieldsOrThrow("config-deploy-request").getStructValue();
212
213         assertThat(requeststruct.getFieldsOrThrow("resolution-key").getStringValue()).isEqualTo("PNFDemo");
214         Struct propertiesStruct = requeststruct.getFieldsOrThrow("config-deploy-properties").getStructValue();
215
216         assertThat(propertiesStruct.getFieldsOrThrow("pnf-name").getStringValue()).isEqualTo("PNFDemo");
217         assertThat(propertiesStruct.getFieldsOrThrow("service-model-uuid").getStringValue())
218                 .isEqualTo("f2daaac6-5017-4e1e-96c8-6a27dfbe1421");
219         assertThat(propertiesStruct.getFieldsOrThrow("pnf-customization-uuid").getStringValue())
220                 .isEqualTo("68dc9a92-214c-11e7-93ae-92361f002680");
221
222         /**
223          * IP addresses match the OAM ip addresses from AAI.
224          */
225         assertThat(propertiesStruct.getFieldsOrThrow("pnf-ipv4-address").getStringValue()).isEqualTo("1.1.1.1");
226         assertThat(propertiesStruct.getFieldsOrThrow("pnf-ipv6-address").getStringValue()).isEqualTo("::/128");
227     }
228
229     /**
230      * Mock the Dmaap Rest interface for Pnf topic.
231      */
232     private void mockDmaapForPnf() {
233
234         String pnfResponse = "[{\"correlationId\": \"PNFDemo\",\"key1\":\"value1\"}]";
235
236         /**
237          * Get the events from PNF topic
238          */
239         wireMockServer.stubFor(get(urlPathMatching("/events/pnfReady/consumerGroup.*"))
240                 .willReturn(okJson(pnfResponse).withFixedDelay(DMAAP_DELAY_TIME_MS)));
241     }
242
243     private void mockAai() {
244
245         String aaiResponse = "{\n" + "  \"results\": [\n" + "    {\n"
246                 + "      \"resource-type\": \"service-instance\",\n"
247                 + "      \"resource-link\": \"https://localhost:8443/aai/" + VERSION
248                 + "/business/customers/customer/ADemoCustomerInCiti/service-subscriptions/service-subscription/vCPE/service-instances/service-instance/key3\"\n"
249                 + "    }\n" + "  ]\n" + "}";
250
251         String aaiPnfEntry = "{  \n" + "   \"pnf-name\":\"PNFDemo\",\n" + "   \"pnf-id\":\"testtest\",\n"
252                 + "   \"in-maint\":true,\n" + "   \"resource-version\":\"1541720264047\",\n"
253                 + "   \"ipaddress-v4-oam\":\"1.1.1.1\",\n" + "   \"ipaddress-v6-oam\":\"::/128\"\n" + "}";
254
255         /**
256          * Get the AAI entry for globalCustomerId as specified in the request file.
257          */
258         wireMockServer
259                 .stubFor(get(urlPathMatching("/aai/" + VERSION + "/business/customers/customer/ADemoCustomerInCiti.*"))
260                         .willReturn(ok()));
261
262         /**
263          * PUT the service to AAI with globalCustomerId, service type as specified in the request file. Service instance
264          * id is generated during runtime, REGEX is used to represent the information.
265          */
266         wireMockServer.stubFor(put(urlPathMatching("/aai/" + VERSION
267                 + "/business/customers/customer/ADemoCustomerInCiti/service-subscriptions/service-subscription/vCPE/service-instances/service-instance/.*")));
268
269         wireMockServer.stubFor(get(urlPathMatching("/aai/" + VERSION
270                 + "/business/customers/customer/ADemoCustomerInCiti/service-subscriptions/service-subscription/vCPE/service-instances/service-instance/.*"))
271                         .willReturn(okJson(aaiResponse)));
272
273         /**
274          * Get the service from AAI
275          */
276         wireMockServer.stubFor(get(urlPathMatching("/aai/" + VERSION + "/nodes/service-instances/service-instance/.*"))
277                 .willReturn(okJson(aaiResponse)));
278
279         /**
280          * Put the project as specified in the request file to AAI.
281          */
282         wireMockServer.stubFor(put(urlEqualTo("/aai/" + VERSION + "/business/projects/project/Project-Demonstration")));
283
284         /**
285          * GET the project as specified in the request file to AAI.
286          */
287         wireMockServer
288                 .stubFor(get(urlPathMatching("/aai/" + VERSION + "/business/projects/project/Project-Demonstration"))
289                         .willReturn(ok()));
290
291         /**
292          * PUT the PNF correlation ID to AAI.
293          */
294         wireMockServer.stubFor(put(urlEqualTo("/aai/" + VERSION + "/network/pnfs/pnf/PNFDemo")));
295
296         /**
297          * Get the PNF entry from AAI.
298          */
299         wireMockServer.stubFor(
300                 get(urlEqualTo("/aai/" + VERSION + "/network/pnfs/pnf/PNFDemo")).willReturn(okJson(aaiPnfEntry)));
301
302         /**
303          * Put the PNF relationship
304          */
305         wireMockServer.stubFor(put(urlEqualTo("/aai/" + VERSION
306                 + "/business/projects/project/Project-Demonstration/relationship-list/relationship")));
307     }
308
309     /**
310      * Mock the catalobdb rest interface.
311      */
312     private void mockCatalogDb() {
313
314         String catalogdbClientResponse =
315                 FileUtil.readResourceFile("response/" + getClass().getSimpleName() + "_catalogdb.json");
316
317         /**
318          * Return valid json for the model UUID in the request file.
319          */
320         wireMockServer
321                 .stubFor(get(urlEqualTo("/v2/serviceResources?serviceModelUuid=f2daaac6-5017-4e1e-96c8-6a27dfbe1421"))
322                         .willReturn(okJson(responseObject)));
323
324         /**
325          * Return valid json for the service model InvariantUUID as specified in the request file.
326          */
327         wireMockServer.stubFor(
328                 get(urlEqualTo("/v2/serviceResources?serviceModelInvariantUuid=539b7a2f-9524-4dbf-9eee-f2e05521df3f"))
329                         .willReturn(okJson(responseObject)));
330
331         /**
332          * Return valid spring data rest json for the service model UUID as specified in the request file.
333          */
334         wireMockServer.stubFor(get(urlEqualTo(
335                 "/pnfResourceCustomization/search/findPnfResourceCustomizationByModelUuid?SERVICE_MODEL_UUID=f2daaac6-5017-4e1e-96c8-6a27dfbe1421"))
336                         .willReturn(okJson(catalogdbClientResponse)));
337     }
338
339     private void mockRequestDb() {
340         wireMockServer.stubFor(post(urlEqualTo("/dbadapters/RequestsDbAdapter")).willReturn(ok()));
341     }
342
343 }