2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
 
   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
 
  12  *      http://www.apache.org/licenses/LICENSE-2.0
 
  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=========================================================
 
  22 package org.onap.ccsdk.sli.northbound.dmaapclient;
 
  24 import java.io.BufferedReader;
 
  25 import java.io.DataOutputStream;
 
  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;
 
  34 import java.util.HashMap;
 
  35 import java.util.Iterator;
 
  38 import javax.net.ssl.HostnameVerifier;
 
  39 import javax.net.ssl.HttpsURLConnection;
 
  40 import javax.net.ssl.SSLSession;
 
  42 import org.apache.commons.codec.binary.Base64;
 
  43 import org.slf4j.Logger;
 
  44 import org.slf4j.LoggerFactory;
 
  46 import com.fasterxml.jackson.databind.JsonNode;
 
  47 import com.fasterxml.jackson.databind.ObjectMapper;
 
  48 import com.fasterxml.jackson.databind.node.ObjectNode;
 
  51 public class SdncFlatJsonDmaapConsumer extends SdncDmaapConsumer {
 
  53         private static final Logger LOG = LoggerFactory
 
  54                         .getLogger(SdncFlatJsonDmaapConsumer.class);
 
  56         private static final String DMAAPLISTENERROOT = "DMAAPLISTENERROOT";
 
  57         private static final String SDNC_ENDPOINT = "SDNC.endpoint";
 
  62         public void processMsg(String msg) throws InvalidMessageException {
 
  64                 processMsg(msg, null);
 
  67         public void processMsg(String msg, String mapDirName) throws InvalidMessageException {
 
  70                         throw new InvalidMessageException("Null message");
 
  73                 ObjectMapper oMapper = new ObjectMapper();
 
  74                 JsonNode instarRootNode = null;
 
  75                 ObjectNode sdncRootNode = null;
 
  77                 String instarMsgName = null;
 
  80                          instarRootNode = oMapper.readTree(msg);
 
  81                 } catch (Exception e) {
 
  82                         throw new InvalidMessageException("Cannot parse json object", e);
 
  85                 Iterator<Map.Entry<String, JsonNode>> instarFields = instarRootNode.fields();
 
  87                 while (instarFields.hasNext()) {
 
  88                         Map.Entry<String, JsonNode> entry = instarFields.next();
 
  90                         instarMsgName = entry.getKey();
 
  91                         instarRootNode = entry.getValue();
 
  95                 Map<String,String> fieldMap = loadMap(instarMsgName, mapDirName);
 
  97                 if (fieldMap == null) {
 
  98                         throw new InvalidMessageException("Unable to process message - cannot load field mappings");
 
 101                 if (!fieldMap.containsKey(SDNC_ENDPOINT)) {
 
 102                         throw new InvalidMessageException("No SDNC endpoint known for message "+instarMsgName);
 
 105                 String sdncEndpoint = fieldMap.get(SDNC_ENDPOINT);
 
 107                 sdncRootNode = oMapper.createObjectNode();
 
 108                 ObjectNode inputNode = oMapper.createObjectNode();
 
 111                 for (String fromField : fieldMap.keySet()) {
 
 113                         if (!SDNC_ENDPOINT.equals(fromField)) {
 
 114                                 JsonNode curNode = instarRootNode.get(fromField);
 
 115                                 if (curNode != null) {
 
 116                                         String fromValue = curNode.textValue();
 
 118                                         inputNode.put(fieldMap.get(fromField), fromValue);
 
 122                 sdncRootNode.put("input", inputNode);
 
 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");
 
 130                         if ((odlUrlBase != null) && (odlUrlBase.length() > 0)) {
 
 131                                 SdncOdlConnection conn = SdncOdlConnection.newInstance(odlUrlBase + sdncEndpoint, odlUser, odlPassword);
 
 133                                 conn.send("POST", "application/json", rpcMsgbody);
 
 135                                 LOG.info("POST message body would be:\n"+rpcMsgbody);
 
 137                 } catch (Exception e) {
 
 143         private Map<String,String> loadMap(String msgType, String mapDirName) {
 
 144                 Map<String, String> results = new HashMap<String, String>();
 
 147                 if (mapDirName == null) {
 
 148                         String rootdir = System.getenv(DMAAPLISTENERROOT);
 
 150                         if ((rootdir == null) || (rootdir.length() == 0)) {
 
 151                                 rootdir = "/opt/app/dmaap-listener";
 
 154                         mapDirName = rootdir + "/lib";
 
 158                 String mapFilename = mapDirName + "/" + msgType + ".map";
 
 160                 File mapFile = new File(mapFilename);
 
 162                 if (!mapFile.canRead()) {
 
 163                         LOG.error("Cannot read map file ("+mapFilename+")");
 
 167                 try (BufferedReader mapReader = new BufferedReader(new FileReader(mapFile))) {
 
 171                         while ((curLine = mapReader.readLine()) != null) {
 
 172                                 curLine = curLine.trim();
 
 174                                 if ((curLine.length() > 0) && (!curLine.startsWith("#"))) {
 
 176                                         if (curLine.contains("=>")) {
 
 177                                                 String[] entry = curLine.split("=>");
 
 178                                                 if (entry.length == 2) {
 
 179                                                         results.put(entry[0].trim(), entry[1].trim());
 
 185                 } catch (Exception e) {
 
 186                         LOG.error("Caught exception reading map "+mapFilename, e);