Merge "Add retry mechanism on Subscription loader"
[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.Map;
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;
34
35 @Component
36 @RequiredArgsConstructor
37 public class DmiServiceUrlBuilder {
38
39     private final NcmpConfiguration.DmiProperties dmiProperties;
40     private final CpsValidator cpsValidator;
41
42     /**
43      * This method creates the dmi service url.
44      *
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
48      */
49     public String getDmiDatastoreUrl(final MultiValueMap<String, String> queryParams,
50                                      final Map<String, Object> uriVariables) {
51         return getUriComponentsBuilder(getResourceDataBasePathUriBuilder(), queryParams, uriVariables)
52                 .buildAndExpand().toUriString();
53     }
54
55     /**
56      * This method creates the dmi service url for bulk request.
57      *
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
61      */
62     public String getBulkRequestUrl(final MultiValueMap<String, String> queryParams,
63                                     final Map<String, Object> uriVariables) {
64         return getUriComponentsBuilder(getBulkResourceDataBasePathUriBuilder(), queryParams, uriVariables)
65                 .buildAndExpand().toUriString();
66     }
67
68     /**
69      * This method creates the dmi service url builder object with path variables.
70      *
71      * @return {@code UriComponentsBuilder} dmi service url builder object
72      */
73     public UriComponentsBuilder getResourceDataBasePathUriBuilder() {
74         return UriComponentsBuilder.newInstance()
75                 .path("{dmiServiceName}")
76                 .pathSegment("{dmiBasePath}")
77                 .pathSegment("v1")
78                 .pathSegment("ch")
79                 .pathSegment("{cmHandleId}");
80     }
81
82     /**
83      * This method creates the dmi service url builder object with path variables for batch of cm handles.
84      *
85      * @return {@code UriComponentsBuilder} dmi service url builder object
86      */
87     public UriComponentsBuilder getBulkResourceDataBasePathUriBuilder() {
88         return UriComponentsBuilder.newInstance()
89                 .path("{dmiServiceName}")
90                 .pathSegment("{dmiBasePath}")
91                 .pathSegment("v1")
92                 .pathSegment("batch");
93     }
94
95     /**
96      * This method populates uri variables.
97      *
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
102      */
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);
113         return uriVariables;
114     }
115
116     /**
117      * This method is used to populate map from query params.
118      *
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
123      */
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);
133         }
134         return queryParams;
135     }
136
137     private TriConsumer<String, String, MultiValueMap<String, String>> getQueryParamConsumer() {
138         return (paramName, paramValue, paramMap) -> {
139             if (Strings.isNotEmpty(paramValue)) {
140                 paramMap.add(paramName, paramValue);
141             }
142         };
143     }
144
145     private UriComponentsBuilder getUriComponentsBuilder(final UriComponentsBuilder uriComponentsBuilder,
146                                                          final MultiValueMap<String, String> queryParams,
147                                                          final Map<String, Object> uriVariables) {
148         return uriComponentsBuilder
149                 .pathSegment("data")
150                 .pathSegment("ds")
151                 .pathSegment("{dataStore}")
152                 .queryParams(queryParams)
153                 .uriVariables(uriVariables);
154     }
155 }