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