b4d7f84ca421d5404630133652c6ff63db42b1b0
[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 org.openecomp.sdc.common.errors.Messages;
26 import org.openecomp.sdc.datatypes.error.ErrorLevel;
27 import org.openecomp.sdc.datatypes.error.ErrorMessage;
28 import java.io.InputStream;
29 import java.io.IOException;
30 import org.apache.commons.io.IOUtils;
31
32 import java.util.ArrayList;
33 import java.util.Collections;
34 import java.util.HashMap;
35 import java.util.List;
36 import java.util.Map;
37
38 import static org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder.getErrorWithParameters;
39 import static org.openecomp.sdc.tosca.csar.CSARConstants.SEPERATOR_MF_ATTRIBUTE;
40 import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_ENTRY_DEFINITIONS;
41
42 public class OnboardingToscaMetadata implements ToscaMetadata{
43
44   private Map<String, String> metaEntries;
45   private List<ErrorMessage> errors;
46
47   private OnboardingToscaMetadata(){
48     metaEntries = new HashMap<>();
49     errors = new ArrayList<>();
50   }
51
52   /**
53    * Method parses input stream of meta file, only block_0 is parsed, the rest of metadata ignored
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     OnboardingToscaMetadata meta = new OnboardingToscaMetadata();
60     List<String> metadataLines = IOUtils.readLines(st, "utf-8");
61     for (String line : metadataLines) {
62       line = line.trim();
63       if (line.isEmpty()) {
64         return meta;
65       }
66       String[] entry = line.split(SEPERATOR_MF_ATTRIBUTE);
67       //No empty keys allowed, no empty values allowed
68       if (entry.length < 2 || entry[0].isEmpty()) {
69         meta.errors.add(new ErrorMessage(ErrorLevel.ERROR, getErrorWithParameters(
70                 Messages.METADATA_INVALID_ENTRY_DEFINITIONS.getErrorMessage(), line)));
71         //want to get all error lines in meta file block_0, no breaking loop
72       } else {
73         meta.metaEntries.put(entry[0].trim(), entry[1].trim());
74       }
75     }
76
77     if (!meta.metaEntries.containsKey(TOSCA_META_ENTRY_DEFINITIONS)) {
78       meta.errors.add(new ErrorMessage(ErrorLevel.ERROR, getErrorWithParameters(
79               Messages.METADATA_NO_ENTRY_DEFINITIONS.getErrorMessage())));
80     }
81     return meta;
82   }
83
84   @Override  public boolean isValid(){
85     return errors.isEmpty();
86   }
87
88   @Override
89   public List<ErrorMessage> getErrors() {
90     return  ImmutableList.copyOf(errors);
91   }
92
93
94   @Override
95   public Map<String, String> getMetaEntries() {
96     if (!isValid()){
97       return Collections.emptyMap();
98     }
99     return ImmutableMap.copyOf(metaEntries);
100   }
101 }
102