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.JsonProperty;
41 import com.fasterxml.jackson.databind.annotation.JsonSerialize;
42 import com.fasterxml.jackson.databind.annotation.JsonSerialize.Inclusion;
45 * This class represents a message being sent out to DMaaP by APPC to update listeners on the status of a request
48 @JsonSerialize(include = Inclusion.NON_NULL)
49 @JsonIgnoreProperties(ignoreUnknown = true)
50 public class OutgoingMessage extends CommonMessage {
52 public OutgoingMessage() {
56 public OutgoingMessage(IncomingMessage msg) {
57 setHeader(msg.getHeader());
58 setPayload(msg.getPayload());
59 // setId(msg.getId());
60 // setOriginalRequest(msg.getRequest());
61 // setRequestClient(msg.getRequestClient());
62 // setRequestTime(msg.getRequestTime());
63 // setVmName(msg.getVmName());
64 // setFromSystem(generateFrom());
65 // setResponse(Status.PENDING);
66 // setPolicyName(msg.getPolicyName());
67 // setPolicyVersion(msg.getPolicyVersion());
68 // setStartTime(msg.getStartTime());
71 private static final long serialVersionUID = -5447940920271469613L;
73 * The status of the response
75 @JsonProperty("Status")
76 private OutStatus status;
81 public OutStatus getStatus() {
86 * @param status the status to set
88 public void setStatus(OutStatus status) {
92 public void updateResponseTime() {
93 SecureRandom rand = new SecureRandom();
94 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss.SSS");
95 df.setTimeZone(TimeZone.getTimeZone("UTC"));
96 String date = df.format(new Date(Time.utcTime()));
97 //this.responseTime = String.format("%s%03d", date, rand.nextInt(1000));
100 public String generateFrom() {
103 InetAddress iAddress = getLocalHost();
104 name = iAddress.getCanonicalHostName();
105 } catch (Exception e) {
106 // Could not get anything from the InetAddress
107 name = "UnknownHost";
109 return "appc@" + name;
112 public JSONObject toResponse() {
113 updateResponseTime();
114 JSONObject json = Mapper.toJsonObject(this);
116 if (!json.has("message")) {
117 // If there is no message, parrot the status (response field)
118 // TODO - Can this be removed for 1602 making message truely optional?
119 //json.put("message", this.getResponse().toString());
122 // Removed duplication of status from message for 1602
123 // json.put("message", String.format("%s: %s", request, json.get("message")));
129 // public String toString() {
130 // return String.format("%s - %s", getId(), getResponse());
133 public static class OutStatus{
134 @JsonProperty("Code")
137 @JsonProperty("Value")
138 private String value;
143 public String getCode() {
148 * @param code the code to set
150 public void setCode(String code) {
157 public String getValue() {
162 * @param value the value to set
164 public void setValue(String value) {
170 public void setResponse(Status newStatus) {
171 if(this.status == null){
172 this.status = new OutStatus();
174 if(newStatus == null) {
180 this.status.setValue(newStatus.getValue());
181 this.status.setCode("100");
185 this.status.setValue(newStatus.getValue());
186 this.status.setCode("500");
190 this.status.setValue(newStatus.getValue());
191 this.status.setCode("400");
199 protected InetAddress getLocalHost() throws UnknownHostException {
200 return InetAddress.getLocalHost();