2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.vid.controller.open;
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;
35 import javax.inject.Inject;
36 import javax.servlet.ServletContext;
37 import java.io.IOException;
39 import java.util.HashMap;
41 import java.util.regex.Matcher;
42 import java.util.regex.Pattern;
44 import static org.apache.commons.lang3.StringUtils.substringAfterLast;
47 @RequestMapping("version")
48 public class VersionController extends UnRestrictedBaseController {
50 private final ServletContext servletContext;
53 public VersionController(ServletContext servletContext) {
54 this.servletContext = servletContext;
57 @RequestMapping(method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
58 public Map<String,String> getVersionAndFeatures()
60 String features = SystemProperties.getProperty("features.set.filename");
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";
71 String majorVersion = getDisplayVersion(features, version);
72 return ImmutableMap.of("Features", features, "Build", version, "DisplayVersion", majorVersion);
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;
79 final String buildByVersion = StringUtils.defaultIfBlank(substringAfterLast(build, "."), build);
81 return StringUtils.join(majorByFeatures, ".", buildByVersion);