b89b7b32218c65cdd7f0b4e40c05b1356e55db64
[cps.git] /
1 /*
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
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.ncmp.api.impl.utils.url.builder;
22
23 import java.util.Collections;
24 import java.util.HashMap;
25 import java.util.LinkedHashMap;
26 import java.util.Map;
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;
31
32 @NoArgsConstructor
33 public class DmiServiceUrlTemplateBuilder {
34
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<>();
40
41     /**
42      * Static factory method to create a new instance of DmiServiceUrlTemplateBuilder.
43      *
44      * @return a new instance of DmiServiceUrlTemplateBuilder
45      */
46     public static DmiServiceUrlTemplateBuilder newInstance() {
47         return new DmiServiceUrlTemplateBuilder();
48     }
49
50     /**
51      * Add a fixed pathSegment to the URL.
52      *
53      * @param pathSegment the path segment
54      * @return this builder instance
55      */
56     public DmiServiceUrlTemplateBuilder fixedPathSegment(final String pathSegment) {
57         pathSegments.put(pathSegment, FIXED_PATH_SEGMENT);
58         return this;
59     }
60
61     /**
62      * Add a variable pathSegment to the URL.
63      * Do NOT add { } braces. the builder will take care of that
64      *
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
68      */
69     public DmiServiceUrlTemplateBuilder variablePathSegment(final String pathSegment, final String value) {
70         pathSegments.put(pathSegment, value);
71         return this;
72     }
73
74     /**
75      * Add a query parameter to the URL.
76      * Do NOT encode as the builder wil take care of encoding
77      *
78      * @param queryParameterName  the name of the variable
79      * @param queryParameterValue the value of the variable (only Strings are supported).
80      *
81      * @return this builder instance
82      */
83     public DmiServiceUrlTemplateBuilder queryParameter(final String queryParameterName,
84                                                        final String queryParameterValue) {
85         if (Strings.isNotBlank(queryParameterValue)) {
86             queryParameters.put(queryParameterName, queryParameterValue);
87         }
88         return this;
89     }
90
91     /**
92      * Constructs a URL template with variables based on the accumulated path segments and query parameters.
93      *
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
97      */
98     public UrlTemplateParameters createUrlTemplateParameters(final String dmiServiceBaseUrl, final String dmiBasePath) {
99         this.uriComponentsBuilder.pathSegment(dmiBasePath)
100             .pathSegment(VERSION_SEGMENT);
101
102         final Map<String, String> urlTemplateVariables = new HashMap<>();
103
104         pathSegments.forEach((pathSegmentName, variablePathValue) ->  {
105             if (StringUtils.equals(variablePathValue, FIXED_PATH_SEGMENT)) {
106                 this.uriComponentsBuilder.pathSegment(pathSegmentName);
107             } else {
108                 this.uriComponentsBuilder.pathSegment("{" + pathSegmentName + "}");
109                 urlTemplateVariables.put(pathSegmentName, variablePathValue);
110             }
111         });
112
113         queryParameters.forEach((paramName, paramValue) -> {
114             this.uriComponentsBuilder.queryParam(paramName, "{" + paramName + "}");
115             urlTemplateVariables.put(paramName, paramValue);
116         });
117
118         final String urlTemplate = dmiServiceBaseUrl + this.uriComponentsBuilder.build().toUriString();
119         return new UrlTemplateParameters(urlTemplate, urlTemplateVariables);
120     }
121
122     /**
123      * Constructs a URL for DMI health check based on the given base URL.
124      *
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.
128      */
129     public UrlTemplateParameters createUrlTemplateParametersForHealthCheck(final String dmiServiceBaseUrl) {
130         this.uriComponentsBuilder.pathSegment("actuator")
131                 .pathSegment("health");
132
133         final String urlTemplate = dmiServiceBaseUrl + this.uriComponentsBuilder.build().toUriString();
134         return new UrlTemplateParameters(urlTemplate, Collections.emptyMap());
135     }
136
137 }