Update license header in appc-event-listener files
[appc.git] / appc-event-listener / appc-event-listener-bundle / src / main / java / org / onap / appc / listener / demo / impl / WorkerImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * 
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.listener.demo.impl;
25
26 import org.onap.appc.exceptions.APPCException;
27 import org.onap.appc.listener.EventHandler;
28 import org.onap.appc.listener.demo.model.IncomingMessage;
29 import org.onap.appc.listener.demo.model.Status;
30
31 import com.att.eelf.configuration.EELFLogger;
32 import com.att.eelf.configuration.EELFManager;
33
34 public class WorkerImpl implements Runnable {
35
36     private final EELFLogger LOG = EELFManager.getInstance().getLogger(WorkerImpl.class);
37
38     // Should have all of the data we need for processing
39     private IncomingMessage event;
40
41     // So we can post messages from inside the worker.
42     private EventHandler dmaap;
43
44     public WorkerImpl(IncomingMessage message, EventHandler dmaap) {
45         this.event = message;
46         this.dmaap = dmaap;
47     }
48
49     @Override
50     public void run() {
51         LOG.debug(String.format("Started working on %s",  event.getHeader().getRequestID()));
52     
53         dmaap.postStatus(event.toOutgoing(Status.ACCEPTED));
54         // Run the dg in a try catch to handle all exceptions and update the
55         // message at the end
56         try {
57             if (doDG(event)) {
58                 dmaap.postStatus(event.toOutgoing(Status.SUCCESS));
59                 LOG.debug(String.format("Event %s finished successfully",  event.getHeader().getRequestID()));
60             } else {
61                 // Should never happen. Exception with message should be thrown instead.
62                 LOG.error(String.format(
63                     "We somehow returned false from doDG() instead of throwing exception. Incoming event [%s]",
64                     event.toJson().toString()));
65                 dmaap.postStatus(event.toOutgoing(Status.FAILURE));
66             }
67
68         } catch (Exception e) {
69             // Unknown exception from DG method. Fail and pass the exception
70             // along
71             String msg = "Exception: " + e.getMessage();
72             LOG.warn(String.format("Event %s finished with failure. %s", event.getHeader().getRequestID(), msg));
73             //TODO: should a message be included? there is nothing in the API spec for a msg?            
74             //dmaap.postStatus(event.toOutgoing(Status.FAILURE, msg));
75             dmaap.postStatus(event.toOutgoing(Status.FAILURE));
76         }
77
78         LOG.debug("Done working on " + event.getHeader().getRequestID());
79     }
80
81     private boolean doDG(IncomingMessage msg) throws APPCException {
82         return ProviderOperations.topologyDG(msg);
83     }
84 }