fix dmaap-listener startup and library
[sdnc/northbound.git] / dmaap-listener / src / main / java / org / openecomp / sdnc / dmaapclient / SdncFlatJsonDmaapConsumer.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.dmaapclient;
23
24 import java.io.BufferedReader;
25 import java.io.DataOutputStream;
26 import java.io.File;
27 import java.io.FileReader;
28 import java.io.IOException;
29 import java.io.InputStreamReader;
30 import java.net.Authenticator;
31 import java.net.HttpURLConnection;
32 import java.net.PasswordAuthentication;
33 import java.net.URL;
34 import java.util.HashMap;
35 import java.util.Iterator;
36 import java.util.Map;
37
38 import javax.net.ssl.HostnameVerifier;
39 import javax.net.ssl.HttpsURLConnection;
40 import javax.net.ssl.SSLSession;
41
42 import org.apache.commons.codec.binary.Base64;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46 import com.fasterxml.jackson.databind.JsonNode;
47 import com.fasterxml.jackson.databind.ObjectMapper;
48 import com.fasterxml.jackson.databind.node.ObjectNode;
49
50
51 public class SdncFlatJsonDmaapConsumer extends SdncDmaapConsumer {
52
53         private static final Logger LOG = LoggerFactory
54                         .getLogger(SdncFlatJsonDmaapConsumer.class);
55
56         private static final String DMAAPLISTENERROOT = "DMAAPLISTENERROOT";
57         private static final String SDNC_ENDPOINT = "SDNC.endpoint";
58
59
60
61         @Override
62         public void processMsg(String msg) throws InvalidMessageException {
63
64                 processMsg(msg, null);
65         }
66
67         public void processMsg(String msg, String mapDirName) throws InvalidMessageException {
68
69                 if (msg == null) {
70                         throw new InvalidMessageException("Null message");
71                 }
72
73                 ObjectMapper oMapper = new ObjectMapper();
74                 JsonNode instarRootNode = null;
75                 ObjectNode sdncRootNode = null;
76
77                 String instarMsgName = null;
78
79                 try {
80                          instarRootNode = oMapper.readTree(msg);
81                 } catch (Exception e) {
82                         throw new InvalidMessageException("Cannot parse json object", e);
83                 }
84
85                 Iterator<Map.Entry<String, JsonNode>> instarFields = instarRootNode.fields();
86
87                 while (instarFields.hasNext()) {
88                         Map.Entry<String, JsonNode> entry = instarFields.next();
89
90                         instarMsgName = entry.getKey();
91                         instarRootNode = entry.getValue();
92                         break;
93                 }
94
95                 Map<String,String> fieldMap = loadMap(instarMsgName, mapDirName);
96
97                 if (fieldMap == null) {
98                         throw new InvalidMessageException("Unable to process message - cannot load field mappings");
99                 }
100
101                 if (!fieldMap.containsKey(SDNC_ENDPOINT)) {
102                         throw new InvalidMessageException("No SDNC endpoint known for message "+instarMsgName);
103                 }
104
105                 String sdncEndpoint = fieldMap.get(SDNC_ENDPOINT);
106
107                 sdncRootNode = oMapper.createObjectNode();
108                 ObjectNode inputNode = oMapper.createObjectNode();
109
110
111                 for (String fromField : fieldMap.keySet()) {
112
113                         if (!SDNC_ENDPOINT.equals(fromField)) {
114                                 JsonNode curNode = instarRootNode.get(fromField);
115                                 if (curNode != null) {
116                                         String fromValue = curNode.textValue();
117
118                                         inputNode.put(fieldMap.get(fromField), fromValue);
119                                 }
120                         }
121                 }
122                 sdncRootNode.put("input", inputNode);
123
124                 try {
125                         String rpcMsgbody = oMapper.writeValueAsString(sdncRootNode);
126                         String odlUrlBase = getProperty("sdnc.odl.url-base");
127                         String odlUser = getProperty("sdnc.odl.user");
128                         String odlPassword = getProperty("sdnc.odl.password");
129
130                         if ((odlUrlBase != null) && (odlUrlBase.length() > 0)) {
131                                 SdncOdlConnection conn = SdncOdlConnection.newInstance(odlUrlBase + sdncEndpoint, odlUser, odlPassword);
132
133                                 conn.send("POST", "application/json", rpcMsgbody);
134                         } else {
135                                 LOG.info("POST message body would be:\n"+rpcMsgbody);
136                         }
137                 } catch (Exception e) {
138
139                 }
140
141         }
142
143         private Map<String,String> loadMap(String msgType, String mapDirName) {
144                 Map<String, String> results = new HashMap<String, String>();
145
146
147                 if (mapDirName == null) {
148                         String rootdir = System.getenv(DMAAPLISTENERROOT);
149
150                         if ((rootdir == null) || (rootdir.length() == 0)) {
151                                 rootdir = "/opt/app/dmaap-listener";
152                         }
153
154                         mapDirName = rootdir + "/lib";
155
156                 }
157
158                 String mapFilename = mapDirName + "/" + msgType + ".map";
159
160                 File mapFile = new File(mapFilename);
161
162                 if (!mapFile.canRead()) {
163                         LOG.error("Cannot read map file ("+mapFilename+")");
164                         return(null);
165                 }
166
167                 try {
168                         BufferedReader mapReader = new BufferedReader(new FileReader(mapFile));
169
170                         String curLine = null;
171
172                         while ((curLine = mapReader.readLine()) != null) {
173                                 curLine = curLine.trim();
174
175                                 if ((curLine.length() > 0) && (!curLine.startsWith("#"))) {
176
177                                         if (curLine.contains("=>")) {
178                                                 String[] entry = curLine.split("=>");
179                                                 if (entry.length == 2) {
180                                                         results.put(entry[0].trim(), entry[1].trim());
181                                                 }
182                                         }
183                                 }
184                         }
185                         mapReader.close();
186                 } catch (Exception e) {
187                         LOG.error("Caught exception reading map "+mapFilename, e);
188                         return(null);
189                 }
190
191                 return(results);
192         }
193
194
195
196 }