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