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
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;
23 import java.util.HashMap;
25 import lombok.RequiredArgsConstructor;
26 import org.apache.logging.log4j.util.Strings;
27 import org.apache.logging.log4j.util.TriConsumer;
28 import org.onap.cps.ncmp.api.impl.config.NcmpConfiguration;
29 import org.onap.cps.spi.utils.CpsValidator;
30 import org.springframework.stereotype.Component;
31 import org.springframework.util.LinkedMultiValueMap;
32 import org.springframework.util.MultiValueMap;
33 import org.springframework.web.util.UriComponentsBuilder;
36 @RequiredArgsConstructor
37 public class DmiServiceUrlBuilder {
39 private final NcmpConfiguration.DmiProperties dmiProperties;
40 private final CpsValidator cpsValidator;
43 * This method creates the dmi service url.
45 * @param queryParams query param map as key,value pair
46 * @param uriVariables uri param map as key (placeholder),value pair
47 * @return {@code String} dmi service url as string
49 public String getDmiDatastoreUrl(final MultiValueMap<String, String> queryParams,
50 final Map<String, Object> uriVariables) {
51 return getUriComponentsBuilder(getResourceDataBasePathUriBuilder(), queryParams, uriVariables)
52 .buildAndExpand().toUriString();
56 * This method creates the dmi service url for bulk request.
58 * @param queryParams query param map as key,value pair
59 * @param uriVariables uri param map as key (placeholder),value pair
60 * @return {@code String} dmi service url as string
62 public String getBulkRequestUrl(final MultiValueMap<String, String> queryParams,
63 final Map<String, Object> uriVariables) {
64 return getUriComponentsBuilder(getBulkResourceDataBasePathUriBuilder(), queryParams, uriVariables)
65 .buildAndExpand().toUriString();
69 * This method creates the dmi service url builder object with path variables.
71 * @return {@code UriComponentsBuilder} dmi service url builder object
73 public UriComponentsBuilder getResourceDataBasePathUriBuilder() {
74 return UriComponentsBuilder.newInstance()
75 .path("{dmiServiceName}")
76 .pathSegment("{dmiBasePath}")
79 .pathSegment("{cmHandleId}");
83 * This method creates the dmi service url builder object with path variables for batch of cm handles.
85 * @return {@code UriComponentsBuilder} dmi service url builder object
87 public UriComponentsBuilder getBulkResourceDataBasePathUriBuilder() {
88 return UriComponentsBuilder.newInstance()
89 .path("{dmiServiceName}")
90 .pathSegment("{dmiBasePath}")
92 .pathSegment("batch");
96 * This method populates uri variables.
98 * @param dataStoreName data store name
99 * @param dmiServiceName dmi service name
100 * @param cmHandleId cm handle id for dmi registration
101 * @return {@code String} dmi service url as string
103 public Map<String, Object> populateUriVariables(final String dataStoreName,
104 final String dmiServiceName,
105 final String cmHandleId) {
106 cpsValidator.validateNameCharacters(cmHandleId);
107 final Map<String, Object> uriVariables = new HashMap<>();
108 final String dmiBasePath = dmiProperties.getDmiBasePath();
109 uriVariables.put("dmiServiceName", dmiServiceName);
110 uriVariables.put("dmiBasePath", dmiBasePath);
111 uriVariables.put("cmHandleId", cmHandleId);
112 uriVariables.put("dataStore", dataStoreName);
117 * This method is used to populate map from query params.
119 * @param resourceId unique id of response for valid topic
120 * @param optionsParamInQuery options into url param
121 * @param topicParamInQuery topic into url param
122 * @return all valid query params as map
124 public MultiValueMap<String, String> populateQueryParams(final String resourceId,
125 final String optionsParamInQuery,
126 final String topicParamInQuery) {
127 final MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<>();
128 getQueryParamConsumer().accept("resourceIdentifier",
129 resourceId, queryParams);
130 getQueryParamConsumer().accept("options", optionsParamInQuery, queryParams);
131 if (Strings.isNotEmpty(topicParamInQuery)) {
132 getQueryParamConsumer().accept("topic", topicParamInQuery, queryParams);
137 private TriConsumer<String, String, MultiValueMap<String, String>> getQueryParamConsumer() {
138 return (paramName, paramValue, paramMap) -> {
139 if (Strings.isNotEmpty(paramValue)) {
140 paramMap.add(paramName, paramValue);
145 private UriComponentsBuilder getUriComponentsBuilder(final UriComponentsBuilder uriComponentsBuilder,
146 final MultiValueMap<String, String> queryParams,
147 final Map<String, Object> uriVariables) {
148 return uriComponentsBuilder
151 .pathSegment("{dataStore}")
152 .queryParams(queryParams)
153 .uriVariables(uriVariables);