c993746465999cba467770470d5cef7dd5076257
[aai/babel.git] / src / main / java / org / onap / aai / babel / service / InfoService.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 European Software Marketing Ltd.
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 package org.onap.aai.babel.service;
22
23 import java.time.Clock;
24 import java.time.LocalDateTime;
25 import java.time.LocalTime;
26 import java.time.format.DateTimeFormatter;
27 import java.time.temporal.ChronoUnit;
28 import java.time.temporal.Temporal;
29 import javax.ws.rs.DefaultValue;
30 import javax.ws.rs.GET;
31 import javax.ws.rs.Path;
32 import javax.ws.rs.Produces;
33 import javax.ws.rs.QueryParam;
34
35 /**
36  * Information service for the micro-service. Return status details to the caller.
37  *
38  * @exclude
39  */
40 @Path("/core/core-service")
41 public class InfoService {
42
43     private Clock clock = Clock.systemDefaultZone();
44     private LocalDateTime startTime = LocalDateTime.now(clock);
45     private long infoCount = 0L;
46
47     /**
48      * @param format is an optional setting - html requests an HTML format
49      * @return a formatted status report
50      */
51     @GET
52     @Path("/info")
53     @Produces("text/plain")
54     public String getInfo(@DefaultValue("text") @QueryParam("format") String format) {
55         return "Status: Up\n" + statusReport(clock) + "\n";
56     }
57
58     /** @return a status report showing the up time for the service */
59     public String statusReport(Clock clock) {
60         Temporal reportTime = LocalDateTime.now(clock);
61         long upTime = ChronoUnit.SECONDS.between(startTime, reportTime);
62         long upTimeDays = ChronoUnit.DAYS.between(startTime, reportTime);
63
64         StringBuilder sb = new StringBuilder("Started at ");
65         sb.append(startTime).append('\n').append("Up time ");
66         if (upTimeDays > 0) {
67             sb.append(upTimeDays).append(" day");
68             if (upTimeDays > 1) {
69                 sb.append("s");
70             }
71             sb.append(" ");
72         }
73         sb.append(LocalTime.MIDNIGHT.plusSeconds(upTime).format(DateTimeFormatter.ofPattern("HH:mm:ss"))).append('\n');
74
75         sb.append('\n').append("Info Service").append('\n');
76         sb.append("total=").append(++infoCount).append('\n');
77
78         return sb.toString();
79     }
80 }