import org.onap.policy.models.base.PfModelException;\r
import org.onap.policy.models.base.PfModelRuntimeException;\r
import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;\r
+import org.springframework.context.annotation.Profile;\r
import org.springframework.http.HttpMethod;\r
import org.springframework.http.HttpStatus;\r
import org.springframework.http.ResponseEntity;\r
*/\r
@RestController\r
@RequiredArgsConstructor\r
+@Profile("default")\r
public class ApiRestController extends CommonRestController implements PolicyDesignApi {\r
\r
private enum Target {\r
import org.onap.policy.models.base.PfModelRuntimeException;
import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
+import org.springframework.context.annotation.Profile;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RestController;
*/
@RestController
@RequiredArgsConstructor
+@Profile("default")
public class NodeTemplateController extends CommonRestController implements ToscaNodeTemplateDesignApi {
private final ToscaServiceTemplateService toscaServiceTemplateService;
--- /dev/null
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2023 Nordix Foundation.
+ * ================================================================================
+ * 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.policy.api.main.rest.stub;
+
+import java.util.UUID;
+import javax.validation.Valid;
+import lombok.RequiredArgsConstructor;
+import org.onap.policy.api.main.rest.CommonRestController;
+import org.onap.policy.api.main.rest.PolicyFetchMode;
+import org.onap.policy.api.main.rest.genapi.PolicyDesignApi;
+import org.onap.policy.api.main.rest.provider.statistics.StatisticsReport;
+import org.onap.policy.common.endpoints.report.HealthCheckReport;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
+import org.springframework.context.annotation.Profile;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.RestController;
+
+@RestController
+@RequiredArgsConstructor
+@Profile("stub")
+public class ApiRestControllerStub extends CommonRestController implements PolicyDesignApi {
+
+ private final StubUtils stubUtils;
+
+ @Override
+ public ResponseEntity<ToscaServiceTemplate> createPolicies(
+ @Valid ToscaServiceTemplate body, UUID requestID) {
+ return stubUtils.getStubbedResponse(ToscaServiceTemplate.class);
+ }
+
+ @Override
+ public ResponseEntity<ToscaServiceTemplate> createPolicy(
+ String policyTypeId,
+ String policyTypeVersion,
+ @Valid ToscaServiceTemplate body,
+ UUID requestID) {
+ return stubUtils.getStubbedResponse(ToscaServiceTemplate.class);
+ }
+
+ @Override
+ public ResponseEntity<ToscaServiceTemplate> createPolicyType(
+ @Valid ToscaServiceTemplate body, UUID requestID) {
+ return stubUtils.getStubbedResponse(ToscaServiceTemplate.class);
+ }
+
+ @Override
+ public ResponseEntity<ToscaServiceTemplate> deleteSpecificPolicy(
+ String policyId,
+ String policyVersion,
+ UUID requestID) {
+ return stubUtils.getStubbedResponse(ToscaServiceTemplate.class);
+ }
+
+ @Override
+ public ResponseEntity<ToscaServiceTemplate> deleteSpecificVersionOfPolicy(
+ String policyTypeId,
+ String policyTypeVersion,
+ String policyId,
+ String policyVersion,
+ UUID requestID) {
+ return stubUtils.getStubbedResponse(ToscaServiceTemplate.class);
+ }
+
+ @Override
+ public ResponseEntity<ToscaServiceTemplate> deleteSpecificVersionOfPolicyType(
+ String policyTypeId,
+ String versionId,
+ UUID requestID) {
+ return stubUtils.getStubbedResponse(ToscaServiceTemplate.class);
+ }
+
+ @Override
+ public ResponseEntity<ToscaServiceTemplate> getAllPolicies(
+ String policyTypeId,
+ String policyTypeVersion,
+ @Valid PolicyFetchMode mode,
+ UUID requestID) {
+ return stubUtils.getStubbedResponse(ToscaServiceTemplate.class);
+ }
+
+ @Override
+ public ResponseEntity<ToscaServiceTemplate> getAllPolicyTypes(
+ UUID requestID) {
+ return stubUtils.getStubbedResponse(ToscaServiceTemplate.class);
+ }
+
+ @Override
+ public ResponseEntity<ToscaServiceTemplate> getAllVersionsOfPolicy(
+ String policyId,
+ String policyTypeId,
+ String policyTypeVersion,
+ @Valid PolicyFetchMode mode,
+ UUID requestID) {
+ return stubUtils.getStubbedResponse(ToscaServiceTemplate.class);
+ }
+
+ @Override
+ public ResponseEntity<ToscaServiceTemplate> getAllVersionsOfPolicyType(
+ String policyTypeId, UUID requestID) {
+ return stubUtils.getStubbedResponse(ToscaServiceTemplate.class);
+ }
+
+ @Override
+ public ResponseEntity<HealthCheckReport> getHealthCheck(
+ UUID requestID) {
+ return stubUtils.getStubbedResponse(HealthCheckReport.class);
+ }
+
+ @Override
+ public ResponseEntity<ToscaServiceTemplate> getLatestVersionOfPolicy(
+ String policyId,
+ String policyTypeId,
+ String policyTypeVersion,
+ @Valid PolicyFetchMode mode,
+ UUID requestID) {
+ return stubUtils.getStubbedResponse(ToscaServiceTemplate.class);
+ }
+
+ @Override
+ public ResponseEntity<ToscaServiceTemplate> getLatestVersionOfPolicyType(
+ String policyTypeId, UUID requestID) {
+ return stubUtils.getStubbedResponse(ToscaServiceTemplate.class);
+ }
+
+ @Override
+ public ResponseEntity<ToscaServiceTemplate> getPolicies(
+ @Valid PolicyFetchMode mode, UUID requestID) {
+ return stubUtils.getStubbedResponse(ToscaServiceTemplate.class);
+ }
+
+ @Override
+ public ResponseEntity<ToscaServiceTemplate> getSpecificPolicy(
+ String policyId,
+ String policyVersion,
+ @Valid PolicyFetchMode mode,
+ UUID requestID) {
+ return stubUtils.getStubbedResponse(ToscaServiceTemplate.class);
+ }
+
+ @Override
+ public ResponseEntity<ToscaServiceTemplate> getSpecificVersionOfPolicy(
+ String policyId,
+ String policyTypeId,
+ String policyTypeVersion,
+ String policyVersion,
+ @Valid PolicyFetchMode mode,
+ UUID requestID) {
+ return stubUtils.getStubbedResponse(ToscaServiceTemplate.class);
+ }
+
+ @Override
+ public ResponseEntity<ToscaServiceTemplate> getSpecificVersionOfPolicyType(
+ String policyTypeId,
+ String versionId,
+ UUID requestID) {
+ return stubUtils.getStubbedResponse(ToscaServiceTemplate.class);
+ }
+
+ @Override
+ public ResponseEntity<StatisticsReport> getStatistics(
+ UUID requestID) {
+ return stubUtils.getStubbedResponse(StatisticsReport.class);
+ }
+
+}
--- /dev/null
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2023 Nordix Foundation.
+ * ================================================================================
+ * 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.policy.api.main.rest.stub;
+
+import java.util.List;
+import java.util.UUID;
+import javax.validation.Valid;
+import lombok.RequiredArgsConstructor;
+import org.onap.policy.api.main.rest.CommonRestController;
+import org.onap.policy.api.main.rest.genapi.ToscaNodeTemplateDesignApi;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
+import org.springframework.context.annotation.Profile;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.RestController;
+
+@RestController
+@RequiredArgsConstructor
+@Profile("stub")
+public class NodeTemplateControllerStub extends CommonRestController
+ implements ToscaNodeTemplateDesignApi {
+
+ private final StubUtils stubUtils;
+
+ @Override
+ public ResponseEntity<ToscaServiceTemplate> createToscaNodeTemplates(
+ @Valid ToscaServiceTemplate body, UUID requestID) {
+ return stubUtils.getStubbedResponse(ToscaServiceTemplate.class);
+ }
+
+ @Override
+ public ResponseEntity<ToscaServiceTemplate> deleteToscaNodeTemplates(
+ String name, String version, UUID requestID) {
+ return stubUtils.getStubbedResponse(ToscaServiceTemplate.class);
+ }
+
+ @Override
+ public ResponseEntity<List<ToscaNodeTemplate>> getAllNodeTemplates(
+ UUID requestID) {
+ return stubUtils.getStubbedResponseList(ToscaNodeTemplate.class);
+ }
+
+ @Override
+ public ResponseEntity<List<ToscaNodeTemplate>> getSpecificVersionOfNodeTemplate(
+ String name, String version, UUID requestID) {
+ return stubUtils.getStubbedResponseList(ToscaNodeTemplate.class);
+ }
+
+ @Override
+ public ResponseEntity<ToscaServiceTemplate> updateToscaNodeTemplates(
+ @Valid ToscaServiceTemplate body, UUID requestID) {
+ return stubUtils.getStubbedResponse(ToscaServiceTemplate.class);
+ }
+}
--- /dev/null
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2023 Nordix Foundation.
+ * ================================================================================
+ * 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.policy.api.main.rest.stub;
+
+import com.google.gson.Gson;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.util.Arrays;
+import java.util.List;
+import javax.servlet.http.HttpServletRequest;
+import lombok.RequiredArgsConstructor;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.context.annotation.Profile;
+import org.springframework.core.io.ClassPathResource;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.stereotype.Service;
+
+@RequiredArgsConstructor
+@Service
+@Profile("stub")
+class StubUtils {
+ private static final Logger log = LoggerFactory.getLogger(StubUtils.class);
+ private final HttpServletRequest request;
+ private static final String ACCEPT = "Accept";
+ private static final String TOSCA_NODE_TEMPLATE_RESOURCE =
+ "nodetemplates/nodetemplates.metadatasets.input.tosca.json";
+ private static final Gson JSON_TRANSLATOR = new Gson();
+
+ <T> ResponseEntity<T> getStubbedResponse(Class<T> clazz) {
+ var accept = request.getHeader(ACCEPT);
+ if (accept != null && accept.contains("application/json")) {
+ final var resource = new ClassPathResource(TOSCA_NODE_TEMPLATE_RESOURCE);
+ try (var inputStream = resource.getInputStream()) {
+ final var string = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
+ var targetObject = JSON_TRANSLATOR.fromJson(string, clazz);
+ return new ResponseEntity<>(targetObject, HttpStatus.OK);
+ } catch (IOException e) {
+ log.error("Couldn't serialize response for content type application/json", e);
+ return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
+ }
+ }
+ return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
+ }
+
+ <T> ResponseEntity<List<T>> getStubbedResponseList(Class<T> clazz) {
+ var accept = request.getHeader(ACCEPT);
+ if (accept != null && accept.contains("application/json")) {
+ final var resource = new ClassPathResource(TOSCA_NODE_TEMPLATE_RESOURCE);
+ try (var inputStream = resource.getInputStream()) {
+ final var string = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
+ var targetObject = Arrays.asList(JSON_TRANSLATOR.fromJson(string, clazz));
+ return new ResponseEntity<>(targetObject, HttpStatus.OK);
+ } catch (IOException e) {
+ log.error("Couldn't serialize response for content type application/json", e);
+ return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
+ }
+ }
+ return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
+ }
+
+}
--- /dev/null
+spring:
+ application:
+ name: "policy-api-stub"
\ No newline at end of file
-server:
- port: 6969
-
spring:
+ profiles:
+ active: default
security.user:
name: policyadmin
password: zb!XztG34
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
implicit-strategy: org.onap.policy.common.spring.utils.CustomImplicitNamingStrategy
+server:
+ port: 6969
+ servlet:
+ context-path: /policy/api/v1
+
policy-api:
name: ApiGroup
aaf: false
that, API is also exposed for clients to retrieve healthcheck status of this API
rest service and the statistics report including the counters of API invocation.
paths:
- /policy/api/v1/healthcheck:
+ /healthcheck:
get:
tags:
- "Policy Design"
x-interface info:
api-version: 1.0.0
last-mod-release: Dublin
- /policy/api/v1/nodetemplates:
+ /nodetemplates:
get:
tags:
- "Tosca Node Template Design"
api-version: 1.0.0
last-mod-release: Jakarta
x-codegen-request-body-name: body
- /policy/api/v1/nodetemplates/{name}/versions/{version}:
+ /nodetemplates/{name}/versions/{version}:
get:
tags:
- "Tosca Node Template Design"
x-interface info:
api-version: 1.0.0
last-mod-release: Jakarta
- /policy/api/v1/policies:
+ /policies:
get:
tags:
- "Policy Design"
api-version: 1.0.0
last-mod-release: El Alto
x-codegen-request-body-name: body
- /policy/api/v1/policies/{policyId}/versions/{policyVersion}:
+ /policies/{policyId}/versions/{policyVersion}:
get:
tags:
- "Policy Design"
x-interface info:
api-version: 1.0.0
last-mod-release: Guilin
- /policy/api/v1/policytypes:
+ /policytypes:
get:
tags:
- "Policy Design"
api-version: 1.0.0
last-mod-release: Dublin
x-codegen-request-body-name: body
- /policy/api/v1/policytypes/{policyTypeId}:
+ /policytypes/{policyTypeId}:
get:
tags:
- "Policy Design"
x-interface info:
api-version: 1.0.0
last-mod-release: Dublin
- /policy/api/v1/policytypes/{policyTypeId}/versions/latest:
+ /policytypes/{policyTypeId}/versions/latest:
get:
tags:
- "Policy Design"
x-interface info:
api-version: 1.0.0
last-mod-release: Dublin
- /policy/api/v1/policytypes/{policyTypeId}/versions/{policyTypeVersion}/policies:
+ /policytypes/{policyTypeId}/versions/{policyTypeVersion}/policies:
get:
tags:
- "Policy Design"
api-version: 1.0.0
last-mod-release: Dublin
x-codegen-request-body-name: body
- /policy/api/v1/policytypes/{policyTypeId}/versions/{policyTypeVersion}/policies/{policyId}:
+ /policytypes/{policyTypeId}/versions/{policyTypeVersion}/policies/{policyId}:
get:
tags:
- "Policy Design"
x-interface info:
api-version: 1.0.0
last-mod-release: Dublin
- /policy/api/v1/policytypes/{policyTypeId}/versions/{policyTypeVersion}/policies/{policyId}/versions/latest:
+ /policytypes/{policyTypeId}/versions/{policyTypeVersion}/policies/{policyId}/versions/latest:
get:
tags:
- "Policy Design"
x-interface info:
api-version: 1.0.0
last-mod-release: Dublin
- /policy/api/v1/policytypes/{policyTypeId}/versions/{policyTypeVersion}/policies/{policyId}/versions/{policyVersion}:
+ /policytypes/{policyTypeId}/versions/{policyTypeVersion}/policies/{policyId}/versions/{policyVersion}:
get:
tags:
- "Policy Design"
x-interface info:
api-version: 1.0.0
last-mod-release: Dublin
- /policy/api/v1/policytypes/{policyTypeId}/versions/{versionId}:
+ /policytypes/{policyTypeId}/versions/{versionId}:
get:
tags:
- "Policy Design"
x-interface info:
api-version: 1.0.0
last-mod-release: Dublin
- /policy/api/v1/statistics:
+ /statistics:
get:
tags:
- "Policy Design"
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = PolicyApiApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
-@ActiveProfiles("test")
+@ActiveProfiles({ "test", "default" })
@DirtiesContext(classMode = ClassMode.AFTER_CLASS)
public class TestApiRestServer extends CommonTestRestController {
@RunWith(SpringRunner.class)
@SpringBootTest(classes = PolicyApiApplication.class)
-@ActiveProfiles("test")
+@ActiveProfiles({ "test", "default" })
public class TestApiStatisticsManager {
@Autowired
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = PolicyApiApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
-@ActiveProfiles("test")
+@ActiveProfiles({ "test", "default" })
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
public class TestNodeTemplateController extends CommonTestRestController {
--- /dev/null
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2023 Nordix Foundation.
+ * ================================================================================
+ * 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.policy.api.main.rest.stub;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+import java.io.IOException;
+import javax.ws.rs.core.Response;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.onap.policy.api.main.PolicyApiApplication;
+import org.onap.policy.api.main.rest.utils.CommonTestRestController;
+import org.onap.policy.common.utils.security.SelfSignedKeyStore;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.boot.web.server.LocalServerPort;
+import org.springframework.test.annotation.DirtiesContext;
+import org.springframework.test.annotation.DirtiesContext.ClassMode;
+import org.springframework.test.context.ActiveProfiles;
+import org.springframework.test.context.DynamicPropertyRegistry;
+import org.springframework.test.context.DynamicPropertySource;
+import org.springframework.test.context.junit4.SpringRunner;
+
+@RunWith(SpringRunner.class)
+@SpringBootTest(classes = PolicyApiApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
+@ActiveProfiles({ "test", "stub" })
+@DirtiesContext(classMode = ClassMode.AFTER_CLASS)
+public class ApisRestControllerStubTest extends CommonTestRestController {
+ protected static final String APP_JSON = "application/json";
+ protected static final String APP_YAML = "application/yaml";
+
+ @LocalServerPort
+ private int apiPort;
+
+ private static SelfSignedKeyStore keystore;
+
+
+ @BeforeClass
+ public static void setupParameters() throws IOException, InterruptedException {
+ keystore = new SelfSignedKeyStore();
+ }
+
+ @DynamicPropertySource
+ static void registerPgProperties(DynamicPropertyRegistry registry) {
+ registry.add("server.ssl.enabled", () -> "true");
+ registry.add("server.ssl.key-store", () -> keystore.getKeystoreName());
+ registry.add("server.ssl.key-store-password", () -> SelfSignedKeyStore.KEYSTORE_PASSWORD);
+ registry.add("server.ssl.key-store-type", () -> "PKCS12");
+ registry.add("server.ssl.key-alias", () -> "policy@policy.onap.org");
+ registry.add("server.ssl.key-password", () -> SelfSignedKeyStore.PRIVATE_KEY_PASSWORD);
+ }
+
+ @Test
+ public void testStubbedGet() throws Exception {
+ checkStubJson("policies");
+ checkStubJson("policies/policyname/versions/1.0.2");
+ checkStubJson("nodetemplates");
+ checkStubJson("nodetemplates/k8stemplate/versions/1.0.0");
+ checkStubJson("policytypes");
+ checkStubJson("policytypes/380d5cb1-e43d-45b7-b10b-ebd15dfabd16/versions/latest");
+ checkStubJson("statistics");
+ checkStubJson("healthcheck");
+ }
+
+ private void checkStubJson(String url) throws Exception {
+ var response = super.readResource(url, APP_JSON, apiPort);
+ assertNotNull(response);
+ assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
+ var responseYaml = super.readResource(url, APP_YAML, apiPort);
+ assertEquals(Response.Status.NOT_IMPLEMENTED.getStatusCode(), responseYaml.getStatus());
+ }
+
+}
protected static final String CONTEXT_PATH = "/policy/api/v1/";
protected void testSwagger(final int apiPort) throws Exception {
- final Invocation.Builder invocationBuilder = sendHttpsRequest("/", "v3/api-docs", APP_JSON, apiPort);
+ final Invocation.Builder invocationBuilder = sendHttpsRequest(CONTEXT_PATH, "v3/api-docs", APP_JSON, apiPort);
final String resp = invocationBuilder.get(String.class);
assertTrue((resp).contains("{\"openapi\":\"3.0.1\",\"info\":{\"title\":\"Policy Framework Lifecycle API\""));
}