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 org.onap.dmaap.mr.client.MRClientFactory;
26 import org.onap.dmaap.mr.client.MRConsumer;
27 import org.onap.dmaap.mr.client.response.MRConsumerResponse;
28 import org.oransc.policyagent.configuration.ApplicationConfig;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.scheduling.annotation.EnableScheduling;
33 import org.springframework.scheduling.annotation.Scheduled;
34 import org.springframework.stereotype.Component;
35
36 @Component
37 @EnableScheduling
38 public class DmaapMessageConsumerImpl implements DmaapMessageConsumer {
39
40     private static final Logger logger = LoggerFactory.getLogger(DmaapMessageConsumerImpl.class);
41
42     private boolean alive = false;
43     protected MRConsumer consumer;
44     private MRConsumerResponse response = null;
45
46     @Autowired
47     public DmaapMessageConsumerImpl(ApplicationConfig applicationConfig) {
48         Properties dmaapConsumerConfig = applicationConfig.getDmaapConsumerConfig();
49         init(dmaapConsumerConfig);
50     }
51
52     @Scheduled(fixedRate = 1000 * 60)
53     @Override
54     public void run() {
55         while (this.alive) {
56             try {
57                 Iterable<String> dmaapMsgs = fetchAllMessages();
58                 for (String msg : dmaapMsgs) {
59                     processMsg(msg);
60                 }
61             } catch (Exception e) {
62                 logger.error("{}: cannot fetch because of ", this, e.getMessage(), e);
63             }
64         }
65     }
66
67     private Iterable<String> fetchAllMessages() {
68         response = consumer.fetchWithReturnConsumerResponse();
69         if (response == null) {
70             logger.warn("{}: DMaaP NULL response received", this);
71         } else {
72             logger.debug("DMaaP consumer received {} : {}", response.getResponseCode(), response.getResponseMessage());
73             if (!"200".equals(response.getResponseCode())) {
74                 logger.error("DMaaP consumer received: {} : {}", response.getResponseCode(),
75                         response.getResponseMessage());
76             }
77         }
78         return response.getActualMessages();
79     }
80
81     @Override
82     public void init(Properties properties) {
83         // Initialize the DMAAP with the properties
84         // Do we need to do any validation of properties before calling the factory?
85         Properties prop = new Properties();
86         prop.setProperty("ServiceName", "localhost:6845/events");
87         prop.setProperty("topic", "A1-P");
88         prop.setProperty("host", "localhost:6845");
89         prop.setProperty("contenttype", "application/json");
90         prop.setProperty("username", "admin");
91         prop.setProperty("password", "admin");
92         prop.setProperty("group", "users");
93         prop.setProperty("id", "policy-agent");
94         prop.setProperty("TransportType", "HTTPNOAUTH");
95         prop.setProperty("timeout", "15000");
96         prop.setProperty("limit", "1000");
97         try {
98             consumer = MRClientFactory.createConsumer(prop);
99             this.alive = true;
100         } catch (IOException e) {
101             logger.error("Exception occurred while creating Dmaap Consumer", e);
102         }
103     }
104
105     @Override
106     public void processMsg(String msg) throws Exception {
107         System.out.println("sysout" + msg);
108         // Call the concurrent Task executor to handle the incoming request
109         // Validate the Input & if its valid, post the ACCEPTED Response back to DMAAP
110         // through REST CLIENT
111         // Call the Controller with the extracted payload
112     }
113
114     @Override
115     public boolean isAlive() {
116         return alive;
117     }
118
119     @Override
120     public void stopConsumer() {
121         alive = false;
122     }
123 }