Implement DMaaP message handling from policy
[dcaegen2/services/son-handler.git] / src / main / java / org / onap / dcaegen2 / services / sonhms / PmThread.java
1 /*******************************************************************************
2  *  ============LICENSE_START=======================================================
3  *  son-handler
4  *  ================================================================================
5  *   Copyright (C) 2019 Wipro Limited.
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
22 package org.onap.dcaegen2.services.sonhms;
23
24 import fj.data.Either;
25
26 import org.onap.dcaegen2.services.sonhms.dmaap.PolicyDmaapClient;
27 import org.onap.dcaegen2.services.sonhms.model.PmNotification;
28 import org.onap.dcaegen2.services.sonhms.utils.DmaapUtils;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 public class PmThread implements Runnable {
33
34     private static Logger log = LoggerFactory.getLogger(PmThread.class);
35
36     private NewPmNotification newPmNotification;
37     
38
39     private DmaapNotificationsComponent dmaapNotificationsComponent;
40
41     private PmNotificationHandler pmNotificationHandler;
42
43     /**
44      * parameterized constructor.
45      */
46     public PmThread(NewPmNotification newPmNotification) {
47         super();
48         this.newPmNotification = newPmNotification;
49         dmaapNotificationsComponent = new DmaapNotificationsComponent();
50         pmNotificationHandler = new PmNotificationHandler(new PolicyDmaapClient(new DmaapUtils(), 
51                 Configuration.getInstance()));
52     }
53
54     @Override
55     public void run() {
56         log.info("PM thread starting ...");
57         // check for PM notifications
58         Boolean done = false;
59         while (!done) {
60             try {
61                 Thread.sleep(1000);
62                 if (newPmNotification.getNewNotif()) {
63                     log.info("New PM notification from Dmaap");
64                     Either<PmNotification, Integer> pmNotification = dmaapNotificationsComponent.getPmNotifications();
65                     if (pmNotification.isRight()) {
66                         if (pmNotification.right().value() == 400) {
67                             log.error("error parsing pm notifications");
68                         } else if (pmNotification.right().value() == 404) {
69                             log.info("Queue is empty");
70                             newPmNotification.setNewNotif(false);
71                         }
72                     } else if (pmNotification.isLeft()) {
73                         Configuration configuration = Configuration.getInstance();
74                         Boolean result = pmNotificationHandler.handlePmNotifications(pmNotification.left().value(),
75                                 configuration.getBadThreshold());
76                         log.info("pm notification handler result {}", result);
77                     }
78
79                 }
80             } catch (Exception e) {
81                 log.error("Exception in PM Thread ", e);
82                 done = true;
83             }
84         }
85
86     }
87
88 }