b60aac9518fa3a8ca962b48aa49f1ff0ec5ecf6b
[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 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 static org.onap.cps.ncmp.api.impl.operations.RequiredDmiService.DATA;
24
25 import java.util.HashMap;
26 import java.util.Map;
27 import lombok.RequiredArgsConstructor;
28 import org.apache.logging.log4j.util.Strings;
29 import org.apache.logging.log4j.util.TriConsumer;
30 import org.onap.cps.ncmp.api.impl.config.NcmpConfiguration;
31 import org.onap.cps.ncmp.api.impl.operations.DmiOperations;
32 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
33 import org.springframework.stereotype.Component;
34 import org.springframework.util.LinkedMultiValueMap;
35 import org.springframework.util.MultiValueMap;
36 import org.springframework.web.util.UriComponentsBuilder;
37
38 @Component
39 @RequiredArgsConstructor
40 public class DmiServiceUrlBuilder {
41
42     private final NcmpConfiguration.DmiProperties dmiProperties;
43
44     /**
45      * This method creates the dmi service url.
46      *
47      * @param queryParams  query param map as key,value pair
48      * @param uriVariables uri param map as key (placeholder),value pair
49      * @return {@code String} dmi service url as string
50      */
51     public String getDmiDatastoreUrl(final MultiValueMap<String, String> queryParams,
52                                      final Map<String, Object> uriVariables) {
53         final UriComponentsBuilder uriComponentsBuilder = getCmHandleUrl()
54                 .pathSegment("data")
55                 .pathSegment("ds")
56                 .pathSegment("{dataStore}")
57                 .queryParams(queryParams)
58                 .uriVariables(uriVariables);
59         return uriComponentsBuilder.buildAndExpand().toUriString();
60     }
61
62     /**
63      * This method creates the dmi service url builder object with path variables.
64      *
65      * @return {@code UriComponentsBuilder} dmi service url builder object
66      */
67     public UriComponentsBuilder getCmHandleUrl() {
68         return UriComponentsBuilder.newInstance()
69                 .path("{dmiServiceName}")
70                 .pathSegment("{dmiBasePath}")
71                 .pathSegment("v1")
72                 .pathSegment("ch")
73                 .pathSegment("{cmHandle}");
74     }
75
76     /**
77      * This method populates uri variables.
78      *
79      * @param yangModelCmHandle get dmi service name
80      * @param cmHandle          cm handle name for dmi registration
81      * @return {@code String} dmi service url as string
82      */
83     public Map<String, Object> populateUriVariables(final YangModelCmHandle yangModelCmHandle,
84                                                     final String cmHandle,
85                                                     final DmiOperations.DataStoreEnum dataStore) {
86         final Map<String, Object> uriVariables = new HashMap<>();
87         final String dmiBasePath = dmiProperties.getDmiBasePath();
88         uriVariables.put("dmiServiceName",
89                 yangModelCmHandle.resolveDmiServiceName(DATA));
90         uriVariables.put("dmiBasePath", dmiBasePath);
91         uriVariables.put("cmHandle", cmHandle);
92         uriVariables.put("dataStore", dataStore.getValue());
93         return uriVariables;
94     }
95
96     /**
97      * This method is used to populate map from query params.
98      *
99      * @param resourceId          unique id of response for valid topic
100      * @param optionsParamInQuery options into url param
101      * @param topicParamInQuery   topic into url param
102      * @return all valid query params as map
103      */
104     public MultiValueMap<String, String> populateQueryParams(final String resourceId,
105                                                              final String optionsParamInQuery,
106                                                              final String topicParamInQuery) {
107         final MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<>();
108         getQueryParamConsumer().accept("resourceIdentifier",
109                 resourceId, queryParams);
110         getQueryParamConsumer().accept("options", optionsParamInQuery, queryParams);
111         if (Strings.isNotEmpty(topicParamInQuery)) {
112             getQueryParamConsumer().accept("topic", topicParamInQuery, queryParams);
113         }
114         return queryParams;
115     }
116
117     private TriConsumer<String, String, MultiValueMap<String, String>> getQueryParamConsumer() {
118         return (paramName, paramValue, paramMap) -> {
119             if (Strings.isNotEmpty(paramValue)) {
120                 paramMap.add(paramName, paramValue);
121             }
122         };
123     }
124 }