2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
 
   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
 
  12  *      http://www.apache.org/licenses/LICENSE-2.0
 
  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=========================================================
 
  22 package org.openecomp.appc.listener.CL1607.model;
 
  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;
 
  30 import org.json.JSONObject;
 
  31 import org.openecomp.appc.listener.util.Mapper;
 
  32 import org.openecomp.appc.util.Time;
 
  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;
 
  41  * This class represents a message being sent out to DMaaP by APPC to update listeners on the status of a request
 
  44 @JsonSerialize(include = Inclusion.NON_NULL)
 
  45 @JsonIgnoreProperties(ignoreUnknown = true)
 
  46 public class OutgoingMessage extends CommonMessage {
 
  48     public OutgoingMessage() {
 
  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());
 
  67     private static final long serialVersionUID = -5447940920271469613L;
 
  69      * The status of the response
 
  71     @JsonProperty("Status")
 
  72     private OutStatus status;
 
  77         public OutStatus getStatus() {
 
  82          * @param status the status to set
 
  84         public void setStatus(OutStatus status) {
 
  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));
 
  96     public String generateFrom() {
 
  99             InetAddress iAddress = InetAddress.getLocalHost();
 
 100             name = iAddress.getCanonicalHostName();
 
 101         } catch (Exception e) {
 
 102             // Could not get anything from the InetAddress
 
 103             name = "UnknownHost";
 
 105         return "appc@" + name;
 
 108     public JSONObject toResponse() {
 
 109         updateResponseTime();
 
 110         JSONObject json = Mapper.toJsonObject(this);
 
 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());
 
 118         // Removed duplication of status from message for 1602
 
 119         // json.put("message", String.format("%s: %s", request, json.get("message")));
 
 125 //    public String toString() {
 
 126 //        return String.format("%s - %s", getId(), getResponse());
 
 129     public static class OutStatus{
 
 130         @JsonProperty("Code")
 
 133         @JsonProperty("Value")
 
 134         private String value;
 
 139                 public String getCode() {
 
 144                  * @param code the code to set
 
 146                 public void setCode(String code) {
 
 153                 public String getValue() {
 
 158                  * @param value the value to set
 
 160                 public void setValue(String value) {
 
 166         public void setResponse(Status newStatus) {
 
 167                 if(this.status == null){
 
 168                         this.status = new OutStatus();
 
 173                         this.status.setValue(newStatus.getValue());
 
 174                         this.status.setCode("100");
 
 178                         this.status.setValue(newStatus.getValue());
 
 179                         this.status.setCode("500");
 
 183                         this.status.setValue(newStatus.getValue());
 
 184                         this.status.setCode("400");