2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.listener.demo.model;
27 import java.net.InetAddress;
28 import java.security.SecureRandom;
29 import java.text.SimpleDateFormat;
30 import java.util.Date;
31 import java.util.TimeZone;
33 import org.json.JSONObject;
34 import org.onap.appc.listener.util.Mapper;
35 import org.onap.appc.util.Time;
37 import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
38 import com.fasterxml.jackson.annotation.JsonProperty;
39 import com.fasterxml.jackson.databind.annotation.JsonSerialize;
40 import com.fasterxml.jackson.databind.annotation.JsonSerialize.Inclusion;
43 * This class represents a message being sent out to DMaaP by APPC to update listeners on the status of a request
46 @JsonSerialize(include = Inclusion.NON_NULL)
47 @JsonIgnoreProperties(ignoreUnknown = true)
48 public class OutgoingMessage extends CommonMessage {
50 public OutgoingMessage() {
54 public OutgoingMessage(IncomingMessage msg) {
55 setHeader(msg.getHeader());
56 setPayload(msg.getPayload());
57 // setId(msg.getId());
58 // setOriginalRequest(msg.getRequest());
59 // setRequestClient(msg.getRequestClient());
60 // setRequestTime(msg.getRequestTime());
61 // setVmName(msg.getVmName());
62 // setFromSystem(generateFrom());
63 // setResponse(Status.PENDING);
64 // setPolicyName(msg.getPolicyName());
65 // setPolicyVersion(msg.getPolicyVersion());
66 // setStartTime(msg.getStartTime());
69 private static final long serialVersionUID = -5447940920271469613L;
71 * The status of the response
73 @JsonProperty("Status")
74 private OutStatus status;
79 public OutStatus getStatus() {
84 * @param status the status to set
86 public void setStatus(OutStatus status) {
90 public void updateResponseTime() {
91 SecureRandom rand = new SecureRandom();
92 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss.SSS");
93 df.setTimeZone(TimeZone.getTimeZone("UTC"));
94 String date = df.format(new Date(Time.utcTime()));
95 //this.responseTime = String.format("%s%03d", date, rand.nextInt(1000));
98 public String generateFrom() {
101 InetAddress iAddress = InetAddress.getLocalHost();
102 name = iAddress.getCanonicalHostName();
103 } catch (Exception e) {
104 // Could not get anything from the InetAddress
105 name = "UnknownHost";
107 return "appc@" + name;
110 public JSONObject toResponse() {
111 updateResponseTime();
112 JSONObject json = Mapper.toJsonObject(this);
114 if (!json.has("message")) {
115 // If there is no message, parrot the status (response field)
116 // TODO - Can this be removed for 1602 making message truely optional?
117 //json.put("message", this.getResponse().toString());
120 // Removed duplication of status from message for 1602
121 // json.put("message", String.format("%s: %s", request, json.get("message")));
127 // public String toString() {
128 // return String.format("%s - %s", getId(), getResponse());
131 public static class OutStatus{
132 @JsonProperty("Code")
135 @JsonProperty("Value")
136 private String value;
141 public String getCode() {
146 * @param code the code to set
148 public void setCode(String code) {
155 public String getValue() {
160 * @param value the value to set
162 public void setValue(String value) {
168 public void setResponse(Status newStatus) {
169 if(this.status == null){
170 this.status = new OutStatus();
175 this.status.setValue(newStatus.getValue());
176 this.status.setCode("100");
180 this.status.setValue(newStatus.getValue());
181 this.status.setCode("500");
185 this.status.setValue(newStatus.getValue());
186 this.status.setCode("400");