2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ============LICENSE_END=========================================================
24 package org.onap.appc.listener.demo.model;
26 import java.net.InetAddress;
27 import java.security.SecureRandom;
28 import java.text.SimpleDateFormat;
29 import java.util.Date;
30 import java.util.TimeZone;
32 import org.json.JSONObject;
33 import org.onap.appc.listener.util.Mapper;
34 import org.onap.appc.util.Time;
36 import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
37 import com.fasterxml.jackson.annotation.JsonProperty;
38 import com.fasterxml.jackson.databind.annotation.JsonSerialize;
39 import com.fasterxml.jackson.databind.annotation.JsonSerialize.Inclusion;
42 * This class represents a message being sent out to DMaaP by APPC to update listeners on the status of a request
45 @JsonSerialize(include = Inclusion.NON_NULL)
46 @JsonIgnoreProperties(ignoreUnknown = true)
47 public class OutgoingMessage extends CommonMessage {
49 public OutgoingMessage() {
53 public OutgoingMessage(IncomingMessage msg) {
54 setHeader(msg.getHeader());
55 setPayload(msg.getPayload());
56 // setId(msg.getId());
57 // setOriginalRequest(msg.getRequest());
58 // setRequestClient(msg.getRequestClient());
59 // setRequestTime(msg.getRequestTime());
60 // setVmName(msg.getVmName());
61 // setFromSystem(generateFrom());
62 // setResponse(Status.PENDING);
63 // setPolicyName(msg.getPolicyName());
64 // setPolicyVersion(msg.getPolicyVersion());
65 // setStartTime(msg.getStartTime());
68 private static final long serialVersionUID = -5447940920271469613L;
70 * The status of the response
72 @JsonProperty("Status")
73 private OutStatus status;
78 public OutStatus getStatus() {
83 * @param status the status to set
85 public void setStatus(OutStatus status) {
89 public void updateResponseTime() {
90 SecureRandom rand = new SecureRandom();
91 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss.SSS");
92 df.setTimeZone(TimeZone.getTimeZone("UTC"));
93 String date = df.format(new Date(Time.utcTime()));
94 //this.responseTime = String.format("%s%03d", date, rand.nextInt(1000));
97 public String generateFrom() {
100 InetAddress iAddress = InetAddress.getLocalHost();
101 name = iAddress.getCanonicalHostName();
102 } catch (Exception e) {
103 // Could not get anything from the InetAddress
104 name = "UnknownHost";
106 return "appc@" + name;
109 public JSONObject toResponse() {
110 updateResponseTime();
111 JSONObject json = Mapper.toJsonObject(this);
113 if (!json.has("message")) {
114 // If there is no message, parrot the status (response field)
115 // TODO - Can this be removed for 1602 making message truely optional?
116 //json.put("message", this.getResponse().toString());
119 // Removed duplication of status from message for 1602
120 // json.put("message", String.format("%s: %s", request, json.get("message")));
126 // public String toString() {
127 // return String.format("%s - %s", getId(), getResponse());
130 public static class OutStatus{
131 @JsonProperty("Code")
134 @JsonProperty("Value")
135 private String value;
140 public String getCode() {
145 * @param code the code to set
147 public void setCode(String code) {
154 public String getValue() {
159 * @param value the value to set
161 public void setValue(String value) {
167 public void setResponse(Status newStatus) {
168 if(this.status == null){
169 this.status = new OutStatus();
174 this.status.setValue(newStatus.getValue());
175 this.status.setCode("100");
179 this.status.setValue(newStatus.getValue());
180 this.status.setCode("500");
184 this.status.setValue(newStatus.getValue());
185 this.status.setCode("400");