2  *  ============LICENSE_START=======================================================
 
   3  *  Copyright (C) 2022-2024 Nordix Foundation
 
   4  *  ================================================================================
 
   5  *  Licensed under the Apache License, Version 2.0 (the "License");
 
   6  *  you may not use this file except in compliance with the License.
 
   7  *  You may obtain a copy of the License at
 
   9  *        http://www.apache.org/licenses/LICENSE-2.0
 
  11  *  Unless required by applicable law or agreed to in writing, software
 
  12  *  distributed under the License is distributed on an "AS IS" BASIS,
 
  13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  14  *  See the License for the specific language governing permissions and
 
  15  *  limitations under the License.
 
  17  *  SPDX-License-Identifier: Apache-2.0
 
  18  *  ============LICENSE_END=========================================================
 
  21 package org.onap.cps.ncmp.api.impl.utils.url.builder;
 
  23 import java.util.Collections;
 
  24 import java.util.HashMap;
 
  25 import java.util.LinkedHashMap;
 
  27 import lombok.NoArgsConstructor;
 
  28 import org.apache.commons.lang3.StringUtils;
 
  29 import org.apache.logging.log4j.util.Strings;
 
  30 import org.springframework.web.util.UriComponentsBuilder;
 
  33 public class DmiServiceUrlTemplateBuilder {
 
  35     private final UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.newInstance();
 
  36     private static final String FIXED_PATH_SEGMENT = null;
 
  37     private static final String VERSION_SEGMENT = "v1";
 
  38     private final Map<String, String> pathSegments = new LinkedHashMap<>();
 
  39     private final Map<String, String> queryParameters = new LinkedHashMap<>();
 
  42      * Static factory method to create a new instance of DmiServiceUrlTemplateBuilder.
 
  44      * @return a new instance of DmiServiceUrlTemplateBuilder
 
  46     public static DmiServiceUrlTemplateBuilder newInstance() {
 
  47         return new DmiServiceUrlTemplateBuilder();
 
  51      * Add a fixed pathSegment to the URL.
 
  53      * @param pathSegment the path segment
 
  54      * @return this builder instance
 
  56     public DmiServiceUrlTemplateBuilder fixedPathSegment(final String pathSegment) {
 
  57         pathSegments.put(pathSegment, FIXED_PATH_SEGMENT);
 
  62      * Add a variable pathSegment to the URL.
 
  63      * Do NOT add { } braces. the builder will take care of that
 
  65      * @param pathSegment the name of the variable path segment (with { and }
 
  66      * @param value       the value to be insert in teh URL for the given variable path segment
 
  67      * @return this builder instance
 
  69     public DmiServiceUrlTemplateBuilder variablePathSegment(final String pathSegment, final String value) {
 
  70         pathSegments.put(pathSegment, value);
 
  75      * Add a query parameter to the URL.
 
  76      * Do NOT encode as the builder wil take care of encoding
 
  78      * @param queryParameterName  the name of the variable
 
  79      * @param queryParameterValue the value of the variable (only Strings are supported).
 
  81      * @return this builder instance
 
  83     public DmiServiceUrlTemplateBuilder queryParameter(final String queryParameterName,
 
  84                                                        final String queryParameterValue) {
 
  85         if (Strings.isNotBlank(queryParameterValue)) {
 
  86             queryParameters.put(queryParameterName, queryParameterValue);
 
  92      * Constructs a URL template with variables based on the accumulated path segments and query parameters.
 
  94      * @param dmiServiceBaseUrl the base URL of the DMI service, e.g., "http://dmi-service.com".
 
  95      * @param dmiBasePath       the base path of the DMI service
 
  96      * @return a UrlTemplateParameters instance containing the complete URL template and URL variables
 
  98     public UrlTemplateParameters createUrlTemplateParameters(final String dmiServiceBaseUrl, final String dmiBasePath) {
 
  99         this.uriComponentsBuilder.pathSegment(dmiBasePath)
 
 100             .pathSegment(VERSION_SEGMENT);
 
 102         final Map<String, String> urlTemplateVariables = new HashMap<>();
 
 104         pathSegments.forEach((pathSegmentName, variablePathValue) ->  {
 
 105             if (StringUtils.equals(variablePathValue, FIXED_PATH_SEGMENT)) {
 
 106                 this.uriComponentsBuilder.pathSegment(pathSegmentName);
 
 108                 this.uriComponentsBuilder.pathSegment("{" + pathSegmentName + "}");
 
 109                 urlTemplateVariables.put(pathSegmentName, variablePathValue);
 
 113         queryParameters.forEach((paramName, paramValue) -> {
 
 114             this.uriComponentsBuilder.queryParam(paramName, "{" + paramName + "}");
 
 115             urlTemplateVariables.put(paramName, paramValue);
 
 118         final String urlTemplate = dmiServiceBaseUrl + this.uriComponentsBuilder.build().toUriString();
 
 119         return new UrlTemplateParameters(urlTemplate, urlTemplateVariables);
 
 123      * Constructs a URL for DMI health check based on the given base URL.
 
 125      * @param dmiServiceBaseUrl the base URL of the DMI service, e.g., "http://dmi-service.com".
 
 126      * @return a {@link UrlTemplateParameters} instance containing the complete URL template and empty URL variables,
 
 127      *     suitable for DMI health check.
 
 129     public UrlTemplateParameters createUrlTemplateParametersForHealthCheck(final String dmiServiceBaseUrl) {
 
 130         this.uriComponentsBuilder.pathSegment("actuator")
 
 131                 .pathSegment("health");
 
 133         final String urlTemplate = dmiServiceBaseUrl + this.uriComponentsBuilder.build().toUriString();
 
 134         return new UrlTemplateParameters(urlTemplate, Collections.emptyMap());