Updating jaxb-runtime version
[so.git] / so-etsi-nfvo / so-etsi-nfvo-ns-lcm / so-etsi-nfvo-ns-lcm-bpmn-flows / src / main / java / org / onap / so / etsi / nfvo / ns / lcm / bpmn / flows / nsd / ToscaMetadataParser.java
1 /*-
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
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 package org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.nsd;
21
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;
29
30 /**
31  * @author Waqas Ikram (waqas.ikram@est.tech)
32  *
33  */
34 @Service
35 public class ToscaMetadataParser {
36     private static final String ATTRIBUTE_VALUE_SEPARATOR = ":";
37     private static final Logger logger = LoggerFactory.getLogger(ToscaMetadataParser.class);
38
39     public Optional<ToscaMetadata> parse(final FileEntry toscaMetaFile) {
40         try {
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());
49                     } else {
50                         logger.warn("Unexpected line in metadata file: {}", line);
51                     }
52                 } else {
53                     logger.warn("Unexpected line does not contain valid separator {} in metadata file: {}",
54                             ATTRIBUTE_VALUE_SEPARATOR, line);
55                 }
56
57             }
58             return Optional.of(toscaMetadata);
59
60         } catch (final Exception exception) {
61             logger.error("Unable to parser metadata file content", exception);
62         }
63         return Optional.empty();
64     }
65
66
67 }