replace deprecated JsonSerialize.Inclusion
[appc.git] / appc-event-listener / appc-event-listener-bundle / src / main / java / org / onap / appc / listener / demo / model / OutgoingMessage.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
14  * 
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  * 
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.
22  * 
23  * ============LICENSE_END=========================================================
24  */
25
26 package org.onap.appc.listener.demo.model;
27
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;
34
35 import org.json.JSONObject;
36 import org.onap.appc.listener.util.Mapper;
37 import org.onap.appc.util.Time;
38
39 import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
40 import com.fasterxml.jackson.annotation.JsonInclude;
41 import com.fasterxml.jackson.annotation.JsonProperty;
42
43 /**
44  * This class represents a message being sent out to DMaaP by APPC to update listeners on the status of a request
45  *
46  */
47 @JsonInclude(JsonInclude.Include.NON_NULL)
48 @JsonIgnoreProperties(ignoreUnknown = true)
49 public class OutgoingMessage extends CommonMessage {
50
51     public OutgoingMessage() {
52
53     }
54
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());
68     }
69
70     private static final long serialVersionUID = -5447940920271469613L;
71     /*
72      * The status of the response
73      */
74     @JsonProperty("Status")
75     private OutStatus status;
76
77     /**
78      * @return the status
79      */
80     public OutStatus getStatus() {
81         return status;
82     }
83
84     /**
85      * @param status the status to set
86      */
87     public void setStatus(OutStatus status) {
88         this.status = status;
89     }
90
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));
97     }
98
99     public String generateFrom() {
100         String name;
101         try {
102             InetAddress iAddress = getLocalHost();
103             name = iAddress.getCanonicalHostName();
104         } catch (Exception e) {
105             // Could not get anything from the InetAddress
106             name = "UnknownHost";
107         }
108         return "appc@" + name;
109     }
110
111     public JSONObject toResponse() {
112         updateResponseTime();
113         JSONObject json = Mapper.toJsonObject(this);
114
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());
119         }
120
121         // Removed duplication of status from message for 1602
122         // json.put("message", String.format("%s: %s", request, json.get("message")));
123
124         return json;
125     }
126
127     //    @Override
128     //    public String toString() {
129     //        return String.format("%s - %s", getId(), getResponse());
130     //    }
131
132     public static class OutStatus{
133         @JsonProperty("Code")
134         private String code;
135
136         @JsonProperty("Value")
137         private String value;
138
139         /**
140          * @return the code
141          */
142         public String getCode() {
143             return code;
144         }
145
146         /**
147          * @param code the code to set
148          */
149         public void setCode(String code) {
150             this.code = code;
151         }
152
153         /**
154          * @return the value
155          */
156         public String getValue() {
157             return value;
158         }
159
160         /**
161          * @param value the value to set
162          */
163         public void setValue(String value) {
164             this.value = value;
165         }
166
167     }
168
169     public void setResponse(Status newStatus) {
170         if(this.status == null){
171             this.status = new OutStatus();
172         }
173         if(newStatus == null) {
174             return;
175         }
176
177         switch (newStatus){
178             case ACCEPTED:
179                 this.status.setValue(newStatus.getValue());
180                 this.status.setCode("100");
181                 break;
182
183             case FAILURE:
184                 this.status.setValue(newStatus.getValue());
185                 this.status.setCode("500");
186                 break;
187
188             case SUCCESS:
189                 this.status.setValue(newStatus.getValue());
190                 this.status.setCode("400");
191                 break;
192             default:
193                 break;
194
195         }
196     }
197
198     protected InetAddress getLocalHost() throws UnknownHostException {
199         return InetAddress.getLocalHost();
200     }
201 }