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