Merge "Change ApplicationEvent for ModelLoading"
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / impl / utils / DmiServiceUrlBuilder.java
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2022-2023 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;
22
23 import java.util.HashMap;
24 import java.util.LinkedHashMap;
25 import java.util.Map;
26 import lombok.NoArgsConstructor;
27 import org.apache.logging.log4j.util.Strings;
28 import org.springframework.web.util.UriComponentsBuilder;
29
30 @NoArgsConstructor
31 public class DmiServiceUrlBuilder {
32
33     private static final String FIXED_PATH_SEGMENT = null;
34
35     final UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.newInstance();
36     final Map<String, Object> pathSegments = new LinkedHashMap<>();
37
38     public static DmiServiceUrlBuilder newInstance() {
39         return new DmiServiceUrlBuilder();
40     }
41
42     /**
43      * Add a fixed pathSegment to the URI.
44      *
45      * @param pathSegment the path segment
46      * @return this builder
47      */
48     public DmiServiceUrlBuilder pathSegment(final String pathSegment) {
49         pathSegments.put(pathSegment, FIXED_PATH_SEGMENT);
50         return this;
51     }
52
53     /**
54      * Add a variable pathSegment to the URI.
55      * Do NOT add { } braces. the builder will take care of that
56      *
57      * @param pathSegment the name of the variable path segment (with { and }
58      * @param value       the value to be insert in teh URI for the given variable path segment
59      * @return this builder
60      */
61     public DmiServiceUrlBuilder variablePathSegment(final String pathSegment, final Object value) {
62         pathSegments.put(pathSegment, value);
63         return this;
64     }
65
66     /**
67      * Add a query parameter to the URI.
68      * Do NOT encode as the builder wil take care of encoding
69      *
70      * @param name  the name of the variable
71      * @param value the value of the variable (only Strings are supported).
72      *
73      * @return this builder
74      */
75     public DmiServiceUrlBuilder queryParameter(final String name, final String value) {
76         if (Strings.isNotBlank(value)) {
77             uriComponentsBuilder.queryParam(name, value);
78         }
79         return this;
80     }
81
82     /**
83      * Build the URI as a correctly percentage-encoded String.
84      *
85      * @param dmiServiceName the name of the dmi service
86      * @param dmiBasePath    the base path of the dmi service
87      *
88      * @return URI as a string
89      */
90     public String build(final String dmiServiceName, final String dmiBasePath) {
91         uriComponentsBuilder
92             .path("{dmiServiceName}")
93             .pathSegment("{dmiBasePath}")
94             .pathSegment("v1");
95
96         final Map<String, Object> uriVariables = new HashMap<>();
97         uriVariables.put("dmiServiceName", dmiServiceName);
98         uriVariables.put("dmiBasePath", dmiBasePath);
99
100         pathSegments.forEach((pathSegment, variablePathValue) ->  {
101             if (variablePathValue == FIXED_PATH_SEGMENT) {
102                 uriComponentsBuilder.pathSegment(pathSegment);
103             } else {
104                 uriComponentsBuilder.pathSegment("{" + pathSegment + "}");
105                 uriVariables.put(pathSegment, variablePathValue);
106             }
107         });
108         return uriComponentsBuilder.buildAndExpand(uriVariables).encode().toUriString();
109     }
110
111 }