Change nexus values to properties
[appc.git] / app-c / appc / appc-event-listener / appc-event-listener-bundle / src / main / java / org / openecomp / appc / listener / CL / model / OutgoingMessage.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : APP-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
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  */
21
22 package org.openecomp.appc.listener.CL.model;
23
24 import java.net.InetAddress;
25 import java.security.SecureRandom;
26 import java.text.SimpleDateFormat;
27 import java.util.Date;
28 import java.util.TimeZone;
29
30 import org.json.JSONObject;
31 import org.openecomp.appc.listener.util.Mapper;
32 import org.openecomp.appc.util.Time;
33
34 import com.fasterxml.jackson.annotation.JsonIgnore;
35 import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
36 import com.fasterxml.jackson.annotation.JsonProperty;
37 import com.fasterxml.jackson.databind.annotation.JsonSerialize;
38 import com.fasterxml.jackson.databind.annotation.JsonSerialize.Inclusion;
39
40 /**
41  * This class represents a message being sent out to DMaaP by APPC to update listeners on the status of a request
42  *
43  */
44 @JsonSerialize(include = Inclusion.NON_NULL)
45 @JsonIgnoreProperties(ignoreUnknown = true)
46 public class OutgoingMessage extends CommonMessage {
47
48     private static final long serialVersionUID = -5447940920271469613L;
49
50     @JsonProperty("response")
51     private Status response;
52
53     @JsonProperty("responseTime")
54     private String responseTime;
55
56     @JsonProperty("originalRequest")
57     private String originalRequest;
58
59     public OutgoingMessage() {
60
61     }
62
63     public OutgoingMessage(IncomingMessage msg) {
64         setId(msg.getId());
65         setOriginalRequest(msg.getRequest());
66         setRequestClient(msg.getRequestClient());
67         setRequestTime(msg.getRequestTime());
68         setVmName(msg.getVmName());
69         setFromSystem(generateFrom());
70         setResponse(Status.PENDING);
71         setPolicyName(msg.getPolicyName());
72         setPolicyVersion(msg.getPolicyVersion());
73         setStartTime(msg.getStartTime());
74     }
75
76     @JsonProperty("duration")
77     public long getDuration() {
78         return System.currentTimeMillis() - getStartTime();
79     }
80
81     public Status getResponse() {
82         return response;
83     }
84
85     public String getResponseTime() {
86         return responseTime;
87     }
88
89     public String getOriginalRequest() {
90         return originalRequest;
91     }
92
93     @JsonIgnore
94     public void setResponse(Status response) {
95         this.response = response;
96     }
97
98     public void setResponse(String responseString) {
99         this.response = Status.valueOf(responseString);
100     }
101
102     public void setResponseTime(String responseTime) {
103         this.responseTime = responseTime;
104     }
105
106     public void setOriginalRequest(String originalRequest) {
107         this.originalRequest = originalRequest;
108     }
109
110     public void updateResponseTime() {
111         SecureRandom rand = new SecureRandom();
112         SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss.SSS");
113         df.setTimeZone(TimeZone.getTimeZone("UTC"));
114         String date = df.format(new Date(Time.utcTime()));
115         this.responseTime = String.format("%s%03d", date, rand.nextInt(1000));
116     }
117
118     public String generateFrom() {
119         String name;
120         try {
121             InetAddress iAddress = InetAddress.getLocalHost();
122             name = iAddress.getCanonicalHostName();
123         } catch (Exception e) {
124             // Could not get anything from the InetAddress
125             name = "UnknownHost";
126         }
127         return "appc@" + name;
128     }
129
130     public JSONObject toResponse() {
131         updateResponseTime();
132         JSONObject json = Mapper.toJsonObject(this);
133
134         if (!json.has("message")) {
135             // If there is no message, parrot the status (response field)
136             // TODO - Can this be removed for 1602 making message truely optional?
137             json.put("message", this.getResponse().toString());
138         }
139
140         // Removed duplication of status from message for 1602
141         // json.put("message", String.format("%s: %s", request, json.get("message")));
142
143         return json;
144     }
145
146     @Override
147     public String toString() {
148         return String.format("%s - %s", getId(), getResponse());
149     }
150 }