Merge "Adapt A1 controller to latest A1 spec"
[nonrtric.git] / policy-agent / src / main / java / org / oransc / policyagent / dmaap / DmaapMessageConsumerImpl.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 Nordix Foundation
6  * %%
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ========================LICENSE_END===================================
19  */
20
21 package org.oransc.policyagent.dmaap;
22
23 import java.io.IOException;
24 import java.util.Properties;
25 import javax.annotation.PostConstruct;
26 import org.onap.dmaap.mr.client.MRClientFactory;
27 import org.onap.dmaap.mr.client.MRConsumer;
28 import org.onap.dmaap.mr.client.response.MRConsumerResponse;
29 import org.oransc.policyagent.configuration.ApplicationConfig;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.scheduling.annotation.EnableScheduling;
34 import org.springframework.scheduling.annotation.Scheduled;
35 import org.springframework.stereotype.Component;
36
37 @Component
38 @EnableScheduling
39 public class DmaapMessageConsumerImpl implements DmaapMessageConsumer {
40
41     private static final Logger logger = LoggerFactory.getLogger(DmaapMessageConsumerImpl.class);
42
43     private boolean alive = false;
44     private final ApplicationConfig applicationConfig;
45     protected MRConsumer consumer;
46     private MRConsumerResponse response = null;
47     @Autowired
48     private DmaapMessageHandler dmaapMessageHandler;
49
50     @Autowired
51     public DmaapMessageConsumerImpl(ApplicationConfig applicationConfig) {
52         this.applicationConfig = applicationConfig;
53     }
54
55     @Scheduled(fixedRate = 1000 * 10) // , initialDelay=60000)
56     @Override
57     public void run() {
58         /*
59          * if (!alive) { init(); }
60          */
61         if (this.alive) {
62             try {
63                 Iterable<String> dmaapMsgs = fetchAllMessages();
64                 logger.debug("Fetched all the messages from DMAAP and will start to process the messages");
65                 for (String msg : dmaapMsgs) {
66                     processMsg(msg);
67                 }
68             } catch (Exception e) {
69                 logger.error("{}: cannot fetch because of ", this, e.getMessage(), e);
70             }
71         }
72     }
73
74     private Iterable<String> fetchAllMessages() {
75         response = consumer.fetchWithReturnConsumerResponse();
76         if (response == null) {
77             logger.warn("{}: DMaaP NULL response received", this);
78         } else {
79             logger.debug("DMaaP consumer received {} : {}", response.getResponseCode(), response.getResponseMessage());
80             if (!"200".equals(response.getResponseCode())) {
81                 logger.error("DMaaP consumer received: {} : {}", response.getResponseCode(),
82                         response.getResponseMessage());
83             }
84         }
85         return response.getActualMessages();
86     }
87
88     @PostConstruct
89     @Override
90     public void init() {
91         Properties dmaapConsumerProperties = applicationConfig.getDmaapConsumerConfig();
92         Properties dmaapPublisherProperties = applicationConfig.getDmaapPublisherConfig();
93         // No need to start if there is no configuration.
94         if (dmaapConsumerProperties == null || dmaapPublisherProperties == null || dmaapConsumerProperties.size() == 0
95                 || dmaapPublisherProperties.size() == 0) {
96             logger.error("DMaaP properties Failed to Load");
97             return;
98         }
99         try {
100             logger.debug("Creating DMAAP Client");
101             consumer = MRClientFactory.createConsumer(dmaapConsumerProperties);
102             this.alive = true;
103         } catch (IOException e) {
104             logger.error("Exception occurred while creating Dmaap Consumer", e);
105         }
106     }
107
108     @Override
109     public void processMsg(String msg) throws Exception {
110         logger.debug("Message Reveived from DMAAP : {}", msg);
111         // Call the concurrent Task executor to handle the incoming request
112         dmaapMessageHandler.handleDmaapMsg(msg);
113     }
114
115     @Override
116     public boolean isAlive() {
117         return alive;
118     }
119
120     @Override
121     public void stopConsumer() {
122         alive = false;
123     }
124 }