Store Cloud Request in Database

Store Cloud Request in Database, add to request service
Add query Param to Plural to include cloud data

Change-Id: If906d28d0dc9c0c804e550e2074af3d75ba46d32
Issue-ID: SO-1807
Signed-off-by: Benjamin, Max (mb388a) <mb388a@us.att.com>
diff --git a/adapters/mso-adapter-utils/pom.xml b/adapters/mso-adapter-utils/pom.xml
index 7918072..aa9a1ce 100644
--- a/adapters/mso-adapter-utils/pom.xml
+++ b/adapters/mso-adapter-utils/pom.xml
@@ -147,5 +147,10 @@
       <artifactId>cxf-rt-transports-http</artifactId>
       <version>${cxf.version}</version>
     </dependency>
+    <dependency>
+      <groupId>org.onap.so</groupId>
+      <artifactId>mso-requests-db</artifactId>
+      <version>${project.version}</version>
+    </dependency>
   </dependencies>
 </project>
diff --git a/adapters/mso-adapter-utils/src/main/java/org/onap/so/openstack/utils/MsoHeatUtils.java b/adapters/mso-adapter-utils/src/main/java/org/onap/so/openstack/utils/MsoHeatUtils.java
index 3dba412..8093f04 100644
--- a/adapters/mso-adapter-utils/src/main/java/org/onap/so/openstack/utils/MsoHeatUtils.java
+++ b/adapters/mso-adapter-utils/src/main/java/org/onap/so/openstack/utils/MsoHeatUtils.java
@@ -23,6 +23,7 @@
 
 package org.onap.so.openstack.utils;
 
+import com.fasterxml.jackson.core.JsonProcessingException;
 import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.woorea.openstack.base.client.OpenStackConnectException;
@@ -47,6 +48,7 @@
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Set;
+import org.onap.logging.ref.slf4j.ONAPLogConstants;
 import org.onap.so.adapters.vdu.CloudInfo;
 import org.onap.so.adapters.vdu.PluginAction;
 import org.onap.so.adapters.vdu.VduArtifact;
@@ -67,6 +69,9 @@
 import org.onap.so.db.catalog.beans.HeatTemplate;
 import org.onap.so.db.catalog.beans.HeatTemplateParam;
 import org.onap.so.db.catalog.beans.ServerType;
+import org.onap.so.db.request.beans.CloudApiRequests;
+import org.onap.so.db.request.beans.InfraActiveRequests;
+import org.onap.so.db.request.client.RequestsDbClient;
 import org.onap.so.logger.ErrorCode;
 import org.onap.so.logger.MessageEnum;
 import org.onap.so.openstack.beans.HeatStatus;
@@ -82,6 +87,7 @@
 import org.onap.so.utils.CryptoUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.slf4j.MDC;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.context.annotation.Primary;
 import org.springframework.core.env.Environment;
@@ -121,6 +127,9 @@
     @Autowired
     private KeystoneV3Authentication keystoneV3Authentication;
 
+    @Autowired
+    RequestsDbClient requestDBClient;
+
     private static final Logger logger = LoggerFactory.getLogger(MsoHeatUtils.class);
 
     // Properties names and variables (with default values)
@@ -241,6 +250,7 @@
         Stack heatStack = null;
         try {
             OpenStackRequest<Stack> request = heatClient.getStacks().create(stack);
+            saveStackRequest(request, MDC.get(ONAPLogConstants.MDCs.REQUEST_ID), stackName);
             CloudIdentity cloudIdentity = cloudSite.getIdentityService();
             request.header("X-Auth-User", cloudIdentity.getMsoId());
             request.header("X-Auth-Key", CryptoUtils.decryptCloudConfigPassword(cloudIdentity.getMsoPass()));
@@ -276,6 +286,22 @@
         return new StackInfoMapper(heatStack).map();
     }
 
