/*
* ============LICENSE_START=======================================================
- * Copyright (C) 2022 Nordix Foundation
+ * Copyright (C) 2022-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.
package org.onap.cps.ncmp.api.impl.utils;
-import static org.onap.cps.ncmp.api.impl.operations.RequiredDmiService.DATA;
-
import java.util.HashMap;
+import java.util.LinkedHashMap;
import java.util.Map;
-import lombok.RequiredArgsConstructor;
+import lombok.NoArgsConstructor;
import org.apache.logging.log4j.util.Strings;
-import org.apache.logging.log4j.util.TriConsumer;
-import org.onap.cps.ncmp.api.impl.config.NcmpConfiguration;
-import org.onap.cps.ncmp.api.impl.operations.DmiOperations;
-import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
-import org.onap.cps.spi.utils.CpsValidator;
-import org.springframework.stereotype.Component;
-import org.springframework.util.LinkedMultiValueMap;
-import org.springframework.util.MultiValueMap;
import org.springframework.web.util.UriComponentsBuilder;
-@Component
-@RequiredArgsConstructor
+@NoArgsConstructor
public class DmiServiceUrlBuilder {
- private final NcmpConfiguration.DmiProperties dmiProperties;
- private final CpsValidator cpsValidator;
+ private static final String FIXED_PATH_SEGMENT = null;
+
+ final UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.newInstance();
+ final Map<String, Object> pathSegments = new LinkedHashMap<>();
+
+ public static DmiServiceUrlBuilder newInstance() {
+ return new DmiServiceUrlBuilder();
+ }
/**
- * This method creates the dmi service url.
+ * Add a fixed pathSegment to the URI.
*
- * @param queryParams query param map as key,value pair
- * @param uriVariables uri param map as key (placeholder),value pair
- * @return {@code String} dmi service url as string
+ * @param pathSegment the path segment
+ * @return this builder
*/
- public String getDmiDatastoreUrl(final MultiValueMap<String, String> queryParams,
- final Map<String, Object> uriVariables) {
- final UriComponentsBuilder uriComponentsBuilder = getCmHandleUrl()
- .pathSegment("data")
- .pathSegment("ds")
- .pathSegment("{dataStore}")
- .queryParams(queryParams)
- .uriVariables(uriVariables);
- return uriComponentsBuilder.buildAndExpand().toUriString();
+ public DmiServiceUrlBuilder pathSegment(final String pathSegment) {
+ pathSegments.put(pathSegment, FIXED_PATH_SEGMENT);
+ return this;
}
/**
- * This method creates the dmi service url builder object with path variables.
+ * Add a variable pathSegment to the URI.
+ * Do NOT add { } braces. the builder will take care of that
*
- * @return {@code UriComponentsBuilder} dmi service url builder object
+ * @param pathSegment the name of the variable path segment (with { and }
+ * @param value the value to be insert in teh URI for the given variable path segment
+ * @return this builder
*/
- public UriComponentsBuilder getCmHandleUrl() {
- return UriComponentsBuilder.newInstance()
- .path("{dmiServiceName}")
- .pathSegment("{dmiBasePath}")
- .pathSegment("v1")
- .pathSegment("ch")
- .pathSegment("{cmHandleId}");
+ public DmiServiceUrlBuilder variablePathSegment(final String pathSegment, final Object value) {
+ pathSegments.put(pathSegment, value);
+ return this;
}
/**
- * This method populates uri variables.
+ * Add a query parameter to the URI.
+ * Do NOT encode as the builder wil take care of encoding
*
- * @param yangModelCmHandle get dmi service name
- * @param cmHandleId cm handle id for dmi registration
- * @return {@code String} dmi service url as string
+ * @param name the name of the variable
+ * @param value the value of the variable (only Strings are supported).
+ *
+ * @return this builder
*/
- public Map<String, Object> populateUriVariables(final YangModelCmHandle yangModelCmHandle,
- final String cmHandleId,
- final DmiOperations.DataStoreEnum dataStore) {
- cpsValidator.validateNameCharacters(cmHandleId);
- final Map<String, Object> uriVariables = new HashMap<>();
- final String dmiBasePath = dmiProperties.getDmiBasePath();
- uriVariables.put("dmiServiceName",
- yangModelCmHandle.resolveDmiServiceName(DATA));
- uriVariables.put("dmiBasePath", dmiBasePath);
- uriVariables.put("cmHandleId", cmHandleId);
- uriVariables.put("dataStore", dataStore.getValue());
- return uriVariables;
+ public DmiServiceUrlBuilder queryParameter(final String name, final String value) {
+ if (Strings.isNotBlank(value)) {
+ uriComponentsBuilder.queryParam(name, value);
+ }
+ return this;
}
/**
- * This method is used to populate map from query params.
+ * Build the URI as a correctly percentage-encoded String.
*
- * @param resourceId unique id of response for valid topic
- * @param optionsParamInQuery options into url param
- * @param topicParamInQuery topic into url param
- * @return all valid query params as map
+ * @param dmiServiceName the name of the dmi service
+ * @param dmiBasePath the base path of the dmi service
+ *
+ * @return URI as a string
*/
- public MultiValueMap<String, String> populateQueryParams(final String resourceId,
- final String optionsParamInQuery,
- final String topicParamInQuery) {
- final MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<>();
- getQueryParamConsumer().accept("resourceIdentifier",
- resourceId, queryParams);
- getQueryParamConsumer().accept("options", optionsParamInQuery, queryParams);
- if (Strings.isNotEmpty(topicParamInQuery)) {
- getQueryParamConsumer().accept("topic", topicParamInQuery, queryParams);
- }
- return queryParams;
- }
+ public String build(final String dmiServiceName, final String dmiBasePath) {
+ uriComponentsBuilder
+ .path("{dmiServiceName}")
+ .pathSegment("{dmiBasePath}")
+ .pathSegment("v1");
- private TriConsumer<String, String, MultiValueMap<String, String>> getQueryParamConsumer() {
- return (paramName, paramValue, paramMap) -> {
- if (Strings.isNotEmpty(paramValue)) {
- paramMap.add(paramName, paramValue);
+ final Map<String, Object> uriVariables = new HashMap<>();
+ uriVariables.put("dmiServiceName", dmiServiceName);
+ uriVariables.put("dmiBasePath", dmiBasePath);
+
+ pathSegments.forEach((pathSegment, variablePathValue) -> {
+ if (variablePathValue == FIXED_PATH_SEGMENT) {
+ uriComponentsBuilder.pathSegment(pathSegment);
+ } else {
+ uriComponentsBuilder.pathSegment("{" + pathSegment + "}");
+ uriVariables.put(pathSegment, variablePathValue);
}
- };
+ });
+ return uriComponentsBuilder.buildAndExpand(uriVariables).encode().toUriString();
}
+
}