Implement Enhancements to the Microservice
[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.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 public class PMThread implements Runnable {
32
33     private static Logger log = LoggerFactory.getLogger(PMThread.class);
34
35     private NewPmNotification newPmNotification;
36
37     private DmaapNotificationsComponent dmaapNotificationsComponent;
38
39     private PMNotificationHandler pmNotificationHandler;
40
41     /**
42      * parameterized constructor.
43      */
44     public PMThread(NewPmNotification newPmNotification) {
45         super();
46         this.newPmNotification = newPmNotification;
47         dmaapNotificationsComponent = new DmaapNotificationsComponent();
48         pmNotificationHandler = new PMNotificationHandler(new PolicyDmaapClient());
49     }
50
51     @Override
52     public void run() {
53         log.info("PM thread starting ...");
54         // check for PM notifications
55         Boolean done = false;
56         while (!done) {
57             try {
58                 Thread.sleep(1000);
59                 if (newPmNotification.getNewNotif()) {
60                     log.info("New PM notification from Dmaap");
61                     Either<PMNotification, Integer> pmNotification = dmaapNotificationsComponent.getPmNotifications();
62                     if (pmNotification.isRight()) {
63                         if (pmNotification.right().value() == 400) {
64                             log.error("error parsing pm notifications");
65                         } else if (pmNotification.right().value() == 404) {
66                             log.info("Queue is empty");
67                             newPmNotification.setNewNotif(false);
68                         }
69                     } else if (pmNotification.isLeft()) {
70                         Configuration configuration = Configuration.getInstance();
71                         Boolean result = pmNotificationHandler.handlePmNotifications(pmNotification.left().value(),
72                                 configuration.getBadThreshold());
73                         log.info("pm notification handler result {}", result);
74                     }
75
76                 }
77             } catch (Exception e) {
78                 log.error("Exception in PM Thread ", e);
79                 done = true;
80             }
81         }
82
83     }
84
85 }