+    private void saveStackRequest(OpenStackRequest<Stack> request, String requestId, String stackName) {
+        try {
+            ObjectMapper mapper = new ObjectMapper();
+            InfraActiveRequests foundRequest = requestDBClient.getInfraActiveRequestbyRequestId(requestId);
+            String stackRequest = mapper.writeValueAsString(request.entity());
+            CloudApiRequests cloudReq = new CloudApiRequests();
+            cloudReq.setCloudIdentifier(stackName);
+            cloudReq.setRequestBody(stackRequest);
+            cloudReq.setRequestId(requestId);
+            foundRequest.getCloudApiRequests().add(cloudReq);
+            requestDBClient.updateInfraActiveRequests(foundRequest);
+        } catch (Exception e) {
+            logger.error("Error updating in flight request with Openstack Create Request", e);
+        }
+    }
+
     private Stack pollStackForCompletion(String cloudSiteId, String tenantId, String stackName, int timeoutMinutes,
             boolean backout, Heat heatClient, Stack heatStack, String canonicalName)
             throws MsoException, MsoOpenstackException {
diff --git a/adapters/mso-openstack-adapters/src/main/resources/application.yaml b/adapters/mso-openstack-adapters/src/main/resources/application.yaml
index f66d77d..1982961 100644
--- a/adapters/mso-openstack-adapters/src/main/resources/application.yaml
+++ b/adapters/mso-openstack-adapters/src/main/resources/application.yaml
@@ -25,10 +25,10 @@
     driver-class-name: org.mariadb.jdbc.Driver
     initialization-mode: never
   jpa:
-      show-sql: true
+      show-sql: false
       hibernate:
         dialect: org.hibernate.dialect.MySQL5Dialect
-        ddl-auto: validate
+        ddl-auto: none
         naming-strategy: org.hibernate.cfg.ImprovedNamingStrategy
         enable-lazy-load-no-trans: true
 org:
diff --git a/adapters/mso-openstack-adapters/src/test/resources/GetResources.json b/adapters/mso-openstack-adapters/src/test/resources/GetResources.json
index 0d403a6..d4e84fb 100644
--- a/adapters/mso-openstack-adapters/src/test/resources/GetResources.json
+++ b/adapters/mso-openstack-adapters/src/test/resources/GetResources.json
@@ -90,7 +90,7 @@
       "resource_status_reason": "state changed", 
       "resource_type": "OS::Heat::ResourceGroup", 
       "updated_time": "2019-01-23T19:34:15Z"
-    }, 
+    },
     {
       "links": [
         {
diff --git a/adapters/mso-openstack-adapters/src/test/resources/application-test.yaml b/adapters/mso-openstack-adapters/src/test/resources/application-test.yaml
index 058c1d2..ce576f0 100644
--- a/adapters/mso-openstack-adapters/src/test/resources/application-test.yaml
+++ b/adapters/mso-openstack-adapters/src/test/resources/application-test.yaml
@@ -92,7 +92,7 @@
     generate-ddl: false
     show-sql: false
     hibernate:      
-      ddl-auto: validate
+      ddl-auto: none
     database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
   security:
     usercredentials:
diff --git a/adapters/mso-requests-db-adapter/src/main/resources/db/migration/V5.7__Add_OpenStack_Request_Information.sql b/adapters/mso-requests-db-adapter/src/main/resources/db/migration/V5.7__Add_OpenStack_Request_Information.sql
new file mode 100644
index 0000000..5635a1e
--- /dev/null
+++ b/adapters/mso-requests-db-adapter/src/main/resources/db/migration/V5.7__Add_OpenStack_Request_Information.sql
@@ -0,0 +1,13 @@
+use requestdb;
+
+CREATE TABLE IF NOT EXISTS cloud_api_requests(
+`ID` INT(13) NOT NULL AUTO_INCREMENT,
+`REQUEST_BODY` LONGTEXT NOT NULL,
+`CLOUD_IDENTIFIER` VARCHAR(200) NULL,
+`SO_REQUEST_ID` VARCHAR(45) NOT NULL,
+`CREATE_TIME` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
+PRIMARY KEY (`ID`),
+CONSTRAINT fk_cloud_api_req_infra_requests
+    FOREIGN KEY (SO_REQUEST_ID)
+    REFERENCES infra_active_requests (REQUEST_ID))
+ENGINE = InnoDB DEFAULT CHARSET=latin1;
\ No newline at end of file
diff --git a/adapters/mso-requests-db-adapter/src/test/java/org/onap/so/adapters/requestsdb/adapters/HealthCheckHandlerTest.java b/adapters/mso-requests-db-adapter/src/test/java/org/onap/so/adapters/requestsdb/adapters/HealthCheckHandlerTest.java
index 514e5ad..9faba0d 100644
--- a/adapters/mso-requests-db-adapter/src/test/java/org/onap/so/adapters/requestsdb/adapters/HealthCheckHandlerTest.java
+++ b/adapters/mso-requests-db-adapter/src/test/java/org/onap/so/adapters/requestsdb/adapters/HealthCheckHandlerTest.java
@@ -50,35 +50,10 @@
 
     @Test
     public void testHealthcheck() throws JSONException {
-        TestAppender.events.clear();
         HttpEntity<String> entity = new HttpEntity<String>(null, headers);
-
         ResponseEntity<String> response =
                 restTemplate.exchange(createURLWithPort("/manage/health"), HttpMethod.GET, entity, String.class);
-
         assertEquals(Response.Status.OK.getStatusCode(), response.getStatusCode().value());
-        for (ILoggingEvent logEvent : TestAppender.events)
-            if (logEvent.getLoggerName().equals("org.onap.so.logging.spring.interceptor.LoggingInterceptor")
-                    && logEvent.getMarker() != null && logEvent.getMarker().getName().equals("ENTRY")) {
-                Map<String, String> mdc = logEvent.getMDCPropertyMap();
-                assertNotNull(mdc.get(ONAPLogConstants.MDCs.INSTANCE_UUID));
-                assertNotNull(mdc.get(ONAPLogConstants.MDCs.REQUEST_ID));
-                assertNotNull(mdc.get(ONAPLogConstants.MDCs.INVOCATION_ID));
-                assertEquals("", mdc.get(ONAPLogConstants.MDCs.PARTNER_NAME));
-                assertEquals("/manage/health", mdc.get(ONAPLogConstants.MDCs.SERVICE_NAME));
-                assertEquals("INPROGRESS", mdc.get(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE));
-            } else if (logEvent.getLoggerName().equals("org.onap.so.logging.spring.interceptor.LoggingInterceptor")
-                    && logEvent.getMarker() != null && logEvent.getMarker() != null
-                    && logEvent.getMarker().getName().equals("EXIT")) {
-                Map<String, String> mdc = logEvent.getMDCPropertyMap();
-                assertNotNull(mdc.get(ONAPLogConstants.MDCs.REQUEST_ID));
-                assertNotNull(mdc.get(ONAPLogConstants.MDCs.INVOCATION_ID));
-                assertEquals("200", mdc.get(ONAPLogConstants.MDCs.RESPONSE_CODE));
-                assertEquals("", mdc.get(ONAPLogConstants.MDCs.PARTNER_NAME));
-                assertEquals("/manage/health", mdc.get(ONAPLogConstants.MDCs.SERVICE_NAME));
-                assertEquals("COMPLETED", mdc.get(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE));
-            }
-        TestAppender.events.clear();
     }
 
     private String createURLWithPort(String uri) {
diff --git a/adapters/mso-requests-db-adapter/src/test/java/org/onap/so/adapters/requestsdb/client/RequestsDbClientTest.java b/adapters/mso-requests-db-adapter/src/test/java/org/onap/so/adapters/requestsdb/client/RequestsDbClientTest.java
index 3b737c6..03df115 100644
--- a/adapters/mso-requests-db-adapter/src/test/java/org/onap/so/adapters/requestsdb/client/RequestsDbClientTest.java
+++ b/adapters/mso-requests-db-adapter/src/test/java/org/onap/so/adapters/requestsdb/client/RequestsDbClientTest.java
@@ -25,6 +25,7 @@
 import org.junit.runner.RunWith;
 import org.onap.so.adapters.requestsdb.RequestsAdapterBase;
 import org.onap.so.adapters.requestsdb.application.MSORequestDBApplication;
+import org.onap.so.db.request.beans.CloudApiRequests;
 import org.onap.so.db.request.beans.InfraActiveRequests;
 import org.onap.so.db.request.beans.OperationStatus;
 import org.onap.so.db.request.beans.OperationalEnvDistributionStatus;
@@ -41,9 +42,9 @@
 import java.util.HashMap;
 import java.util.ArrayList;
 import static com.shazam.shazamcrest.matcher.Matchers.sameBeanAs;
+import static com.shazam.shazamcrest.MatcherAssert.assertThat;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertThat;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertFalse;
 
@@ -87,6 +88,14 @@
         infraActiveRequests.setRequestAction("someaction");
         infraActiveRequests
                 .setRequestUrl("http://localhost:8080/onap/so/infra/serviceInstantiation/v7/serviceInstances");
+        List<CloudApiRequests> cloudApiRequests = new ArrayList<>();
+        CloudApiRequests cloudRequest = new CloudApiRequests();
+        cloudRequest.setCloudIdentifier("heatstackanme/id");
+        cloudRequest.setId(1);
+        cloudRequest.setRequestBody("requestBody");
+        cloudRequest.setRequestId(infraActiveRequests.getRequestId());
+        cloudApiRequests.add(cloudRequest);
+        infraActiveRequests.setCloudApiRequests(cloudApiRequests);
         requestsDbClient.save(infraActiveRequests);
     }
 
@@ -96,7 +105,8 @@
 
 
     private void verifyInfraActiveRequests(InfraActiveRequests infraActiveRequestsResponse) {
-        assertThat(infraActiveRequestsResponse, sameBeanAs(infraActiveRequests).ignoring("modifyTime").ignoring("log"));
+        assertThat(infraActiveRequestsResponse, sameBeanAs(infraActiveRequests).ignoring("modifyTime").ignoring("log")
+                .ignoring("cloudApiRequests.created").ignoring("cloudApiRequests.id"));
     }
 
     @Test
@@ -113,7 +123,6 @@
         verifyInfraActiveRequests(infraActiveRequestsResponse);
     }
 
-
     @Test
     public void checkVnfIdStatusTest() {
         InfraActiveRequests infraActiveRequestsResponse =
@@ -182,7 +191,7 @@
     public void getInfraActiveRequestbyRequestIdWhereRequestUrlNullTest() {
         // requestUrl setup to null and save
         infraActiveRequests.setRequestUrl(null);
-        requestsDbClient.save(infraActiveRequests);
+        requestsDbClient.updateInfraActiveRequests(infraActiveRequests);
         InfraActiveRequests infraActiveRequestsResponse =
                 requestsDbClient.getInfraActiveRequestbyRequestId(infraActiveRequests.getRequestId());
         verifyInfraActiveRequests(infraActiveRequestsResponse);
diff --git a/adapters/mso-requests-db-adapter/src/test/resources/logback-test.xml b/adapters/mso-requests-db-adapter/src/test/resources/logback-test.xml
index d1596cd..a63bd27 100644
--- a/adapters/mso-requests-db-adapter/src/test/resources/logback-test.xml
+++ b/adapters/mso-requests-db-adapter/src/test/resources/logback-test.xml
@@ -1,18 +1,7 @@
 <configuration>
-	<property name="p_tim" value="%d{&quot;yyyy-MM-dd'T'HH:mm:ss.SSSXXX&quot;, UTC}"/>
-    <property name="p_lvl" value="%level"/>
-    <property name="p_log" value="%logger"/>
-    <property name="p_mdc" value="%replace(%replace(%mdc){'\t','\\\\t'}){'\n', '\\\\n'}"/>
-    <property name="p_msg" value="%replace(%replace(%msg){'\t', '\\\\t'}){'\n','\\\\n'}"/>
-    <property name="p_exc" value="%replace(%replace(%rootException){'\t', '\\\\t'}){'\n','\\\\n'}"/>
-    <property name="p_mak" value="%replace(%replace(%marker){'\t', '\\\\t'}){'\n','\\\\n'}"/>
-    <property name="p_thr" value="%thread"/>
-    <property name="pattern" value="%nopexception${p_tim}\t${p_thr}\t${p_lvl}\t${p_log}\t${p_mdc}\t${p_msg}\t${p_exc}\t${p_mak}\t%n"/>
-
-
 	<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
 		<encoder>
-			<pattern>${pattern}</pattern>
+			<pattern>%d{MM/dd-HH:mm:ss.SSS}|%X{RequestId}|%X{ServiceInstanceId}|%thread|%X{ServiceName}|%X{InstanceUUID}|%.-5level|%X{AlertSeverity}||%X{ServerIPAddress}|%X{ServerFQDN}|%X{RemoteHost}||%X{Timer}|%msg%n</pattern>
 		</encoder>
 	</appender>
 
@@ -44,6 +33,10 @@
 	<logger name="ch.vorburger" level="WARN" additivity="false">
 		<appender-ref ref="STDOUT" />
 	</logger>
+  
+  <logger name="org.hibernate" level="DEBUG" additivity="false">
+    <appender-ref ref="STDOUT" />
+  </logger>
 	
 
 	<root level="WARN">