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