2d90f95c7ce135103913b493c8996d201620a6ad
[appc.git] /
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.provider;
24
25 import org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160104.TIMESTAMP;
26 import org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160104.common.response.header.CommonResponseHeader;
27 import org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160104.common.response.header.CommonResponseHeaderBuilder;
28 import org.openecomp.appc.util.Time;
29
30 import java.text.DateFormat;
31 import java.text.SimpleDateFormat;
32
33
34 /**
35  * Builds the responses from the APP-C services according to the YANG domainmodel
36  *
37  * @since Nov 16, 2015
38  * @version $Id$
39  */
40 public class ResponseHeaderBuilder {
41
42     /**
43      * The date/time formatter to format timestamps.
44      */
45     @SuppressWarnings("nls")
46     public static final DateFormat FORMATTER = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS");
47     public static final DateFormat ZULU_FORMATTER = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SS'Z'");
48
49     /**
50      * Private default constructor prevents instantiation
51      */
52     private ResponseHeaderBuilder() {
53     }
54
55     /**
56      * This method builds the common response header and returns it to the caller for integration into the response
57      *
58      * @param success
59      *            True or false indicating the outcome of the operation. True indicates that the operation was
60      *            successful, false indicates it failed.
61      * @param requestId
62      *            The original request id for the service
63      * @param reason
64      *            The reason for the failure if the success flag is false. If success is true, the reason is not used.
65      * @param duration
66      *            The duration of the request processing
67      * @return The common response header to be returned to the caller.
68      */
69     @SuppressWarnings("nls")
70     public static CommonResponseHeader buildHeader(Boolean success, String requestId, String reason, long duration) {
71         CommonResponseHeaderBuilder builder = new CommonResponseHeaderBuilder();
72
73         TIMESTAMP timestamp = new TIMESTAMP(FORMATTER.format(Time.utcDate()));
74         builder.setServiceRequestId(requestId);
75         builder.setCompleted(timestamp);
76         builder.setDuration(duration);
77         builder.setSuccess(success);
78         
79         if (success.equals(Boolean.TRUE)) {
80             builder.setReason("Success");
81         } else {
82             builder.setReason(reason);
83         }
84
85         return builder.build();
86     }
87
88
89 }