2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020 Ericsson. All rights reserved.
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=========================================================
20 package org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.nsd;
22 import static org.apache.commons.lang3.StringUtils.isNotBlank;
23 import java.util.List;
24 import java.util.Optional;
25 import org.apache.commons.io.IOUtils;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28 import org.springframework.stereotype.Service;
31 * @author Waqas Ikram (waqas.ikram@est.tech)
35 public class ToscaMetadataParser {
36 private static final String ATTRIBUTE_VALUE_SEPARATOR = ":";
37 private static final Logger logger = LoggerFactory.getLogger(ToscaMetadataParser.class);
39 public Optional<ToscaMetadata> parse(final FileEntry toscaMetaFile) {
41 final ToscaMetadata toscaMetadata = new ToscaMetadata();
42 final List<String> lines = IOUtils.readLines(toscaMetaFile.getFileContentAsStream(), "utf-8");
43 for (final String line : lines) {
44 final String trimmedLine = line.trim();
45 if (!trimmedLine.isEmpty() && trimmedLine.contains(ATTRIBUTE_VALUE_SEPARATOR)) {
46 final String[] entry = trimmedLine.split(ATTRIBUTE_VALUE_SEPARATOR);
47 if (entry.length >= 2 && isNotBlank(entry[0]) && isNotBlank(entry[1])) {
48 toscaMetadata.addEntry(entry[0].trim(), entry[1].trim());
50 logger.warn("Unexpected line in metadata file: {}", line);
53 logger.warn("Unexpected line does not contain valid separator {} in metadata file: {}",
54 ATTRIBUTE_VALUE_SEPARATOR, line);
58 return Optional.of(toscaMetadata);
60 } catch (final Exception exception) {
61 logger.error("Unable to parser metadata file content", exception);
63 return Optional.empty();