[VID-12] Delivery of remaining features for v1.1
[vid.git] / vid-app-common / src / main / java / org / openecomp / vid / asdc / beans / tosca / ToscaMeta.java
1 /*-\r
2  * ============LICENSE_START=======================================================\r
3  * VID\r
4  * ================================================================================\r
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.\r
6  * ================================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  * \r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * \r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  * ============LICENSE_END=========================================================\r
19  */\r
20 \r
21 package org.openecomp.vid.asdc.beans.tosca;\r
22 \r
23 import java.io.BufferedReader;\r
24 import java.io.IOException;\r
25 import java.io.InputStream;\r
26 import java.io.InputStreamReader;\r
27 import java.util.HashMap;\r
28 import java.util.Map;\r
29 \r
30 import org.openecomp.vid.asdc.AsdcCatalogException;\r
31 \r
32 /**\r
33  * The Class ToscaMeta.\r
34  */\r
35 public class ToscaMeta {\r
36 \r
37         /** The metadata. */\r
38         private final Map<String, String> metadata;\r
39         \r
40         /**\r
41          * Instantiates a new tosca meta.\r
42          *\r
43          * @param builder the builder\r
44          * @throws IOException Signals that an I/O exception has occurred.\r
45          * @throws AsdcCatalogException the asdc catalog exception\r
46          */\r
47         private ToscaMeta(Builder builder) throws IOException, AsdcCatalogException {\r
48                 metadata = new HashMap<String, String> ();\r
49                 \r
50                 read(builder.inputStream);\r
51         }\r
52         \r
53         /**\r
54          * The Class Builder.\r
55          */\r
56         public static class Builder {\r
57                 \r
58                 /** The input stream. */\r
59                 private final InputStream inputStream;\r
60                 \r
61                 /**\r
62                  * Instantiates a new builder.\r
63                  *\r
64                  * @param inputStream the input stream\r
65                  */\r
66                 public Builder(InputStream inputStream) {\r
67                         this.inputStream = inputStream;\r
68                 }\r
69                 \r
70                 /**\r
71                  * Builds the.\r
72                  *\r
73                  * @return the tosca meta\r
74                  * @throws IOException Signals that an I/O exception has occurred.\r
75                  * @throws AsdcCatalogException the asdc catalog exception\r
76                  */\r
77                 public ToscaMeta build() throws IOException, AsdcCatalogException {\r
78                         return new ToscaMeta(this);\r
79                 }\r
80         }\r
81         \r
82         /**\r
83          * Gets the.\r
84          *\r
85          * @param property the property\r
86          * @return the string\r
87          */\r
88         public String get(String property) {\r
89                 return metadata.get(property);\r
90         }\r
91         \r
92         /**\r
93          * Read.\r
94          *\r
95          * @param inputStream the input stream\r
96          * @throws IOException Signals that an I/O exception has occurred.\r
97          * @throws AsdcCatalogException the asdc catalog exception\r
98          */\r
99         private void read(InputStream inputStream) throws IOException, AsdcCatalogException {\r
100                 \r
101                 final BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));\r
102                 \r
103                 String line;\r
104                 \r
105                 try {\r
106                         while ((line = br.readLine()) != null) {\r
107                                 if ( line.length() > 0 ) {\r
108                                         final String[] entry = line.split(":");\r
109                                         \r
110                                         if (entry.length != 2) throw new AsdcCatalogException("TOSCA.meta file cannot be parsed (more than 1 colon found on a single line");\r
111                                         if (!entry[1].startsWith(" ")) throw new AsdcCatalogException("TOSCA.meta file cannot be parsed (: not immediately followed by ' ')");\r
112                                         \r
113                                         metadata.put(entry[0], entry[1].substring(1));\r
114                                 }\r
115                         }\r
116                 } catch (IOException e) {\r
117                         metadata.clear();\r
118                         throw e;\r
119                 } catch (AsdcCatalogException e) {\r
120                         metadata.clear();\r
121                         throw e;\r
122                 }\r
123         }\r
124 }\r