Merge from ECOMP's repository
[vid.git] / vid-app-common / src / main / java / org / onap / vid / asdc / beans / tosca / ToscaMeta.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 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.onap.vid.asdc.beans.tosca;
22
23 import org.onap.vid.asdc.AsdcCatalogException;
24
25 import java.io.BufferedReader;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.io.InputStreamReader;
29 import java.util.HashMap;
30 import java.util.Map;
31
32 /**
33  * The Class ToscaMeta.
34  */
35 public class ToscaMeta {
36
37         /** The metadata. */
38         private final Map<String, String> metadata;
39         
40         /**
41          * Instantiates a new tosca meta.
42          *
43          * @param builder the builder
44          * @throws IOException Signals that an I/O exception has occurred.
45          * @throws AsdcCatalogException the asdc catalog exception
46          */
47         private ToscaMeta(Builder builder) throws IOException, AsdcCatalogException {
48                 metadata = new HashMap<> ();
49                 
50                 read(builder.inputStream);
51         }
52         
53         /**
54          * The Class Builder.
55          */
56         public static class Builder {
57                 
58                 /** The input stream. */
59                 private final InputStream inputStream;
60                 
61                 /**
62                  * Instantiates a new builder.
63                  *
64                  * @param inputStream the input stream
65                  */
66                 public Builder(InputStream inputStream) {
67                         this.inputStream = inputStream;
68                 }
69                 
70                 /**
71                  * Builds the.
72                  *
73                  * @return the tosca meta
74                  * @throws IOException Signals that an I/O exception has occurred.
75                  * @throws AsdcCatalogException the asdc catalog exception
76                  */
77                 public ToscaMeta build() throws IOException, AsdcCatalogException {
78                         return new ToscaMeta(this);
79                 }
80         }
81         
82         /**
83          * Gets the.
84          *
85          * @param property the property
86          * @return the string
87          */
88         public String get(String property) {
89                 return metadata.get(property);
90         }
91         
92         /**
93          * Read.
94          *
95          * @param inputStream the input stream
96          * @throws IOException Signals that an I/O exception has occurred.
97          * @throws AsdcCatalogException the asdc catalog exception
98          */
99         private void read(InputStream inputStream) throws IOException, AsdcCatalogException {
100                 
101                 final BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
102                 
103                 String line;
104                 
105                 try {
106                         while ((line = br.readLine()) != null) {
107                                 if ( line.length() > 0 ) {
108                                         final String[] entry = line.split(":");
109                                         
110                                         if (entry.length != 2) throw new AsdcCatalogException("TOSCA.meta file cannot be parsed (more than 1 colon found on a single line");
111                                         if (!entry[1].startsWith(" ")) throw new AsdcCatalogException("TOSCA.meta file cannot be parsed (: not immediately followed by ' ')");
112                                         
113                                         metadata.put(entry[0], entry[1].substring(1));
114                                 }
115                         }
116                 } catch (IOException | AsdcCatalogException e) {
117                         metadata.clear();
118                         throw e;
119                 }
120         }
121 }