2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.openecomp.appc.listener.CL.model;
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;
30 import org.json.JSONObject;
31 import org.openecomp.appc.listener.util.Mapper;
32 import org.openecomp.appc.util.Time;
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;
41 * This class represents a message being sent out to DMaaP by APPC to update listeners on the status of a request
44 @JsonSerialize(include = Inclusion.NON_NULL)
45 @JsonIgnoreProperties(ignoreUnknown = true)
46 public class OutgoingMessage extends CommonMessage {
48 private static final long serialVersionUID = -5447940920271469613L;
50 @JsonProperty("response")
51 private Status response;
53 @JsonProperty("responseTime")
54 private String responseTime;
56 @JsonProperty("originalRequest")
57 private String originalRequest;
59 public OutgoingMessage() {
63 public OutgoingMessage(IncomingMessage msg) {
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());
76 @JsonProperty("duration")
77 public long getDuration() {
78 return System.currentTimeMillis() - getStartTime();
81 public Status getResponse() {
85 public String getResponseTime() {
89 public String getOriginalRequest() {
90 return originalRequest;
94 public void setResponse(Status response) {
95 this.response = response;
98 public void setResponse(String responseString) {
99 this.response = Status.valueOf(responseString);
102 public void setResponseTime(String responseTime) {
103 this.responseTime = responseTime;
106 public void setOriginalRequest(String originalRequest) {
107 this.originalRequest = originalRequest;
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));
118 public String generateFrom() {
121 InetAddress iAddress = InetAddress.getLocalHost();
122 name = iAddress.getCanonicalHostName();
123 } catch (Exception e) {
124 // Could not get anything from the InetAddress
125 name = "UnknownHost";
127 return "appc@" + name;
130 public JSONObject toResponse() {
131 updateResponseTime();
132 JSONObject json = Mapper.toJsonObject(this);
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());
140 // Removed duplication of status from message for 1602
141 // json.put("message", String.format("%s: %s", request, json.get("message")));
147 public String toString() {
148 return String.format("%s - %s", getId(), getResponse());