f436d379efd234a207fc7ce6bbed3cb3347c237f
[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  * ONAP : APPC
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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.openecomp.appc.listener.demo.model;
26
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;
32
33 import org.json.JSONObject;
34 import org.openecomp.appc.listener.util.Mapper;
35 import org.openecomp.appc.util.Time;
36
37 import com.fasterxml.jackson.annotation.JsonIgnore;
38 import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
39 import com.fasterxml.jackson.annotation.JsonProperty;
40 import com.fasterxml.jackson.databind.annotation.JsonSerialize;
41 import com.fasterxml.jackson.databind.annotation.JsonSerialize.Inclusion;
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 @JsonSerialize(include = Inclusion.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 = InetAddress.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                 
174                 switch (newStatus){
175                 case ACCEPTED:
176                         this.status.setValue(newStatus.getValue());
177                         this.status.setCode("100");
178                         break;
179
180                 case FAILURE:
181                         this.status.setValue(newStatus.getValue());
182                         this.status.setCode("500");
183                         break;
184
185                 case SUCCESS:
186                         this.status.setValue(newStatus.getValue());
187                         this.status.setCode("400");
188                         break;
189                 default:
190                         break;
191                         
192                 }
193                 
194         }
195 }