tom

Added Version and CoreService/Manager as an initial outline of a bootstrap coordinator.

1 package org.onlab.onos.cli; 1 package org.onlab.onos.cli;
2 2
3 import org.apache.karaf.shell.commands.Command; 3 import org.apache.karaf.shell.commands.Command;
4 +import org.onlab.onos.CoreService;
4 import org.onlab.onos.cluster.ClusterService; 5 import org.onlab.onos.cluster.ClusterService;
5 import org.onlab.onos.net.device.DeviceService; 6 import org.onlab.onos.net.device.DeviceService;
6 import org.onlab.onos.net.flow.FlowRuleService; 7 import org.onlab.onos.net.flow.FlowRuleService;
...@@ -21,7 +22,8 @@ public class SummaryCommand extends AbstractShellCommand { ...@@ -21,7 +22,8 @@ public class SummaryCommand extends AbstractShellCommand {
21 protected void execute() { 22 protected void execute() {
22 TopologyService topologyService = get(TopologyService.class); 23 TopologyService topologyService = get(TopologyService.class);
23 Topology topology = topologyService.currentTopology(); 24 Topology topology = topologyService.currentTopology();
24 - print("nodes=%d, devices=%d, links=%d, hosts=%d, clusters=%s, paths=%d, flows=%d, intents=%d", 25 + print("version=%s, nodes=%d, devices=%d, links=%d, hosts=%d, clusters=%s, paths=%d, flows=%d, intents=%d",
26 + get(CoreService.class).version().toString(),
25 get(ClusterService.class).getNodes().size(), 27 get(ClusterService.class).getNodes().size(),
26 get(DeviceService.class).getDeviceCount(), 28 get(DeviceService.class).getDeviceCount(),
27 get(LinkService.class).getLinkCount(), 29 get(LinkService.class).getLinkCount(),
......
1 +package org.onlab.onos;
2 +
3 +/**
4 + * Service for interacting with the core system of the controller.
5 + */
6 +public interface CoreService {
7 +
8 + /**
9 + * Returns the product version.
10 + *
11 + * @return product version
12 + */
13 + Version version();
14 +
15 +}
1 +package org.onlab.onos;
2 +
3 +import java.util.Objects;
4 +
5 +import static java.lang.Integer.parseInt;
6 +
7 +/**
8 + * Representation of the product version.
9 + */
10 +public final class Version {
11 +
12 + public static final String FORMAT = "%d.%d.%d.%s";
13 +
14 + private final int major;
15 + private final int minor;
16 + private final int patch;
17 + private final String build;
18 +
19 + private final String format;
20 +
21 + // Creates a new version descriptor
22 + private Version(int major, int minor, int patch, String build) {
23 + this.major = major;
24 + this.minor = minor;
25 + this.patch = patch;
26 + this.build = build;
27 + this.format = String.format(FORMAT, major, minor, patch, build);
28 + }
29 +
30 +
31 + /**
32 + * Creates a new version from the specified constituent numbers.
33 + *
34 + * @param major major version number
35 + * @param minor minod version number
36 + * @param patch version patch number
37 + * @param build build string
38 + * @return version descriptor
39 + */
40 + public static Version version(int major, int minor, int patch, String build) {
41 + return new Version(major, minor, patch, build);
42 + }
43 +
44 + /**
45 + * Creates a new version by parsing the specified string.
46 + *
47 + * @param string version string
48 + * @return version descriptor
49 + */
50 + public static Version version(String string) {
51 + String[] fields = string.split("[.-]");
52 + return new Version(parseInt(fields[0]), parseInt(fields[1]),
53 + parseInt(fields[2]), fields[3]);
54 + }
55 +
56 + /**
57 + * Returns the major version number.
58 + *
59 + * @return major version number
60 + */
61 + public int major() {
62 + return major;
63 + }
64 +
65 + /**
66 + * Returns the minor version number.
67 + *
68 + * @return minor version number
69 + */
70 + public int minor() {
71 + return minor;
72 + }
73 +
74 + /**
75 + * Returns the version patch number.
76 + *
77 + * @return patch number
78 + */
79 + public int patch() {
80 + return patch;
81 + }
82 +
83 + /**
84 + * Returns the version build string.
85 + *
86 + * @return build string
87 + */
88 + public String build() {
89 + return build;
90 + }
91 +
92 + @Override
93 + public String toString() {
94 + return format;
95 + }
96 +
97 + @Override
98 + public int hashCode() {
99 + return Objects.hash(format);
100 + }
101 +
102 + @Override
103 + public boolean equals(Object obj) {
104 + if (this == obj) {
105 + return true;
106 + }
107 + if (obj instanceof Version) {
108 + final Version other = (Version) obj;
109 + return Objects.equals(this.format, other.format);
110 + }
111 + return false;
112 + }
113 +}
1 +package org.onlab.onos;
2 +
3 +import com.google.common.testing.EqualsTester;
4 +import org.junit.Test;
5 +
6 +import static org.junit.Assert.*;
7 +import static org.onlab.onos.Version.version;
8 +
9 +/**
10 + * Tests of the version descriptor.
11 + */
12 +public class VersionTest {
13 +
14 + @Test
15 + public void fromParts() {
16 + Version v = version(1, 2, 3, "4321");
17 + assertEquals("wrong major", 1, v.major());
18 + assertEquals("wrong minor", 2, v.minor());
19 + assertEquals("wrong patch", 3, v.patch());
20 + assertEquals("wrong build", "4321", v.build());
21 + }
22 +
23 + @Test
24 + public void fromString() {
25 + Version v = version("1.2.3.4321");
26 + assertEquals("wrong major", 1, v.major());
27 + assertEquals("wrong minor", 2, v.minor());
28 + assertEquals("wrong patch", 3, v.patch());
29 + assertEquals("wrong build", "4321", v.build());
30 + }
31 +
32 + @Test
33 + public void snapshot() {
34 + Version v = version("1.2.3-SNAPSHOT");
35 + assertEquals("wrong major", 1, v.major());
36 + assertEquals("wrong minor", 2, v.minor());
37 + assertEquals("wrong patch", 3, v.patch());
38 + assertEquals("wrong build", "SNAPSHOT", v.build());
39 + }
40 +
41 + @Test
42 + public void testEquals() {
43 + new EqualsTester()
44 + .addEqualityGroup(version("1.2.3.4321"), version(1, 2, 3, "4321"))
45 + .addEqualityGroup(version("1.9.3.4321"), version(1, 9, 3, "4321"))
46 + .addEqualityGroup(version("1.2.8.4321"), version(1, 2, 8, "4321"))
47 + .addEqualityGroup(version("1.2.3.x"), version(1, 2, 3, "x"))
48 + .testEquals();
49 + }
50 +}
...\ No newline at end of file ...\ No newline at end of file
1 +package org.onlab.onos.cluster.impl;
2 +
3 +import org.apache.felix.scr.annotations.Activate;
4 +import org.apache.felix.scr.annotations.Component;
5 +import org.apache.felix.scr.annotations.Service;
6 +import org.onlab.onos.CoreService;
7 +import org.onlab.onos.Version;
8 +import org.onlab.util.Tools;
9 +
10 +import java.io.File;
11 +import java.util.List;
12 +
13 +/**
14 + * Core service implementation.
15 + */
16 +@Component
17 +@Service
18 +public class CoreManager implements CoreService {
19 +
20 + private static final File VERSION_FILE = new File("../VERSION");
21 + private static Version version = Version.version("1.0.0-SNAPSHOT");
22 +
23 + // TODO: work in progress
24 +
25 + @Activate
26 + public void activate() {
27 + List<String> versionLines = Tools.slurp(VERSION_FILE);
28 + if (versionLines != null && !versionLines.isEmpty()) {
29 + version = Version.version(versionLines.get(0));
30 + }
31 + }
32 +
33 + @Override
34 + public Version version() {
35 + return version;
36 + }
37 +
38 +}
...@@ -60,6 +60,13 @@ perl -pi.old -e "s|^(featuresBoot=.*)|\1,$ONOS_FEATURES|" \ ...@@ -60,6 +60,13 @@ perl -pi.old -e "s|^(featuresBoot=.*)|\1,$ONOS_FEATURES|" \
60 cp $M2_REPO/org/onlab/onos/onos-branding/$ONOS_VERSION/onos-branding-*.jar \ 60 cp $M2_REPO/org/onlab/onos/onos-branding/$ONOS_VERSION/onos-branding-*.jar \
61 $ONOS_STAGE/$KARAF_DIST/lib 61 $ONOS_STAGE/$KARAF_DIST/lib
62 62
63 +# Patch in the ONOS version file use the build number or the user name for
64 +# build postfix in place of the SNAPSHOT post-fix.
65 +build=${BUILD_NUMBER:-$(id -un)}
66 +grep '<version>' $ONOS_ROOT/pom.xml | head -n1 | \
67 + sed 's:.*<version>::g;s:</version>.*::g' | sed "s/SNAPSHOT/$build/g" \
68 + >> $ONOS_STAGE/VERSION
69 +
63 # Now package up the ONOS tar file 70 # Now package up the ONOS tar file
64 cd $ONOS_STAGE_ROOT 71 cd $ONOS_STAGE_ROOT
65 COPYFILE_DISABLE=1 tar zcf $ONOS_TAR $ONOS_BITS 72 COPYFILE_DISABLE=1 tar zcf $ONOS_TAR $ONOS_BITS
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
4 #------------------------------------------------------------------------------- 4 #-------------------------------------------------------------------------------
5 5
6 export JAVA_HOME=${JAVA_HOME:-/usr/lib/jvm/java-7-openjdk-amd64/} 6 export JAVA_HOME=${JAVA_HOME:-/usr/lib/jvm/java-7-openjdk-amd64/}
7 +export JAVA_OPTS="-Xms256M -Xmx2048M"
7 8
8 cd /opt/onos 9 cd /opt/onos
9 /opt/onos/apache-karaf-3.0.1/bin/karaf "$@" 10 /opt/onos/apache-karaf-3.0.1/bin/karaf "$@"
......
...@@ -4,6 +4,12 @@ import com.google.common.base.Strings; ...@@ -4,6 +4,12 @@ import com.google.common.base.Strings;
4 import com.google.common.primitives.UnsignedLongs; 4 import com.google.common.primitives.UnsignedLongs;
5 import com.google.common.util.concurrent.ThreadFactoryBuilder; 5 import com.google.common.util.concurrent.ThreadFactoryBuilder;
6 6
7 +import java.io.BufferedReader;
8 +import java.io.File;
9 +import java.io.FileReader;
10 +import java.io.IOException;
11 +import java.util.ArrayList;
12 +import java.util.List;
7 import java.util.concurrent.ThreadFactory; 13 import java.util.concurrent.ThreadFactory;
8 14
9 public abstract class Tools { 15 public abstract class Tools {
...@@ -66,4 +72,24 @@ public abstract class Tools { ...@@ -66,4 +72,24 @@ public abstract class Tools {
66 } 72 }
67 } 73 }
68 74
75 + /**
76 + * Slurps the contents of a file into a list of strings, one per line.
77 + *
78 + * @param path file path
79 + * @return file contents
80 + */
81 + public static List<String> slurp(File path) {
82 + try (BufferedReader br = new BufferedReader(new FileReader(path))) {
83 + List<String> lines = new ArrayList<>();
84 + String line;
85 + while ((line = br.readLine()) != null) {
86 + lines.add(line);
87 + }
88 + return lines;
89 +
90 + } catch (IOException e) {
91 + return null;
92 + }
93 + }
94 +
69 } 95 }
......