746416c8e4e672bb3abc695f9928d11d28090b9f
[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 / parser / 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.parser;
21
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;
32
33 /**
34  * @author Waqas Ikram (waqas.ikram@est.tech)
35  *
36  */
37 @Service
38 public class ToscaMetadataParser {
39     private static final String ATTRIBUTE_VALUE_SEPARATOR = ":";
40     private static final Logger logger = LoggerFactory.getLogger(ToscaMetadataParser.class);
41
42     public Optional<ToscaMetadata> parse(final FileEntry toscaMetaFile) {
43         try {
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());
52                     } else {
53                         logger.warn("Unexpected line in metadata file: {}", line);
54                     }
55                 } else {
56                     logger.warn("Unexpected line does not contain valid separator {} in metadata file: {}",
57                             ATTRIBUTE_VALUE_SEPARATOR, line);
58                 }
59
60             }
61             return Optional.of(toscaMetadata);
62
63         } catch (final Exception exception) {
64             logger.error("Unable to parser metadata file content", exception);
65         }
66         return Optional.empty();
67     }
68
69
70 }