aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAjith Sreekumar <ajith.sreekumar@bell.ca>2022-02-14 14:11:41 +0000
committerGerrit Code Review <gerrit@onap.org>2022-02-14 14:11:41 +0000
commitb1f5a9439f285e16c055e50ce12c65a6623f21a0 (patch)
treee9f7d0e95c5583aa1229e9aa49ec0730e12880c8
parent0fdbdd8200b8a16243ce96a2a039dbc3922d3171 (diff)
parentf5fda6421dad76d369774c59e89a448a6c4e8c07 (diff)
Merge "Add yaml support to pap api's"
-rw-r--r--main/src/main/java/org/onap/policy/pap/main/config/WebConfig.java43
-rw-r--r--main/src/main/java/org/onap/policy/pap/main/config/converter/YamlHttpMessageConverter.java109
-rw-r--r--main/src/test/java/org/onap/policy/pap/main/rest/CommonPapRestServer.java29
-rw-r--r--main/src/test/java/org/onap/policy/pap/main/rest/e2e/PdpGroupCreateOrUpdateTest.java20
-rw-r--r--main/src/test/resources/e2e/createGroups.yaml31
5 files changed, 221 insertions, 11 deletions
diff --git a/main/src/main/java/org/onap/policy/pap/main/config/WebConfig.java b/main/src/main/java/org/onap/policy/pap/main/config/WebConfig.java
new file mode 100644
index 00000000..751ca611
--- /dev/null
+++ b/main/src/main/java/org/onap/policy/pap/main/config/WebConfig.java
@@ -0,0 +1,43 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2022 Bell Canada. All rights reserved.
+ * ================================================================================
+ * 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.pap.main.config;
+
+import java.util.Arrays;
+import java.util.List;
+import org.onap.policy.pap.main.config.converter.YamlHttpMessageConverter;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.http.MediaType;
+import org.springframework.http.converter.HttpMessageConverter;
+import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
+
+/**
+ * Register custom converters to Spring configuration.
+ */
+@Configuration
+public class WebConfig implements WebMvcConfigurer {
+
+ @Override
+ public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
+ YamlHttpMessageConverter yamlConverter = new YamlHttpMessageConverter();
+ yamlConverter.setSupportedMediaTypes(Arrays.asList(MediaType.parseMediaType("application/yaml")));
+ converters.add(yamlConverter);
+ }
+} \ No newline at end of file
diff --git a/main/src/main/java/org/onap/policy/pap/main/config/converter/YamlHttpMessageConverter.java b/main/src/main/java/org/onap/policy/pap/main/config/converter/YamlHttpMessageConverter.java
new file mode 100644
index 00000000..5678b837
--- /dev/null
+++ b/main/src/main/java/org/onap/policy/pap/main/config/converter/YamlHttpMessageConverter.java
@@ -0,0 +1,109 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2022 Bell Canada. All rights reserved.
+ * ================================================================================
+ * 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.pap.main.config.converter;
+
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.OutputStreamWriter;
+import java.io.Reader;
+import java.io.Writer;
+import java.lang.reflect.Type;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+import org.onap.policy.common.utils.coder.YamlJsonTranslator;
+import org.springframework.core.GenericTypeResolver;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpInputMessage;
+import org.springframework.http.HttpOutputMessage;
+import org.springframework.http.MediaType;
+import org.springframework.http.converter.AbstractGenericHttpMessageConverter;
+import org.springframework.http.converter.HttpMessageNotReadableException;
+import org.springframework.http.converter.HttpMessageNotWritableException;
+import org.springframework.lang.Nullable;
+
+/**
+ * Custom converter to marshal/unmarshall data structured with YAML media type.
+ */
+public class YamlHttpMessageConverter extends AbstractGenericHttpMessageConverter<Object> {
+
+ public static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;
+
+ private static final YamlJsonTranslator TRANSLATOR = new YamlJsonTranslator();
+
+ public YamlHttpMessageConverter() {
+ super(new MediaType("application", "yaml"));
+ setDefaultCharset(DEFAULT_CHARSET);
+ }
+
+ @Override
+ public final Object read(Type type, @Nullable Class<?> contextClass, HttpInputMessage inputMessage)
+ throws IOException {
+ return readResolved(GenericTypeResolver.resolveType(type, contextClass), inputMessage);
+ }
+
+ @Override
+ protected final Object readInternal(Class<?> clazz, HttpInputMessage inputMessage) throws IOException {
+ return readResolved(clazz, inputMessage);
+ }
+
+ private Object readInternal(Type resolvedType, Reader reader) {
+ Class<?> clazz = (Class<?>) resolvedType;
+ return TRANSLATOR.fromYaml(reader, clazz);
+ }
+
+ @Override
+ protected final void writeInternal(Object object, @Nullable Type type, HttpOutputMessage outputMessage)
+ throws IOException {
+ var writer = getWriter(outputMessage);
+ try {
+ writeInternal(object, type, writer);
+ } catch (Exception ex) {
+ throw new HttpMessageNotWritableException("Could not write YAML: " + ex.getMessage(), ex);
+ }
+ writer.flush();
+ }
+
+ private void writeInternal(Object object, @Nullable Type type, Writer writer) {
+ TRANSLATOR.toYaml(writer, object);
+ }
+
+ private Object readResolved(Type resolvedType, HttpInputMessage inputMessage) throws IOException {
+ var reader = getReader(inputMessage);
+ try {
+ return readInternal(resolvedType, reader);
+ } catch (Exception ex) {
+ throw new HttpMessageNotReadableException("Could not read YAML: " + ex.getMessage(), ex, inputMessage);
+ }
+ }
+
+ private static Reader getReader(HttpInputMessage inputMessage) throws IOException {
+ return new InputStreamReader(inputMessage.getBody(), getCharset(inputMessage.getHeaders()));
+ }
+
+ private static Writer getWriter(HttpOutputMessage outputMessage) throws IOException {
+ return new OutputStreamWriter(outputMessage.getBody(), getCharset(outputMessage.getHeaders()));
+ }
+
+ private static Charset getCharset(HttpHeaders headers) {
+ Charset charset = (headers.getContentType() == null ? null : headers.getContentType().getCharset());
+ return (charset != null ? charset : DEFAULT_CHARSET);
+ }
+} \ No newline at end of file
diff --git a/main/src/test/java/org/onap/policy/pap/main/rest/CommonPapRestServer.java b/main/src/test/java/org/onap/policy/pap/main/rest/CommonPapRestServer.java
index e42cfd21..c2d9f038 100644
--- a/main/src/test/java/org/onap/policy/pap/main/rest/CommonPapRestServer.java
+++ b/main/src/test/java/org/onap/policy/pap/main/rest/CommonPapRestServer.java
@@ -46,6 +46,7 @@ import org.junit.BeforeClass;
import org.junit.runner.RunWith;
import org.onap.policy.common.endpoints.event.comm.TopicEndpointManager;
import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
+import org.onap.policy.common.endpoints.http.server.YamlMessageBodyHandler;
import org.onap.policy.common.gson.GsonMessageBodyHandler;
import org.onap.policy.common.utils.network.NetworkUtil;
import org.onap.policy.common.utils.security.SelfSignedKeyStore;
@@ -143,7 +144,8 @@ public abstract class CommonPapRestServer {
* @throws Exception if an error occurs
*/
protected void testSwagger(final String endpoint) throws Exception {
- final Invocation.Builder invocationBuilder = sendFqeRequest(httpsPrefix + "v2/api-docs", true);
+ final Invocation.Builder invocationBuilder =
+ sendFqeRequest(httpsPrefix + "v2/api-docs", true, MediaType.APPLICATION_JSON);
final String resp = invocationBuilder.get(String.class);
assertTrue(resp.contains(ENDPOINT_PREFIX + endpoint));
}
@@ -198,7 +200,19 @@ public abstract class CommonPapRestServer {
* @throws Exception if an error occurs
*/
protected Invocation.Builder sendRequest(final String endpoint) throws Exception {
- return sendFqeRequest(httpsPrefix + ENDPOINT_PREFIX + endpoint, true);
+ return sendFqeRequest(httpsPrefix + ENDPOINT_PREFIX + endpoint, true, MediaType.APPLICATION_JSON);
+ }
+
+ /**
+ * Sends a request to an endpoint.
+ *
+ * @param endpoint the target endpoint
+ * @param mediaType the media type for the request
+ * @return a request builder
+ * @throws Exception if an error occurs
+ */
+ protected Invocation.Builder sendRequest(final String endpoint, String mediaType) throws Exception {
+ return sendFqeRequest(httpsPrefix + ENDPOINT_PREFIX + endpoint, true, mediaType);
}
/**
@@ -209,7 +223,7 @@ public abstract class CommonPapRestServer {
* @throws Exception if an error occurs
*/
protected Invocation.Builder sendNoAuthRequest(final String endpoint) throws Exception {
- return sendFqeRequest(httpsPrefix + ENDPOINT_PREFIX + endpoint, false);
+ return sendFqeRequest(httpsPrefix + ENDPOINT_PREFIX + endpoint, false, MediaType.APPLICATION_JSON);
}
/**
@@ -220,8 +234,8 @@ public abstract class CommonPapRestServer {
* @return a request builder
* @throws Exception if an error occurs
*/
- protected Invocation.Builder sendFqeRequest(final String fullyQualifiedEndpoint, boolean includeAuth)
- throws Exception {
+ protected Invocation.Builder sendFqeRequest(final String fullyQualifiedEndpoint, boolean includeAuth,
+ String mediaType) throws Exception {
final SSLContext sc = SSLContext.getInstance("TLSv1.2");
sc.init(null, NetworkUtil.getAlwaysTrustingManager(), new SecureRandom());
final ClientBuilder clientBuilder =
@@ -229,7 +243,8 @@ public abstract class CommonPapRestServer {
final Client client = clientBuilder.build();
client.property(ClientProperties.METAINF_SERVICES_LOOKUP_DISABLE, "true");
- client.register(GsonMessageBodyHandler.class);
+ client.register((mediaType.equalsIgnoreCase(MediaType.APPLICATION_JSON) ? GsonMessageBodyHandler.class
+ : YamlMessageBodyHandler.class));
if (includeAuth) {
final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("policyadmin", "zb!XztG34");
@@ -238,6 +253,6 @@ public abstract class CommonPapRestServer {
final WebTarget webTarget = client.target(fullyQualifiedEndpoint);
- return webTarget.request(MediaType.APPLICATION_JSON);
+ return webTarget.request(mediaType);
}
}
diff --git a/main/src/test/java/org/onap/policy/pap/main/rest/e2e/PdpGroupCreateOrUpdateTest.java b/main/src/test/java/org/onap/policy/pap/main/rest/e2e/PdpGroupCreateOrUpdateTest.java
index 3f763863..650dd41a 100644
--- a/main/src/test/java/org/onap/policy/pap/main/rest/e2e/PdpGroupCreateOrUpdateTest.java
+++ b/main/src/test/java/org/onap/policy/pap/main/rest/e2e/PdpGroupCreateOrUpdateTest.java
@@ -77,18 +77,30 @@ public class PdpGroupCreateOrUpdateTest extends End2EndBase {
}
@Test
- public void testCreateGroups() throws Exception {
+ public void testCreateGroupsJson() throws Exception {
+ createPdpGroups("createGroups.json", MediaType.APPLICATION_JSON);
+ }
+
+ @Test
+ public void testCreateGroupsYaml() throws Exception {
+
+ createPdpGroups("createGroups.yaml", "application/yaml");
+ }
+
+ private void createPdpGroups(String fileName, String mediaType) throws Exception, InterruptedException {
context.addPdp("pdpAA_1", CREATE_SUBGROUP);
context.addPdp("pdpAA_2", CREATE_SUBGROUP);
context.addPdp("pdpAB_1", "pdpTypeB");
context.startThreads();
- Invocation.Builder invocationBuilder = sendRequest(CREATEORUPDATE_GROUPS_ENDPOINT);
+ Invocation.Builder invocationBuilder = sendRequest(CREATEORUPDATE_GROUPS_ENDPOINT, mediaType);
- PdpGroups groups = loadJsonFile("createGroups.json", PdpGroups.class);
- Entity<PdpGroups> entity = Entity.entity(groups, MediaType.APPLICATION_JSON);
+ PdpGroups groups = (mediaType.equalsIgnoreCase(MediaType.APPLICATION_JSON)
+ ? loadJsonFile(fileName, PdpGroups.class)
+ : loadYamlFile(fileName, PdpGroups.class));
+ Entity<PdpGroups> entity = Entity.entity(groups, mediaType);
Response rawresp = invocationBuilder.post(entity);
PdpGroupUpdateResponse resp = rawresp.readEntity(PdpGroupUpdateResponse.class);
assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
diff --git a/main/src/test/resources/e2e/createGroups.yaml b/main/src/test/resources/e2e/createGroups.yaml
new file mode 100644
index 00000000..fc98e971
--- /dev/null
+++ b/main/src/test/resources/e2e/createGroups.yaml
@@ -0,0 +1,31 @@
+groups:
+ - name: createGroups
+ pdpGroupState: PASSIVE
+ properties:
+ hello: world
+ pdpSubgroups:
+ - pdpType: pdpTypeA
+ desiredInstanceCount: 2
+ properties: {}
+ pdpInstances:
+ - instanceId: pdpAA_1
+ pdpState: ACTIVE
+ healthy: HEALTHY
+ - instanceId: pdpAA_2
+ pdpState: ACTIVE
+ healthy: HEALTHY
+ supportedPolicyTypes:
+ - name: onap.policies.monitoring.cdap.tca.hi.lo.app
+ version: 1.0.0
+ policies: []
+ - pdpType: pdpTypeB
+ desiredInstanceCount: 1
+ properties: {}
+ pdpInstances:
+ - instanceId: pdpAB_1
+ pdpState: ACTIVE
+ healthy: HEALTHY
+ supportedPolicyTypes:
+ - name: onap.policies.monitoring.cdap.tca.hi.lo.app
+ version: 1.0.0
+ policies: []
874'>874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228