1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
/*******************************************************************************
* ============LICENSE_START=======================================================
* son-handler
* ================================================================================
* Copyright (C) 2019 Wipro Limited.
* ==============================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ============LICENSE_END=========================================================
*
*******************************************************************************/
package org.onap.dcaegen2.services.sonhms.dmaap;
import com.att.nsa.cambria.client.CambriaBatchingPublisher;
import com.att.nsa.cambria.client.CambriaConsumer;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.Map;
import org.onap.dcaegen2.services.sonhms.Configuration;
import org.onap.dcaegen2.services.sonhms.model.Payload;
import org.onap.dcaegen2.services.sonhms.model.PolicyNotification;
import org.onap.dcaegen2.services.sonhms.utils.DmaapUtils;
import org.slf4j.Logger;
public class PolicyDmaapClient {
private static final Logger log = org.slf4j.LoggerFactory.getLogger(PolicyDmaapClient.class);
private DmaapUtils dmaapUtils;
private Configuration configuration;
public PolicyDmaapClient(DmaapUtils dmaapUtils, Configuration configuration) {
this.dmaapUtils = dmaapUtils;
this.configuration = configuration;
}
/**
* Method stub for sending notification to policy.
*/
@SuppressWarnings("unchecked")
public boolean sendNotificationToPolicy(String msg) {
Map<String, Object> streamsPublishes = configuration.getStreamsPublishes();
String policyTopicUrl = ((Map<String, String>) ((Map<String, Object>) streamsPublishes.get("CL_topic"))
.get("dmaap_info")).get("topic_url");
String[] policyTopicSplit = policyTopicUrl.split("\\/");
String policyTopic = policyTopicSplit[policyTopicSplit.length - 1];
CambriaBatchingPublisher cambriaBatchingPublisher;
try {
cambriaBatchingPublisher = dmaapUtils.buildPublisher(configuration, policyTopic);
NotificationProducer notificationProducer = new NotificationProducer(cambriaBatchingPublisher);
notificationProducer.sendNotification(msg);
} catch (IOException e) {
return false;
}
return true;
}
/**
* Handles policy response.
*/
@SuppressWarnings("unchecked")
public Boolean handlePolicyResponse(String requestId) {
Map<String, Object> streamSubscribes = configuration.getStreamsSubscribes();
String policyResponseTopicUrl = ((Map<String, String>) ((Map<String, Object>) streamSubscribes
.get("dcae_cl_response_topic")).get("dmaap_info")).get("topic_url");
String[] policyResponseTopicUrlSplit = policyResponseTopicUrl.split("\\/");
String policyResponseTopic = policyResponseTopicUrlSplit[policyResponseTopicUrlSplit.length - 1];
log.debug("policyResponse Topic : {}", policyResponseTopic);
CambriaConsumer policyResponseCambriaConsumer = null;
policyResponseCambriaConsumer = dmaapUtils.buildConsumer(configuration, policyResponseTopic);
Timestamp startTimer = new Timestamp(System.currentTimeMillis());
Timestamp currentTime = new Timestamp(System.currentTimeMillis());
Long difference = currentTime.getTime() - startTimer.getTime();
while (difference < 10000) {
Iterable<String> policyResponseMessages;
try {
policyResponseMessages = policyResponseCambriaConsumer.fetch();
for (String msg : policyResponseMessages) {
ObjectMapper mapper = new ObjectMapper();
PolicyNotification policyResponse = mapper.readValue(msg, PolicyNotification.class);
if (requestId.equals(policyResponse.getRequestId())) {
String payload = policyResponse.getPayload();
Payload payloadObject = mapper.readValue(payload, Payload.class);
int status = payloadObject.getConfiguration().get(0).getStatus().getCode();
String statusToString = Integer.toString(status);
log.info("response from policy, status code {}", statusToString);
}
return true;
}
} catch (IOException e) {
log.info("caught io exception while fetching policy response");
}
currentTime = new Timestamp(System.currentTimeMillis());
difference = currentTime.getTime() - startTimer.getTime();
}
log.info("no response from policy, timer expired");
return true;
}
}
|