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