c8b5c956c77898786da52f653a6361af3eb66066
[appc.git] / appc-event-listener / appc-event-listener-bundle / src / main / java / org / openecomp / appc / listener / demo / model / OutgoingMessage.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright (C) 2017 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22
23 package org.openecomp.appc.listener.demo.model;
24
25 import java.net.InetAddress;
26 import java.security.SecureRandom;
27 import java.text.SimpleDateFormat;
28 import java.util.Date;
29 import java.util.TimeZone;
30
31 import org.json.JSONObject;
32 import org.openecomp.appc.listener.util.Mapper;
33 import org.openecomp.appc.util.Time;
34
35 import com.fasterxml.jackson.annotation.JsonIgnore;
36 import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
37 import com.fasterxml.jackson.annotation.JsonProperty;
38 import com.fasterxml.jackson.databind.annotation.JsonSerialize;
39 import com.fasterxml.jackson.databind.annotation.JsonSerialize.Inclusion;
40
41 /**
42  * This class represents a message being sent out to DMaaP by APPC to update listeners on the status of a request
43  *
44  */
45 @JsonSerialize(include = Inclusion.NON_NULL)
46 @JsonIgnoreProperties(ignoreUnknown = true)
47 public class OutgoingMessage extends CommonMessage {
48
49     public OutgoingMessage() {
50
51     }
52
53     public OutgoingMessage(IncomingMessage msg) {
54         setHeader(msg.getHeader());
55         setPayload(msg.getPayload());
56 //        setId(msg.getId());
57 //        setOriginalRequest(msg.getRequest());
58 //        setRequestClient(msg.getRequestClient());
59 //        setRequestTime(msg.getRequestTime());
60 //        setVmName(msg.getVmName());
61 //        setFromSystem(generateFrom());
62 //        setResponse(Status.PENDING);
63 //        setPolicyName(msg.getPolicyName());
64 //        setPolicyVersion(msg.getPolicyVersion());
65 //        setStartTime(msg.getStartTime());
66     }
67     
68     private static final long serialVersionUID = -5447940920271469613L;
69     /*
70      * The status of the response
71      */
72     @JsonProperty("Status")
73     private OutStatus status;
74
75     /**
76          * @return the status
77          */
78         public OutStatus getStatus() {
79                 return status;
80         }
81
82         /**
83          * @param status the status to set
84          */
85         public void setStatus(OutStatus status) {
86                 this.status = status;
87         }
88
89         public void updateResponseTime() {
90         SecureRandom rand = new SecureRandom();
91         SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss.SSS");
92         df.setTimeZone(TimeZone.getTimeZone("UTC"));
93         String date = df.format(new Date(Time.utcTime()));
94         //this.responseTime = String.format("%s%03d", date, rand.nextInt(1000));
95     }
96
97     public String generateFrom() {
98         String name;
99         try {
100             InetAddress iAddress = InetAddress.getLocalHost();
101             name = iAddress.getCanonicalHostName();
102         } catch (Exception e) {
103             // Could not get anything from the InetAddress
104             name = "UnknownHost";
105         }
106         return "appc@" + name;
107     }
108
109     public JSONObject toResponse() {
110         updateResponseTime();
111         JSONObject json = Mapper.toJsonObject(this);
112
113         if (!json.has("message")) {
114             // If there is no message, parrot the status (response field)
115             // TODO - Can this be removed for 1602 making message truely optional?
116             //json.put("message", this.getResponse().toString());
117         }
118
119         // Removed duplication of status from message for 1602
120         // json.put("message", String.format("%s: %s", request, json.get("message")));
121
122         return json;
123     }
124
125 //    @Override
126 //    public String toString() {
127 //        return String.format("%s - %s", getId(), getResponse());
128 //    }
129     
130     public static class OutStatus{
131         @JsonProperty("Code")
132         private String code;
133         
134         @JsonProperty("Value")
135         private String value;
136
137                 /**
138                  * @return the code
139                  */
140                 public String getCode() {
141                         return code;
142                 }
143
144                 /**
145                  * @param code the code to set
146                  */
147                 public void setCode(String code) {
148                         this.code = code;
149                 }
150
151                 /**
152                  * @return the value
153                  */
154                 public String getValue() {
155                         return value;
156                 }
157
158                 /**
159                  * @param value the value to set
160                  */
161                 public void setValue(String value) {
162                         this.value = value;
163                 }
164         
165     }
166
167         public void setResponse(Status newStatus) {
168                 if(this.status == null){
169                         this.status = new OutStatus();
170                 }
171                 
172                 switch (newStatus){
173                 case ACCEPTED:
174                         this.status.setValue(newStatus.getValue());
175                         this.status.setCode("100");
176                         break;
177
178                 case FAILURE:
179                         this.status.setValue(newStatus.getValue());
180                         this.status.setCode("500");
181                         break;
182
183                 case SUCCESS:
184                         this.status.setValue(newStatus.getValue());
185                         this.status.setCode("400");
186                         break;
187                 default:
188                         break;
189                         
190                 }
191                 
192         }
193 }