bc104335f57c63d384e6d30fea5b6f01117a0976
[sdc.git] / openecomp-be / lib / openecomp-tosca-lib / src / main / java / org / openecomp / sdc / tosca / csar / OnboardingToscaMetadata.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.openecomp.sdc.tosca.csar;
21
22 import static org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder.getErrorWithParameters;
23 import static org.openecomp.sdc.tosca.csar.ManifestTokenType.ATTRIBUTE_VALUE_SEPARATOR;
24 import static org.openecomp.sdc.tosca.csar.ToscaMetaEntry.ENTRY_DEFINITIONS;
25
26 import com.google.common.collect.ImmutableList;
27 import com.google.common.collect.ImmutableMap;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.util.ArrayList;
31 import java.util.Collections;
32 import java.util.HashMap;
33 import java.util.List;
34 import java.util.Map;
35 import java.util.Optional;
36 import org.apache.commons.io.IOUtils;
37 import org.openecomp.sdc.common.errors.Messages;
38 import org.openecomp.sdc.datatypes.error.ErrorLevel;
39 import org.openecomp.sdc.datatypes.error.ErrorMessage;
40
41 public class OnboardingToscaMetadata implements ToscaMetadata {
42
43     private Map<String, String> metaEntries;
44     private List<ErrorMessage> errors;
45
46     private OnboardingToscaMetadata() {
47         metaEntries = new HashMap<>();
48         errors = new ArrayList<>();
49     }
50
51     /**
52      * Method parses input stream of meta file, only block_0 is parsed, the rest of metadata ignored
53      *
54      * @param st meta file input stream
55      * @return OnboardingToscaMetadata instance
56      * @throws IOException
57      */
58     public static ToscaMetadata parseToscaMetadataFile(InputStream st) throws IOException {
59         if (st == null) {
60             throw new IOException(Messages.METADATA_PARSER_INTERNAL.getErrorMessage());
61         }
62         OnboardingToscaMetadata meta = new OnboardingToscaMetadata();
63         List<String> metadataLines = IOUtils.readLines(st, "utf-8");
64         for (String line : metadataLines) {
65             line = line.trim();
66             if (line.isEmpty()) {
67                 return meta;
68             }
69             String[] entry = line.split(ATTRIBUTE_VALUE_SEPARATOR.getToken());
70             //No empty keys allowed, no empty values allowed
71             if (entry.length < 2 || entry[0].isEmpty()) {
72                 meta.errors.add(
73                     new ErrorMessage(ErrorLevel.ERROR, getErrorWithParameters(Messages.METADATA_INVALID_ENTRY_DEFINITIONS.getErrorMessage(), line)));
74                 //want to get all error lines in meta file block_0, no breaking loop
75             } else {
76                 meta.metaEntries.put(entry[0].trim(), entry[1].trim());
77             }
78         }
79         if (!meta.metaEntries.containsKey(ENTRY_DEFINITIONS.getName())) {
80             meta.errors.add(new ErrorMessage(ErrorLevel.ERROR, getErrorWithParameters(Messages.METADATA_NO_ENTRY_DEFINITIONS.getErrorMessage())));
81         }
82         return meta;
83     }
84
85     @Override
86     public boolean isValid() {
87         return errors.isEmpty();
88     }
89
90     @Override
91     public List<ErrorMessage> getErrors() {
92         return ImmutableList.copyOf(errors);
93     }
94
95     @Override
96     public Map<String, String> getMetaEntries() {
97         if (!isValid()) {
98             return Collections.emptyMap();
99         }
100         return ImmutableMap.copyOf(metaEntries);
101     }
102
103     @Override
104     public boolean hasEntry(final String entry) {
105         return metaEntries.containsKey(entry);
106     }
107
108     @Override
109     public Optional<String> getEntry(final ToscaMetaEntry entry) {
110         return Optional.ofNullable(metaEntries.get(entry.getName()));
111     }
112 }