2 * ============LICENSE_START=======================================================
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
15 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 * ============LICENSE_END=========================================================
26 package org.onap.appc.listener.demo.model;
28 import java.net.InetAddress;
29 import java.net.UnknownHostException;
30 import java.security.SecureRandom;
31 import java.text.SimpleDateFormat;
32 import java.util.Date;
33 import java.util.TimeZone;
35 import org.json.JSONObject;
36 import org.onap.appc.listener.util.Mapper;
37 import org.onap.appc.util.Time;
39 import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
40 import com.fasterxml.jackson.annotation.JsonInclude;
41 import com.fasterxml.jackson.annotation.JsonProperty;
44 * This class represents a message being sent out to DMaaP by APPC to update listeners on the status of a request
47 @JsonInclude(JsonInclude.Include.NON_NULL)
48 @JsonIgnoreProperties(ignoreUnknown = true)
49 public class OutgoingMessage extends CommonMessage {
51 public OutgoingMessage() {
55 public OutgoingMessage(IncomingMessage msg) {
56 setHeader(msg.getHeader());
57 setPayload(msg.getPayload());
58 // setId(msg.getId());
59 // setOriginalRequest(msg.getRequest());
60 // setRequestClient(msg.getRequestClient());
61 // setRequestTime(msg.getRequestTime());
62 // setVmName(msg.getVmName());
63 // setFromSystem(generateFrom());
64 // setResponse(Status.PENDING);
65 // setPolicyName(msg.getPolicyName());
66 // setPolicyVersion(msg.getPolicyVersion());
67 // setStartTime(msg.getStartTime());
70 private static final long serialVersionUID = -5447940920271469613L;
72 * The status of the response
74 @JsonProperty("Status")
75 private OutStatus status;
80 public OutStatus getStatus() {
85 * @param status the status to set
87 public void setStatus(OutStatus status) {
91 public void updateResponseTime() {
92 SecureRandom rand = new SecureRandom();
93 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss.SSS");
94 df.setTimeZone(TimeZone.getTimeZone("UTC"));
95 String date = df.format(new Date(Time.utcTime()));
96 //this.responseTime = String.format("%s%03d", date, rand.nextInt(1000));
99 public String generateFrom() {
102 InetAddress iAddress = getLocalHost();
103 name = iAddress.getCanonicalHostName();
104 } catch (Exception e) {
105 // Could not get anything from the InetAddress
106 name = "UnknownHost";
108 return "appc@" + name;
111 public JSONObject toResponse() {
112 updateResponseTime();
113 JSONObject json = Mapper.toJsonObject(this);
115 if (!json.has("message")) {
116 // If there is no message, parrot the status (response field)
117 // TODO - Can this be removed for 1602 making message truely optional?
118 //json.put("message", this.getResponse().toString());
121 // Removed duplication of status from message for 1602
122 // json.put("message", String.format("%s: %s", request, json.get("message")));
128 // public String toString() {
129 // return String.format("%s - %s", getId(), getResponse());
132 public static class OutStatus{
133 @JsonProperty("Code")
136 @JsonProperty("Value")
137 private String value;
142 public String getCode() {
147 * @param code the code to set
149 public void setCode(String code) {
156 public String getValue() {
161 * @param value the value to set
163 public void setValue(String value) {
169 public void setResponse(Status newStatus) {
170 if(this.status == null){
171 this.status = new OutStatus();
173 if(newStatus == null) {
179 this.status.setValue(newStatus.getValue());
180 this.status.setCode("100");
184 this.status.setValue(newStatus.getValue());
185 this.status.setCode("500");
189 this.status.setValue(newStatus.getValue());
190 this.status.setCode("400");
198 protected InetAddress getLocalHost() throws UnknownHostException {
199 return InetAddress.getLocalHost();