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