20bcf84aa26015fa7abc4299c7ca64d527b1510d
[sdc.git] /
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
21 package org.openecomp.sdc.tosca.csar;
22
23 import com.google.common.collect.ImmutableList;
24 import com.google.common.collect.ImmutableMap;
25 import java.util.Optional;
26 import org.openecomp.sdc.common.errors.Messages;
27 import org.openecomp.sdc.datatypes.error.ErrorLevel;
28 import org.openecomp.sdc.datatypes.error.ErrorMessage;
29 import java.io.InputStream;
30 import java.io.IOException;
31 import org.apache.commons.io.IOUtils;
32
33 import java.util.ArrayList;
34 import java.util.Collections;
35 import java.util.HashMap;
36 import java.util.List;
37 import java.util.Map;
38
39 import static org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder.getErrorWithParameters;
40 import static org.openecomp.sdc.tosca.csar.ManifestTokenType.ATTRIBUTE_VALUE_SEPARATOR;
41 import static org.openecomp.sdc.tosca.csar.ToscaMetaEntry.ENTRY_DEFINITIONS;
42
43 public class OnboardingToscaMetadata implements ToscaMetadata {
44
45   private Map<String, String> metaEntries;
46   private List<ErrorMessage> errors;
47
48   private OnboardingToscaMetadata(){
49     metaEntries = new HashMap<>();
50     errors = new ArrayList<>();
51   }
52
53   /**
54    * Method parses input stream of meta file, only block_0 is parsed, the rest of metadata ignored
55    * @param st meta file input stream
56    * @return OnboardingToscaMetadata instance
57    * @throws IOException
58    */
59   public static ToscaMetadata parseToscaMetadataFile(InputStream st) throws IOException {
60     if(st == null) {
61         throw new IOException(Messages.METADATA_PARSER_INTERNAL.getErrorMessage());
62     }
63     OnboardingToscaMetadata meta = new OnboardingToscaMetadata();
64     List<String> metadataLines = IOUtils.readLines(st, "utf-8");
65     for (String line : metadataLines) {
66       line = line.trim();
67       if (line.isEmpty()) {
68         return meta;
69       }
70       String[] entry = line.split(ATTRIBUTE_VALUE_SEPARATOR.getToken());
71       //No empty keys allowed, no empty values allowed
72       if (entry.length < 2 || entry[0].isEmpty()) {
73         meta.errors.add(new ErrorMessage(ErrorLevel.ERROR, getErrorWithParameters(
74                 Messages.METADATA_INVALID_ENTRY_DEFINITIONS.getErrorMessage(), line)));
75         //want to get all error lines in meta file block_0, no breaking loop
76       } else {
77         meta.metaEntries.put(entry[0].trim(), entry[1].trim());
78       }
79     }
80
81     if (!meta.metaEntries.containsKey(ENTRY_DEFINITIONS.getName())) {
82       meta.errors.add(new ErrorMessage(ErrorLevel.ERROR, getErrorWithParameters(
83               Messages.METADATA_NO_ENTRY_DEFINITIONS.getErrorMessage())));
84     }
85     return meta;
86   }
87
88   @Override  public boolean isValid(){
89     return errors.isEmpty();
90   }
91
92   @Override
93   public List<ErrorMessage> getErrors() {
94     return ImmutableList.copyOf(errors);
95   }
96
97
98   @Override
99   public Map<String, String> getMetaEntries() {
100     if (!isValid()){
101       return Collections.emptyMap();
102     }
103     return ImmutableMap.copyOf(metaEntries);
104   }
105
106   @Override
107   public boolean hasEntry(final String entry) {
108     return metaEntries.containsKey(entry);
109
110   }
111
112   @Override
113   public Optional<String> getEntry(final ToscaMetaEntry entry) {
114     return Optional.ofNullable(metaEntries.get(entry.getName()));
115   }
116 }
117