77c28c8fff7b530d760418c993eca0a0a79f2a28
[sdnc/northbound.git] / ueb-listener / src / main / java / org / openecomp / sdnc / uebclient / SdncArtifactMap.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                                      reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.openecomp.sdnc.uebclient;
23
24 import java.io.BufferedReader;
25 import java.io.File;
26 import java.io.FileReader;
27 import java.io.IOException;
28 import java.util.HashMap;
29 import java.util.Map;
30
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 public class SdncArtifactMap {
35
36         private static final Logger LOG = LoggerFactory
37                         .getLogger(SdncArtifactMap.class);
38
39         public class SdncArtifactType {
40                 private String tag;
41                 private String artifactType;
42                 private String rpc;
43                 private int pass;
44
45                 public String getTag() {
46                         return tag;
47                 }
48                 public String getArtifactType() {
49                         return artifactType;
50                 }
51                 public String getRpc() {
52                         return rpc;
53                 }
54
55                 public int getPass() {
56                         return pass;
57                 }
58
59         public String getRpcUrl(String base) {
60                         return(base+rpc);
61         }
62
63                 private SdncArtifactType(String tag, String rpc, String pass) {
64                         this.tag = tag;
65                         this.rpc = rpc;
66                         try {
67                                 this.pass = Integer.parseInt(pass);
68                         } catch (Exception e) {
69                                 LOG.error("Invalid pass value for artifact map entry ("+tag+","+rpc+","+pass+")");
70                         }
71                 }
72         }
73
74
75
76         private Map<String, SdncArtifactType> mapItems = new HashMap<String, SdncArtifactType>();
77
78         private int NumPasses = 1;
79
80         public int getNumPasses() {
81                 return NumPasses;
82         }
83
84         public void load(String fileName) {
85
86                 File mapFile = new File(fileName);
87
88                 if (mapFile.exists() && mapFile.canRead()) {
89
90                         BufferedReader rdr = null;
91                         try {
92
93                                 rdr = new BufferedReader(new FileReader(mapFile));
94
95                                 for (String ln ; (ln = rdr.readLine()) != null ; ) {
96                                         String[] lnFields = ln.split(",");
97                                         if (lnFields.length == 3) {
98                                                 SdncArtifactType entry = new SdncArtifactType(lnFields[0], lnFields[1], lnFields[2]);
99                                                 mapItems.put(entry.getTag(), entry);
100                                                 if (entry.getPass() + 1 > NumPasses ) {
101                                                         NumPasses = entry.getPass() + 1;
102                                                 }
103                                         }
104                                 }
105
106
107                         } catch (Exception e) {
108                                 LOG.error("Caught exception reading artifact map", e);
109                                 return;
110                         } finally {
111                                 if (rdr != null) {
112                                         try {
113                                                 rdr.close();
114                                         } catch (IOException e) {
115
116                                         }
117                                 }
118                         }
119                 }
120         }
121
122         public SdncArtifactType getMapping(String tag) {
123                 if (mapItems.containsKey(tag)) {
124                         return(mapItems.get(tag));
125                 } else {
126                         return(null);
127                 }
128         }
129
130         public static SdncArtifactMap getInstance() {
131                 return new SdncArtifactMap();
132         }
133
134 }