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