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