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