2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * Copyright (C) 2021 Nokia. All rights reserved.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.dcaegen2.collectors.datafile.model;
25 import java.nio.file.Path;
26 import java.nio.file.Paths;
27 import java.util.ArrayList;
28 import java.util.Collection;
29 import java.util.List;
30 import java.util.Optional;
32 import lombok.Builder;
34 import org.apache.hc.core5.http.NameValuePair;
35 import org.apache.hc.core5.net.URIBuilder;
36 import org.onap.dcaegen2.collectors.datafile.commons.FileServerData;
37 import org.onap.dcaegen2.collectors.datafile.commons.FileServerData.FileServerDataBuilder;
38 import org.onap.dcaegen2.collectors.datafile.commons.Scheme;
39 import org.onap.dcaegen2.collectors.datafile.configuration.AppConfig;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
44 * Contains data, from the fileReady event, about the file to collect from the
49 public class FileData {
51 private static final Logger logger = LoggerFactory.getLogger(FileData.class);
53 public FileReadyMessage.ArrayOfNamedHashMap fileInfo;
55 public FileReadyMessage.MessageMetaData messageMetaData;
57 public static Iterable<FileData> createFileData(FileReadyMessage msg) {
58 Collection<FileData> res = new ArrayList<>();
59 for (FileReadyMessage.ArrayOfNamedHashMap arr : msg.event.notificationFields.arrayOfNamedHashMap) {
60 FileData data = FileData.builder().fileInfo(arr).messageMetaData(msg.event.commonEventHeader).build();
67 * Get the name of the PNF, must be unique in the network.
69 * @return the name of the PNF, must be unique in the network
71 public String sourceName() {
72 return messageMetaData.sourceName;
75 public String name() {
76 return this.messageMetaData.sourceName + "/" + fileInfo.name;
80 * Get the path to file to get from the PNF.
82 * @return the path to the file on the PNF.
84 public String remoteFilePath() {
85 return URI.create(fileInfo.hashMap.location).getPath();
88 public Scheme scheme() {
89 URI uri = URI.create(fileInfo.hashMap.location);
91 return Scheme.getSchemeFromString(uri.getScheme());
92 } catch (Exception e) {
93 logger.warn("Could noit get scheme :{}", e.getMessage());
99 * Get the path to the locally stored file.
101 * @return the path to the locally stored file.
103 public Path getLocalFilePath(AppConfig config) {
104 return Paths.get(config.collectedFilesPath, this.messageMetaData.sourceName, fileInfo.name);
108 * Get the data about the file server where the file should be collected from.
109 * Query data included as it can contain JWT token
111 * @return the data about the file server where the file should be collected
114 public FileServerData fileServerData() {
115 URI uri = URI.create(fileInfo.hashMap.location);
116 Optional<String[]> userInfo = getUserNameAndPasswordIfGiven(uri.getUserInfo());
118 FileServerDataBuilder builder = FileServerData.builder() //
119 .serverAddress(uri.getHost()) //
120 .userId(userInfo.isPresent() ? userInfo.get()[0] : "") //
121 .password(userInfo.isPresent() ? userInfo.get()[1] : "");
122 if (uri.getPort() > 0) {
123 builder.port(uri.getPort());
125 URIBuilder uriBuilder = new URIBuilder(uri);
126 List<NameValuePair> query = uriBuilder.getQueryParams();
127 if (query != null && !query.isEmpty()) {
128 builder.queryParameters(query);
130 String fragment = uri.getRawFragment();
131 if (fragment != null && fragment.length() > 0) {
132 builder.uriRawFragment(fragment);
134 return builder.build();
138 * Extracts user name and password from the user info, if it they are given in
141 * @param userInfoString the user info string from the URI.
143 * @return An <code>Optional</code> containing a String array with the user name
144 * and password if given, or an empty
145 * <code>Optional</code> if not given.
147 private static Optional<String[]> getUserNameAndPasswordIfGiven(String userInfoString) {
148 if (userInfoString != null) {
149 String[] userAndPassword = userInfoString.split(":");
150 if (userAndPassword.length == 2) {
151 return Optional.of(userAndPassword);
152 } else if (userAndPassword.length == 1)// if just user
154 String[] tab = new String[2];
155 tab[0] = userAndPassword[0];
156 tab[1] = "";// add empty password
157 return Optional.of(tab);
160 return Optional.empty();