623904e262bddd4ded08df6464162b529ee7bfa4
[appc.git] / appc-event-listener / appc-event-listener-bundle / src / main / java / org / openecomp / appc / listener / demo / impl / WorkerImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.openecomp.appc.listener.demo.impl;
26
27 import org.openecomp.appc.exceptions.APPCException;
28 import org.openecomp.appc.listener.EventHandler;
29 import org.openecomp.appc.listener.demo.model.IncomingMessage;
30 import org.openecomp.appc.listener.demo.model.Status;
31
32 import com.att.eelf.configuration.EELFLogger;
33 import com.att.eelf.configuration.EELFManager;
34
35 public class WorkerImpl implements Runnable {
36
37     private final EELFLogger LOG = EELFManager.getInstance().getLogger(WorkerImpl.class);
38
39     // Should have all of the data we need for processing
40     private IncomingMessage event;
41
42     // So we can post messages from inside the worker.
43     private EventHandler dmaap;
44
45     public WorkerImpl(IncomingMessage message, EventHandler dmaap) {
46         this.event = message;
47         this.dmaap = dmaap;
48     }
49
50     @Override
51     public void run() {
52         LOG.debug(String.format("Started working on %s",  event.getHeader().getRequestID()));
53     
54         dmaap.postStatus(event.toOutgoing(Status.ACCEPTED));
55         // Run the dg in a try catch to handle all exceptions and update the
56         // message at the end
57         try {
58             if (doDG(event)) {
59                 dmaap.postStatus(event.toOutgoing(Status.SUCCESS));
60                 LOG.debug(String.format("Event %s finished successfully",  event.getHeader().getRequestID()));
61             } else {
62                 // Should never happen. Exception with message should be thrown instead.
63                 LOG.error(String.format(
64                     "We somehow returned false from doDG() instead of throwing exception. Incoming event [%s]",
65                     event.toJson().toString()));
66                 dmaap.postStatus(event.toOutgoing(Status.FAILURE));
67             }
68
69         } catch (Exception e) {
70             // Unknown exception from DG method. Fail and pass the exception
71             // along
72             String msg = "Exception: " + e.getMessage();
73             LOG.warn(String.format("Event %s finished with failure. %s", event.getHeader().getRequestID(), msg));
74             //TODO: should a message be included? there is nothing in the API spec for a msg?            
75             //dmaap.postStatus(event.toOutgoing(Status.FAILURE, msg));
76             dmaap.postStatus(event.toOutgoing(Status.FAILURE));
77         }
78
79         LOG.debug("Done working on " + event.getHeader().getRequestID());
80     }
81
82     private boolean doDG(IncomingMessage msg) throws APPCException {
83         return ProviderOperations.topologyDG(msg);
84     }
85 }