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