PoC Contract Stubs NCMP Rest Endpoints

  Stubs defined for cmHandleSearch and passthrough operational

Issue-ID: CPS-919
Signed-off-by: puthuparambil.aditya <aditya.puthuparambil@bell.ca>
Change-Id: I4fd7bee8fa70ed237f74c721068ffa39817d5c7a
diff --git a/cps-ncmp-rest-stub/pom.xml b/cps-ncmp-rest-stub/pom.xml
new file mode 100644
index 0000000..976e21e
--- /dev/null
+++ b/cps-ncmp-rest-stub/pom.xml
@@ -0,0 +1,190 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ============LICENSE_START=======================================================
+  Copyright (C) 2022 Bell Canada
+  ================================================================================
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+        http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+
+  SPDX-License-Identifier: Apache-2.0
+  ============LICENSE_END=========================================================
+-->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+    <parent>
+        <groupId>org.onap.cps</groupId>
+        <artifactId>cps-parent</artifactId>
+        <version>3.1.0-SNAPSHOT</version>
+        <relativePath>../cps-parent/pom.xml</relativePath>
+    </parent>
+
+    <artifactId>cps-ncmp-rest-stub</artifactId>
+    <properties>
+        <app>org.onap.cps.ncmp.rest.stub.Application</app>
+        <java.version>11</java.version>
+        <jacoco.reportDirectory.aggregate>${project.reporting.outputDirectory}/jacoco-aggregate</jacoco.reportDirectory.aggregate>
+        <sonar.coverage.jacoco.xmlReportPaths>
+            ../jacoco-report/target/site/jacoco-aggregate/jacoco.xml
+        </sonar.coverage.jacoco.xmlReportPaths>
+        <groovy.version>3.0.8</groovy.version>
+        <nexusproxy>https://nexus.onap.org</nexusproxy>
+        <releaseNexusPath>/content/repositories/releases/</releaseNexusPath>
+        <snapshotNexusPath>/content/repositories/snapshots/</snapshotNexusPath>
+        <sonar.skip>true</sonar.skip>
+        <mapstruct.version>1.4.2.Final</mapstruct.version>
+    </properties>
+
+    <dependencies>
+        <dependency>
+            <groupId>io.swagger.core.v3</groupId>
+            <artifactId>swagger-annotations</artifactId>
+            <version>2.1.4</version>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-web</artifactId>
+            <exclusions>
+                <exclusion>
+                    <groupId>org.springframework.boot</groupId>
+                    <artifactId>spring-boot-starter-tomcat</artifactId>
+                </exclusion>
+            </exclusions>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-validation</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-jetty</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.mapstruct</groupId>
+            <artifactId>mapstruct</artifactId>
+            <version>${mapstruct.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.mapstruct</groupId>
+            <artifactId>mapstruct-processor</artifactId>
+            <version>${mapstruct.version}</version>
+        </dependency>
+        <!-- T E S T   D E P E N D E N C I E S -->
+        <dependency>
+            <groupId>org.projectlombok</groupId>
+            <artifactId>lombok</artifactId>
+        </dependency>
+        <dependency>
+            <!-- The SpotBugs Maven plugin uses SLF4J 1.8 beta 2 -->
+            <groupId>org.slf4j</groupId>
+            <artifactId>slf4j-simple</artifactId>
+            <version>1.8.0-beta4</version>
+        </dependency>
+        <dependency>
+            <groupId>com.google.guava</groupId>
+            <artifactId>guava</artifactId>
+            <version>20.0</version>
+        </dependency>
+        <dependency>
+            <groupId>cglib</groupId>
+            <artifactId>cglib-nodep</artifactId>
+            <scope>test</scope>
+            <version>3.1</version>
+        </dependency>
+        <dependency>
+            <groupId>com.google.code.gson</groupId>
+            <artifactId>gson</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.onap.cps</groupId>
+            <artifactId>cps-ncmp-rest</artifactId>
+            <exclusions>
+                <exclusion>
+                    <groupId>org.onap.cps</groupId>
+                    <artifactId>cps-ncmp-service</artifactId>
+                </exclusion>
+            </exclusions>
+            <optional>true</optional>
+        </dependency>
+    </dependencies>
+
+    <build>
+        <plugins>
+            <!-- Swagger code generation. -->
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-compiler-plugin</artifactId>
+                <version>2.3.2</version>
+                <configuration>
+                    <source>${java.version}</source>
+                    <target>${java.version}</target>
+                </configuration>
+            </plugin>
+
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-shade-plugin</artifactId>
+                <dependencies>
+                    <dependency>
+                        <groupId>org.springframework.boot</groupId>
+                        <artifactId>spring-boot-maven-plugin</artifactId>
+                        <version>1.2.7.RELEASE</version>
+                    </dependency>
+                </dependencies>
+                <configuration>
+                    <keepDependenciesWithProvidedScope>true</keepDependenciesWithProvidedScope>
+                    <createDependencyReducedPom>true</createDependencyReducedPom>
+                    <filters>
+                        <filter>
+                            <artifact>*:*</artifact>
+                            <excludes>
+                                <exclude>org/onap/cps/ncmp/rest/controller/**</exclude>
+                            </excludes>
+                        </filter>
+                    </filters>
+                </configuration>
+                <executions>
+                    <execution>
+                        <phase>package</phase>
+                        <goals>
+                            <goal>shade</goal>
+                        </goals>
+                        <configuration>
+                            <transformers>
+                                <transformer
+                                  implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
+                                    <resource>META-INF/spring.handlers</resource>
+                                </transformer>
+                                <transformer
+                                  implementation="org.springframework.boot.maven.PropertiesMergingResourceTransformer">
+                                    <resource>META-INF/spring.factories</resource>
+                                </transformer>
+                                <transformer
+                                  implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
+                                    <resource>META-INF/spring.schemas</resource>
+                                </transformer>
+                                <transformer
+                                  implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
+                                <transformer
+                                  implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
+                                    <mainClass>org/onap/cps/ncmp/rest/stub/Application</mainClass>
+                                </transformer>
+                            </transformers>
+                        </configuration>
+                    </execution>
+                </executions>
+            </plugin>
+        </plugins>
+    </build>
+</project>
diff --git a/cps-ncmp-rest-stub/src/main/java/org/onap/cps/ncmp/rest/stub/Application.java b/cps-ncmp-rest-stub/src/main/java/org/onap/cps/ncmp/rest/stub/Application.java
new file mode 100644
index 0000000..0f0035d
--- /dev/null
+++ b/cps-ncmp-rest-stub/src/main/java/org/onap/cps/ncmp/rest/stub/Application.java
@@ -0,0 +1,32 @@
+/*
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2022 Bell Canada.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.cps.ncmp.rest.stub;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+@SpringBootApplication
+public class Application {
+
+    public static void main(final String[] args) {
+        SpringApplication.run(Application.class, args);
+    }
+}
diff --git a/cps-ncmp-rest-stub/src/main/java/org/onap/cps/ncmp/rest/stub/controller/NetworkCmProxyStubController.java b/cps-ncmp-rest-stub/src/main/java/org/onap/cps/ncmp/rest/stub/controller/NetworkCmProxyStubController.java
new file mode 100644
index 0000000..6362cf1
--- /dev/null
+++ b/cps-ncmp-rest-stub/src/main/java/org/onap/cps/ncmp/rest/stub/controller/NetworkCmProxyStubController.java
@@ -0,0 +1,172 @@
+/*
+ *  ============LICENSE_START=======================================================
+ *  Copyright (C) 2022 Bell Canada
+ *  ================================================================================
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ *  SPDX-License-Identifier: Apache-2.0
+ *  ============LICENSE_END=========================================================
+ */
+
+package org.onap.cps.ncmp.rest.stub.controller;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.UUID;
+import javax.validation.Valid;
+import javax.validation.constraints.NotNull;
+import lombok.extern.slf4j.Slf4j;
+import org.onap.cps.ncmp.rest.api.NetworkCmProxyApi;
+import org.onap.cps.ncmp.rest.model.CmHandleQueryRestParameters;
+import org.onap.cps.ncmp.rest.model.CmHandles;
+import org.onap.cps.ncmp.rest.model.Conditions;
+import org.onap.cps.ncmp.rest.model.RestModuleReference;
+import org.onap.cps.ncmp.rest.model.RestOutputCmHandle;
+import org.onap.cps.ncmp.rest.model.RestOutputCmHandlePublicProperties;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.core.io.ClassPathResource;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+@Slf4j
+@RestController
+@RequestMapping("${rest.api.ncmp-stub-base-path}")
+public class NetworkCmProxyStubController implements NetworkCmProxyApi {
+
+    public static final String ASYNC_REQUEST_ID = "requestId";
+
+    @Value("${stub.path}")
+    private String pathToResponseFiles;
+
+    @Override
+    public ResponseEntity<Void> createResourceDataRunningForCmHandle(@NotNull @Valid final String resourceIdentifier,
+        final String cmHandleId, @Valid final Object body, final String contentType) {
+        return new ResponseEntity<>(HttpStatus.CREATED);
+    }
+
+    @Override
+    public ResponseEntity<Void> deleteResourceDataRunningForCmHandle(final String cmHandleId,
+        @NotNull @Valid final String resourceIdentifier, final String contentType) {
+        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
+    }
+
+    @Override
+    public ResponseEntity<CmHandles> executeCmHandleSearch(@Valid final Conditions body) {
+        final ObjectMapper mapper = new ObjectMapper();
+        CmHandles cmHandles = new CmHandles();
+        // read JSON file and map/convert to java POJO
+        final ClassPathResource resource = new ClassPathResource(pathToResponseFiles + "cmHandlesSearch.json");
+        try (InputStream inputStream = resource.getInputStream()) {
+            final String string = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
+            cmHandles = mapper.readValue(string, CmHandles.class);
+        } catch (final IOException exception) {
+            log.error("Error reading the file.", exception);
+        }
+        return ResponseEntity.ok(cmHandles);
+    }
+
+    @Override
+    public ResponseEntity<RestOutputCmHandlePublicProperties> getCmHandlePublicPropertiesByCmHandleId(
+        final String cmHandleId) {
+        return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
+    }
+
+    @Override
+    public ResponseEntity<List<RestModuleReference>> getModuleReferencesByCmHandle(final String cmHandleId) {
+        return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
+    }
+
+    /**
+     * Get resource data from operational datastore.
+     *
+     * @param cmHandleId cm handle identifier
+     * @param resourceIdentifier resource identifier
+     * @param optionsParamInQuery options query parameter
+     * @param topicParamInQuery topic query parameter
+     * @return {@code ResponseEntity} response from dmi plugin
+     */
+    @Override
+    public ResponseEntity<Object> getResourceDataOperationalForCmHandle(final String cmHandleId,
+        final String resourceIdentifier, final String optionsParamInQuery, final String topicParamInQuery) {
+        final ResponseEntity<Map<String, Object>> asyncResponse = populateAsyncResponse(topicParamInQuery);
+        final Map<String, Object> asyncResponseData = asyncResponse.getBody();
+        final ObjectMapper mapper = new ObjectMapper();
+        Object responseObject = null;
+        // read JSON file and map/convert to java POJO
+        final ClassPathResource resource = new ClassPathResource(pathToResponseFiles
+            + "passthrough-operational-example.json");
+        try (InputStream inputStream = resource.getInputStream()) {
+            final String string = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
+            responseObject = mapper.readValue(string, Object.class);
+        } catch (final IOException exception) {
+            log.error("Error reading the file.", exception);
+        }
+        if (asyncResponseData == null) {
+            return ResponseEntity.ok(responseObject);
+        }
+        return ResponseEntity.ok(asyncResponse);
+
+    }
+
+    @Override
+    public ResponseEntity<Object> getResourceDataRunningForCmHandle(final String cmHandleId,
+        final String resourceIdentifier, final String options, final String topic) {
+        return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
+    }
+
+    @Override
+    public ResponseEntity<Object> patchResourceDataRunningForCmHandle(final String resourceIdentifier,
+        final String cmHandleId, final Object body, final String contentType) {
+        return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
+    }
+
+    @Override
+    public ResponseEntity<List<String>> queryCmHandles(final CmHandleQueryRestParameters body) {
+        return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
+    }
+
+    @Override
+    public ResponseEntity<RestOutputCmHandle> retrieveCmHandleDetailsById(final String cmHandleId) {
+        return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
+    }
+
+    @Override
+    public ResponseEntity<Object> updateResourceDataRunningForCmHandle(@NotNull @Valid final String resourceIdentifier,
+        final String cmHandleId, @Valid final Object body, final String contentType) {
+        return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
+    }
+
+    private ResponseEntity<Map<String, Object>> populateAsyncResponse(final String topicParamInQuery) {
+        final Map<String, Object> responseData;
+        if (topicParamInQuery != null) {
+            responseData = getAsyncResponseData();
+        } else {
+            responseData = null;
+        }
+        return ResponseEntity.ok().body(responseData);
+    }
+
+    private Map<String, Object> getAsyncResponseData() {
+        final Map<String, Object> asyncResponseData = new HashMap<>(1);
+        final String resourceDataRequestId = UUID.randomUUID().toString();
+        asyncResponseData.put(ASYNC_REQUEST_ID, resourceDataRequestId);
+        return asyncResponseData;
+    }
+}
diff --git a/cps-ncmp-rest-stub/src/main/resources/application.yml b/cps-ncmp-rest-stub/src/main/resources/application.yml
new file mode 100644
index 0000000..9fbd359
--- /dev/null
+++ b/cps-ncmp-rest-stub/src/main/resources/application.yml
@@ -0,0 +1,39 @@
+#  ============LICENSE_START=======================================================
+#  Copyright (C) 2022 Bell Canada
+#  ================================================================================
+#  Licensed under the Apache License, Version 2.0 (the "License");
+#  you may not use this file except in compliance with the License.
+#  You may obtain a copy of the License at
+#
+#        http://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing, software
+#  distributed under the License is distributed on an "AS IS" BASIS,
+#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#  See the License for the specific language governing permissions and
+#  limitations under the License.
+#
+#  SPDX-License-Identifier: Apache-2.0
+#  ============LICENSE_END=========================================================
+server:
+    port: 8091
+
+rest:
+    api:
+        ncmp-stub-base-path: /ncmp
+
+spring:
+    main:
+        banner-mode: "off"
+    application:
+        name: "cps-ncmp-rest-stub"
+
+logging:
+    level:
+        org:
+            springframework: INFO
+            onap:
+                cps: INFO
+
+stub:
+    path: "/stubs/"
\ No newline at end of file
diff --git a/cps-ncmp-rest-stub/src/main/resources/stubs/cmHandlesSearch.json b/cps-ncmp-rest-stub/src/main/resources/stubs/cmHandlesSearch.json
new file mode 100644
index 0000000..409d56f
--- /dev/null
+++ b/cps-ncmp-rest-stub/src/main/resources/stubs/cmHandlesSearch.json
@@ -0,0 +1,10 @@
+{
+  "cmHandles": [
+    {
+      "cmHandleId": "stub-cm-handle-id"
+    },
+    {
+      "cmHandleId": "stub-cm-handle-id2"
+    }
+  ]
+}
\ No newline at end of file
diff --git a/cps-ncmp-rest-stub/src/main/resources/stubs/passthrough-operational-example.json b/cps-ncmp-rest-stub/src/main/resources/stubs/passthrough-operational-example.json
new file mode 100644
index 0000000..1838b47
--- /dev/null
+++ b/cps-ncmp-rest-stub/src/main/resources/stubs/passthrough-operational-example.json
@@ -0,0 +1,45 @@
+{
+    "stores:bookstore": {
+        "categories": [
+            {
+                "code": "02",
+                "books": [
+                    {
+                        "title": "A Horror book",
+                        "price": "2000",
+                        "pub_year": 2003,
+                        "lang": "English",
+                        "authors": [
+                            "Joe"
+                        ]
+                    },
+                    {
+                        "title": "Another Horror Book",
+                        "price": "2000",
+                        "pub_year": 2003,
+                        "lang": "English",
+                        "authors": [
+                            "Joe"
+                        ]
+                    }
+                ],
+                "name": "Horror"
+            },
+            {
+                "code": "100",
+                "books": [
+                    {
+                        "title": "A Cook book",
+                        "price": "2011",
+                        "pub_year": 2019,
+                        "lang": "English",
+                        "authors": [
+                            "Rahul"
+                        ]
+                    }
+                ],
+                "name": "Cooking Books"
+            }
+        ]
+    }
+}
\ No newline at end of file
diff --git a/cps-ncmp-rest-stub/src/test/java/org/onap/cps/TestApplication.java b/cps-ncmp-rest-stub/src/test/java/org/onap/cps/TestApplication.java
new file mode 100644
index 0000000..a095edc
--- /dev/null
+++ b/cps-ncmp-rest-stub/src/test/java/org/onap/cps/TestApplication.java
@@ -0,0 +1,30 @@
+/*
+ *  ============LICENSE_START=======================================================
+ *  Copyright (C) 2022 Bell Canada.
+ *  ================================================================================
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ *  SPDX-License-Identifier: Apache-2.0
+ *  ============LICENSE_END=========================================================
+ */
+
+package org.onap.cps;
+
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+/**
+ * The @SpringBootApplication annotated class is required in order to run tests
+ * marked with @SpringBootTest annotation.
+ */
+@SpringBootApplication
+public class TestApplication {
+}