Release version 1.1.0 of sli/northbound
[ccsdk/sli/northbound.git] / dmaap-listener / src / main / java / org / onap / ccsdk / sli / northbound / dmaapclient / A1AdapterPolicyDmaapConsumer.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 © 2019 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 A1AdapterPolicyDmaapConsumer extends SdncDmaapConsumerImpl {
32
33     private static final Logger LOG = LoggerFactory.getLogger(A1AdapterPolicyDmaapConsumer.class);
34
35     private static final String BODY = "body";
36     private static final String RPC = "rpc-name";
37     private static final String INPUT = "input";
38     private static final String PAYLOAD = "Payload";
39
40
41     @Override
42     public void processMsg(String msg) throws InvalidMessageException {
43
44         if (msg == null) {
45             throw new InvalidMessageException("Null A1-ADAPTER-DMAAP message");
46         }
47
48         ObjectMapper oMapper = new ObjectMapper();
49         JsonNode a1AdapterRootNode;
50         try {
51             a1AdapterRootNode = oMapper.readTree(msg);
52         } catch (Exception e) {
53             throw new InvalidMessageException("Cannot parse A1-ADAPTER-DMAAP json input", e);
54         }
55
56         JsonNode bodyNode = a1AdapterRootNode.get(BODY);
57         if(bodyNode == null) {
58             LOG.warn("Missing body in A1-ADAPTER-DMAAP message");
59             return;
60         }
61
62         JsonNode input = bodyNode.get(INPUT);
63         if(input == null) {
64                  LOG.info("Missing input node.");
65                  return;
66         }
67
68         JsonNode payloadNode = input.get(PAYLOAD);
69         if(payloadNode == null) {
70             LOG.info("Missing payload node.");
71             return;
72         }
73
74         String rpcMsgbody;
75         try {
76                 ObjectMapper mapper = new ObjectMapper();
77                 rpcMsgbody = mapper.writeValueAsString(payloadNode);
78
79         } catch (Exception e) {
80             LOG.error("Unable to parse payload in A1-ADAPTER-DMAAP message", e);
81             return;
82         }
83
84         JsonNode rpcNode = a1AdapterRootNode.get(RPC);
85         if(rpcNode == null) {
86             LOG.warn("Missing node in A1-ADAPTER-DMAAP message- " + RPC);
87             return;
88         }
89         String rpc = rpcNode.textValue();
90         String sdncEndpoint = "A1-ADAPTER-API:" + rpc;
91
92         try {
93             String odlUrlBase = getProperty("sdnc.odl.url-base");
94             String odlUser = getProperty("sdnc.odl.user");
95             String odlPassword = getProperty("sdnc.odl.password");
96             LOG.info("POST A1-ADAPTER-API Request " + rpcMsgbody);
97             if ((odlUrlBase != null) && (odlUrlBase.length() > 0)) {
98                 SdncOdlConnection conn = SdncOdlConnection.newInstance(odlUrlBase + "/" + sdncEndpoint, odlUser, odlPassword);
99
100                 conn.send("POST", "application/json", rpcMsgbody);
101             } else {
102                 LOG.warn("Unable to POST A1-ADAPTER-API message. SDNC URL not available. body:\n" + rpcMsgbody);
103             }
104         } catch (Exception e) {
105             LOG.error("Unable to process message", e);
106         }
107     }
108 }