0a4d6f5ac7040ec26c6bd0f44b7b3f726810284a
[vid.git] / vid-app-common / src / main / java / org / onap / vid / controller / open / VersionController.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.vid.controller.open;
22
23
24 import com.fasterxml.jackson.core.type.TypeReference;
25 import com.fasterxml.jackson.databind.ObjectMapper;
26 import com.google.common.collect.ImmutableMap;
27 import org.apache.commons.lang3.StringUtils;
28 import org.onap.portalsdk.core.controller.UnRestrictedBaseController;
29 import org.onap.portalsdk.core.util.SystemProperties;
30 import org.springframework.http.MediaType;
31 import org.springframework.web.bind.annotation.RequestMapping;
32 import org.springframework.web.bind.annotation.RequestMethod;
33 import org.springframework.web.bind.annotation.RestController;
34
35 import javax.inject.Inject;
36 import javax.servlet.ServletContext;
37 import java.io.IOException;
38 import java.net.URL;
39 import java.util.HashMap;
40 import java.util.Map;
41 import java.util.regex.Matcher;
42 import java.util.regex.Pattern;
43
44 import static org.apache.commons.lang3.StringUtils.substringAfterLast;
45
46 @RestController
47 @RequestMapping("version")
48 public class VersionController extends UnRestrictedBaseController {
49
50     private final ServletContext servletContext;
51
52     @Inject
53     public VersionController(ServletContext servletContext) {
54         this.servletContext = servletContext;
55     }
56
57     @RequestMapping(method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
58     public Map<String,String> getVersionAndFeatures()
59     {
60         String features = SystemProperties.getProperty("features.set.filename");
61         String version;
62         try {
63             final URL resource = servletContext.getResource("/app/vid/scripts/constants/version.json");
64             HashMap<String,String> versionFile = new HashMap <>();
65             ObjectMapper mapper = new ObjectMapper();
66             versionFile.putAll(mapper.readValue(resource, new TypeReference<HashMap<String, String>>() {}));
67             version = versionFile.get("Version");
68         } catch (IOException e) {
69             version = "Error retrieving build number";
70         }
71         String majorVersion = getDisplayVersion(features, version);
72         return ImmutableMap.of("Features", features, "Build", version, "DisplayVersion", majorVersion);
73     }
74
75     String getDisplayVersion(String features, String build) {
76         Matcher matcher = Pattern.compile("([^/]+?)(\\.features|$)").matcher(features);
77         final String majorByFeatures = matcher.find() ? matcher.group(1) : features;
78
79         final String buildByVersion = StringUtils.defaultIfBlank(substringAfterLast(build, "."), build);
80
81         return StringUtils.join(majorByFeatures, ".", buildByVersion);
82     }
83 }