Release version 1.1.0 of sli/northbound
[ccsdk/sli/northbound.git] / dmaap-listener / src / main / java / org / onap / ccsdk / sli / northbound / dmaapclient / SdncLcmDmaapConsumer.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : SDN-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                      reserved.
7  * Modifications Copyright © 2018 IBM.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.ccsdk.sli.northbound.dmaapclient;
24
25 import com.fasterxml.jackson.databind.JsonNode;
26 import com.fasterxml.jackson.databind.ObjectMapper;
27
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 public class SdncLcmDmaapConsumer extends SdncDmaapConsumerImpl {
32
33     private static final Logger LOG = LoggerFactory.getLogger(SdncLcmDmaapConsumer.class);
34
35     private static final String BODY = "body";
36     private static final String RPC = "rpc-name";
37     
38     @Override
39     public void processMsg(String msg) throws InvalidMessageException {
40
41         if (msg == null) {
42             throw new InvalidMessageException("Null LCM message");
43         }
44
45         ObjectMapper oMapper = new ObjectMapper();
46         JsonNode lcmRootNode;
47         try {
48             lcmRootNode = oMapper.readTree(msg);
49         } catch (Exception e) {
50             throw new InvalidMessageException("Cannot parse LCM json input", e);
51         }        
52
53         JsonNode bodyNode = lcmRootNode.get(BODY);
54         if(bodyNode == null) {
55             LOG.warn("Missing body in LCM message");
56             return;
57         }
58         String rpcMsgbody;
59         try {
60                 ObjectMapper mapper = new ObjectMapper();
61                 rpcMsgbody = mapper.writeValueAsString(bodyNode);
62                 
63         } catch (Exception e) {
64             LOG.error("Unable to parse body in LCM message", e);
65             return;
66         }
67       
68         JsonNode rpcNode = lcmRootNode.get(RPC);
69         if(rpcNode == null) {
70             LOG.warn("Missing node in LCM message- " + RPC);
71             return;
72         }
73         String rpc = rpcNode.textValue();
74         String sdncEndpoint = "LCM:" + rpc;
75               
76         try {
77             String odlUrlBase = getProperty("sdnc.odl.url-base");
78             String odlUser = getProperty("sdnc.odl.user");
79             String odlPassword = getProperty("sdnc.odl.password");
80             LOG.info("POST LCM Request " + rpcMsgbody);
81             if ((odlUrlBase != null) && (odlUrlBase.length() > 0)) {
82                 SdncOdlConnection conn = SdncOdlConnection.newInstance(odlUrlBase + "/" + sdncEndpoint, odlUser, odlPassword);
83
84                 conn.send("POST", "application/json", rpcMsgbody);
85             } else {
86                 LOG.warn("Unable to POST LCM message. SDNC URL not available. body:\n" + rpcMsgbody);
87             }
88         } catch (Exception e) {
89             LOG.error("Unable to process message", e);
90         }
91     }
92 }
93