Skip to content
Snippets Groups Projects
Commit 76cbc830 authored by Ryan Izard's avatar Ryan Izard
Browse files

Remove old makefile. Add round #1 of version support via REST API. The...

Remove old makefile. Add round #1 of version support via REST API. The included code adds a /wm/core/version/json REST API. This will work in Eclipse and in situations where the jar is being run in the build environment with the pom.xml. It will not work when the jar has been compiled and relocated outside of the build environment. For this, we need to come up with a solution to build in the version at compile-time. This will either incorporate it as code or save it somewhere in the classpath.
parent ef7ca2e8
No related branches found
No related tags found
No related merge requests found
# Because I am old and crotchety and my fingers can't stop from running
# `make` commands
.PHONY: docs doc all test tests count install clean
all:
ant
init:
ant init
docs:
ant javadoc
doc:
ant javadoc
javadoc:
ant javadoc
check: tests
test: tests
tests: all unit-tests
unit-tests:
ant tests
regression-tests:
make -C regress tests
count:
@find src -name \*.java | xargs wc -l | sort -n
clean:
ant clean
package net.floodlightcontroller.core.util;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
public class ControllerVersion {
private static final Logger log = LoggerFactory.getLogger(ControllerVersion.class);
public synchronized static final String getFloodlightVersion() {
/* First, check our manifest to see if it's been set */
String version = null;
String title = null;
Package pkg = ControllerVersion.class.getPackage();
if (pkg != null) {
version = pkg.getImplementationVersion();
if (version == null) {
version = pkg.getSpecificationVersion();
}
if (version != null) {
version = version.trim();
if (!version.isEmpty()) {
log.info("Floodlight version {} found in manifest", version);
return version;
}
}
title = pkg.getImplementationTitle();
if (title == null) {
title = pkg.getSpecificationTitle();
}
}
/* Try to get version number from pom.xml (available in Eclipse) */
try {
String className = ControllerVersion.class.getName();
String classfileName = "/" + className.replace('.', '/') + ".class";
URL classfileResource = ControllerVersion.class.getResource(classfileName);
if (classfileResource != null) {
Path absolutePackagePath = Paths.get(classfileResource.toURI())
.getParent();
int packagePathSegments = className.length()
- className.replace(".", "").length();
/*
* Remove package segments from path, plus two more levels
* for "target/classes", which is the standard location for
* classes in Eclipse.
*/
Path path = absolutePackagePath;
for (int i = 0, segmentsToRemove = packagePathSegments + 2;
i < segmentsToRemove; i++) {
path = path.getParent();
}
Path pom = path.resolve("pom.xml");
try (InputStream is = Files.newInputStream(pom)) {
Document doc = DocumentBuilderFactory.newInstance()
.newDocumentBuilder().parse(is);
doc.getDocumentElement().normalize();
version = (String) XPathFactory.newInstance()
.newXPath().compile("/project/version")
.evaluate(doc, XPathConstants.STRING);
if (version != null) {
version = version.trim();
if (version.isEmpty()) {
return "unknown";
} else {
log.info("Floodlight version {} found in pom.xml", version);
return version;
}
}
}
}
} catch (Exception e) { }
return "unknown";
}
public synchronized static final String getFloodlightName() {
/* First, check our manifest to see if it's been set */
String title = null;
Package pkg = ControllerVersion.class.getPackage();
if (pkg != null) {
title = pkg.getImplementationTitle();
if (title == null) {
title = pkg.getSpecificationTitle();
}
if (title != null) {
title = title.trim();
if (!title.isEmpty()) {
log.info("Floodlight name {} found in manifest", title);
return title;
}
}
}
/* Try to get version number from pom.xml (available in Eclipse) */
try {
String className = ControllerVersion.class.getName();
String classfileName = "/" + className.replace('.', '/') + ".class";
URL classfileResource = ControllerVersion.class.getResource(classfileName);
if (classfileResource != null) {
Path absolutePackagePath = Paths.get(classfileResource.toURI())
.getParent();
int packagePathSegments = className.length()
- className.replace(".", "").length();
/*
* Remove package segments from path, plus two more levels
* for "target/classes", which is the standard location for
* classes in Eclipse.
*/
Path path = absolutePackagePath;
for (int i = 0, segmentsToRemove = packagePathSegments + 2;
i < segmentsToRemove; i++) {
path = path.getParent();
}
Path pom = path.resolve("pom.xml");
try (InputStream is = Files.newInputStream(pom)) {
Document doc = DocumentBuilderFactory.newInstance()
.newDocumentBuilder().parse(is);
doc.getDocumentElement().normalize();
title = (String) XPathFactory.newInstance()
.newXPath().compile("/project/name")
.evaluate(doc, XPathConstants.STRING);
if (title != null) {
title = title.trim();
if (title.isEmpty()) {
return "unknown";
} else {
log.info("Floodlight name {} found in manifest", title);
return title;
}
}
}
}
} catch (Exception e) { }
return "unknown";
}
}
/**
* Copyright 2011, Big Switch Networks, Inc.
* Originally created by David Erickson, Stanford University
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
**/
package net.floodlightcontroller.core.web;
import java.util.HashMap;
import java.util.Map;
import org.restlet.resource.Get;
import org.restlet.resource.ServerResource;
import net.floodlightcontroller.core.util.ControllerVersion;
/**
* Retrieve Floodlight version
* @author rizard
*/
public class ControllerVersionResource extends ServerResource {
@Get("json")
public Map<String, Object> retrieve() {
HashMap<String, Object> model = new HashMap<String, Object>();
model.put("version", ControllerVersion.getFloodlightVersion());
model.put("name", ControllerVersion.getFloodlightName());
return model;
}
}
......@@ -61,6 +61,7 @@ public class CoreWebRoutable implements RestletRoutable {
router.attach("/role/json", ControllerRoleResource.class);
router.attach("/health/json", HealthCheckResource.class);
router.attach("/system/uptime/json", SystemUptimeResource.class);
router.attach("/version/json", ControllerVersionResource.class);
return router;
}
}
package net.floodlightcontroller.core.util;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class VersionTest {
private static final Logger log = LoggerFactory.getLogger(VersionTest.class);
@Test
public void testVersion() {
String version = ControllerVersion.getFloodlightVersion();
if (version.equals("unknown")) {
assertTrue("Version should have been detected", false);
}
log.info("Found Floodlight version {}", version);
}
@Test
public void testName() {
String name = ControllerVersion.getFloodlightName();
if (name.equals("unknown")) {
assertTrue("Name should have been detected", false);
}
log.info("Found Floodlight name {}", name);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment