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