Fix dg-common bundle error
[appc.git] / appc-service-communicator / appc-service-communicator-bundle / src / main / java / org / onap / appc / srvcomm / messaging / event / EventSender.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * ================================================================================
9  * Modifications Copyright (C) 2019 Ericsson
10  * =============================================================================
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  * 
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  * 
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  * 
23  * ============LICENSE_END=========================================================
24  */
25
26 package org.onap.appc.srvcomm.messaging.event;
27
28 import com.att.eelf.configuration.EELFLogger;
29 import com.att.eelf.configuration.EELFManager;
30 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
31 import org.onap.appc.configuration.Configuration;
32 import org.onap.appc.configuration.ConfigurationFactory;
33 import org.onap.appc.exceptions.APPCException;
34 import org.onap.appc.srvcomm.messaging.MessageDestination;
35 import org.onap.appc.srvcomm.messaging.MessagingConnector;
36 import java.util.Date;
37 import java.util.Map;
38
39
40 public class EventSender implements EventSenderInterface
41 {
42     private final EELFLogger LOG = EELFManager.getInstance().getLogger(EventSender.class);
43     public static final String PROPERTY_PREFIX = "dmaap.event";
44
45     private static Configuration configuration = ConfigurationFactory.getConfiguration();
46
47     private MessagingConnector messagingConnector;
48
49     public EventSender(){
50         messagingConnector = new MessagingConnector();
51     }
52
53     @Override
54     public boolean sendEvent(MessageDestination destination, EventMessage msg) {
55         String jsonStr = msg.toJson();
56         String id = msg.getEventHeader().getEventId();
57         LOG.info(String.format("Posting Message [%s - %s]", id, jsonStr));
58         String propertyPrefix = destination.toString() + "." + PROPERTY_PREFIX;
59         return messagingConnector.publishMessage(propertyPrefix, id, jsonStr);
60     }
61
62     @Override
63     public boolean sendEvent(MessageDestination destination, EventMessage msg, String eventTopicName) {
64         String jsonStr = msg.toJson();
65         String id = msg.getEventHeader().getEventId();
66         LOG.info(String.format("Posting Message [%s - %s]", id, jsonStr));
67         String propertyPrefix = destination.toString() + "." + PROPERTY_PREFIX;
68         return messagingConnector.publishMessage(propertyPrefix, id, eventTopicName, jsonStr);
69     }
70
71     @Override
72     public boolean sendEvent(MessageDestination destination, Map<String, String> params, SvcLogicContext ctx) throws APPCException {
73
74         if (params == null) {
75             String message = "Parameters map is empty (null)";
76             LOG.error(message);
77             throw new APPCException(message);
78         }
79         String eventTime = new Date(System.currentTimeMillis()).toString();
80         String apiVer = params.get("apiVer");
81         String eventId = params.get("eventId");
82         String reason = params.get("reason");
83         String entityId = params.get("entityId");
84         if(entityId != null){
85             reason += "(" + entityId + ")";
86         }
87         Integer code = Integer.getInteger(params.get("code"), 500);
88
89         if (eventTime == null || apiVer == null || eventId == null || reason == null) {
90             String message = String.format("Missing input parameters: %s", params);
91             LOG.error(message);
92             throw new APPCException(message);
93         }
94         EventMessage eventMessage = new EventMessage(
95                         new EventHeader(eventTime, apiVer, eventId),
96                         new EventStatus(code, reason));
97
98         return sendEvent(destination, eventMessage);
99     }
100     
101 }