Jian Li
Committed by Gerrit Code Review

Refactor SystemInfo, and store SystemInfo into singleton instance

- Implement Bill Pugh singleton for ControlMetricsFactory and
  SystemInfo class

Change-Id: Ia97538d9f1be9ea900b0e87371bf50877eaf6483
...@@ -13,69 +13,45 @@ ...@@ -13,69 +13,45 @@
13 * See the License for the specific language governing permissions and 13 * See the License for the specific language governing permissions and
14 * limitations under the License. 14 * limitations under the License.
15 */ 15 */
16 -package org.onosproject.cpman.impl; 16 +package org.onosproject.cpman;
17 17
18 /** 18 /**
19 - * Control metrics class for storing system specification. 19 + * System information interface.
20 */ 20 */
21 -public final class ControlMetricsSystemSpec { 21 +public interface SystemInfo {
22 - private int numOfCores;
23 - private int numOfCpus;
24 - private int cpuSpeed; // in MHz
25 - private long totalMemory; // in bytes
26 -
27 - private ControlMetricsSystemSpec(int numOfCores, int numOfCpus,
28 - int cpuSpeed, long totalMemory) {
29 - this.numOfCores = numOfCores;
30 - this.numOfCpus = numOfCpus;
31 - this.cpuSpeed = cpuSpeed;
32 - this.totalMemory = totalMemory;
33 - }
34 22
35 /** 23 /**
36 * Returns number of CPU cores. 24 * Returns number of CPU cores.
37 * 25 *
38 * @return number of CPU cores 26 * @return number of CPU cores
39 */ 27 */
40 - public int numOfCores() { 28 + int coreCount();
41 - return this.numOfCores;
42 - }
43 29
44 /** 30 /**
45 * Returns number of CPUs. 31 * Returns number of CPUs.
46 * 32 *
47 * @return number of CPUs 33 * @return number of CPUs
48 */ 34 */
49 - public int numOfCpus() { 35 + int cpuCount();
50 - return this.numOfCpus;
51 - }
52 36
53 /** 37 /**
54 * Returns CPU speed in MHz. 38 * Returns CPU speed in MHz.
55 * 39 *
56 * @return CPU speed 40 * @return CPU speed
57 */ 41 */
58 - public int cpuSpeed() { 42 + int cpuSpeed();
59 - return this.cpuSpeed;
60 - }
61 43
62 /** 44 /**
63 - * Returns the total amount of memory. 45 + * Returns the total amount of memory in Mega Bytes.
64 * 46 *
65 * @return memory size 47 * @return memory size
66 */ 48 */
67 - public long totalMemory() { 49 + int totalMemory();
68 - return this.totalMemory;
69 - }
70 50
71 /** 51 /**
72 - * ControlMetricsSystemSpec builder class. 52 + * A builder of SystemInfo.
73 */ 53 */
74 - public static final class Builder { 54 + interface Builder {
75 - private int numOfCores;
76 - private int numOfCpus;
77 - private int cpuSpeed; // in MHz
78 - private long totalMemory; // in bytes
79 55
80 /** 56 /**
81 * Sets number of CPU cores. 57 * Sets number of CPU cores.
...@@ -83,50 +59,36 @@ public final class ControlMetricsSystemSpec { ...@@ -83,50 +59,36 @@ public final class ControlMetricsSystemSpec {
83 * @param numOfCores number of CPU cores 59 * @param numOfCores number of CPU cores
84 * @return Builder object 60 * @return Builder object
85 */ 61 */
86 - public Builder numOfCores(int numOfCores) { 62 + Builder numOfCores(int numOfCores);
87 - this.numOfCores = numOfCores;
88 - return this;
89 - }
90 63
91 /** 64 /**
92 * Sets number of CPUs. 65 * Sets number of CPUs.
93 * @param numOfCpus number of CPUs 66 * @param numOfCpus number of CPUs
94 * @return Builder object 67 * @return Builder object
95 */ 68 */
96 - public Builder numOfCpus(int numOfCpus) { 69 + Builder numOfCpus(int numOfCpus);
97 - this.numOfCpus = numOfCpus;
98 - return this;
99 - }
100 70
101 /** 71 /**
102 * Sets CPU speed. 72 * Sets CPU speed.
103 * 73 *
104 - * @param cpuSpeed CPU speed 74 + * @param cpuSpeedMhz CPU speed in Mhz
105 * @return Builder object 75 * @return Builder object
106 */ 76 */
107 - public Builder cpuSpeed(int cpuSpeed) { 77 + Builder cpuSpeed(int cpuSpeedMhz);
108 - this.cpuSpeed = cpuSpeed;
109 - return this;
110 - }
111 78
112 /** 79 /**
113 * Sets total amount of memory. 80 * Sets total amount of memory.
114 * 81 *
115 - * @param totalMemory memory size 82 + * @param totalMemoryMbytes memory size in Mega Bytes
116 * @return Builder object 83 * @return Builder object
117 */ 84 */
118 - public Builder totalMemory(long totalMemory) { 85 + Builder totalMemory(int totalMemoryMbytes);
119 - this.totalMemory = totalMemory;
120 - return this;
121 - }
122 86
123 /** 87 /**
124 - * Builds a ControlMetricsSystemSpec object. 88 + * Builds a SystemInfo object.
125 * 89 *
126 - * @return ControlMetricsSystemSpec object 90 + * @return SystemInfo object
127 */ 91 */
128 - public ControlMetricsSystemSpec build() { 92 + SystemInfo build();
129 - return new ControlMetricsSystemSpec(numOfCores, numOfCpus, cpuSpeed, totalMemory);
130 - }
131 } 93 }
132 } 94 }
......
...@@ -32,13 +32,9 @@ import java.util.concurrent.ConcurrentHashMap; ...@@ -32,13 +32,9 @@ import java.util.concurrent.ConcurrentHashMap;
32 * Singleton class to provide various control plane metrics to other components. 32 * Singleton class to provide various control plane metrics to other components.
33 */ 33 */
34 public final class ControlMetricsFactory { 34 public final class ControlMetricsFactory {
35 - private static volatile ControlMetricsFactory uniqueInstance;
36 -
37 private MetricsService metricsService; 35 private MetricsService metricsService;
38 private boolean enableMonitor = false; 36 private boolean enableMonitor = false;
39 - 37 + private Boolean isInitialized = false;
40 - // define a ControlMetricsSystemSpec
41 - private ControlMetricsSystemSpec cmss;
42 38
43 // define a set of MetricsAggregators 39 // define a set of MetricsAggregators
44 private MetricsAggregator cpuLoad; 40 private MetricsAggregator cpuLoad;
...@@ -68,48 +64,23 @@ public final class ControlMetricsFactory { ...@@ -68,48 +64,23 @@ public final class ControlMetricsFactory {
68 private Set<String> nwInterfaces = Sets.newConcurrentHashSet(); 64 private Set<String> nwInterfaces = Sets.newConcurrentHashSet();
69 65
70 /** 66 /**
71 - * Constructs a control metrics factory using the given metrics and device services. 67 + * Initializes the control metrics factory instance using the given
68 + * metric service and device service. Makes sure that we only initialize
69 + * control metrics factory instance once.
72 * 70 *
73 - * @param metricsService metric service reference 71 + * @param metricsService metric service
74 - * @param deviceService device service reference 72 + * @param deviceService device service
75 */ 73 */
76 - private ControlMetricsFactory(MetricsService metricsService, DeviceService deviceService) { 74 + public void initialization(MetricsService metricsService, DeviceService deviceService) {
75 + synchronized (isInitialized) {
76 + if (!isInitialized) {
77 this.metricsService = metricsService; 77 this.metricsService = metricsService;
78 registerMetrics(); 78 registerMetrics();
79 -
80 deviceService.getDevices().forEach(d->deviceIds.add(d.id())); 79 deviceService.getDevices().forEach(d->deviceIds.add(d.id()));
81 -
82 addAllControlMessageMetrics(deviceIds); 80 addAllControlMessageMetrics(deviceIds);
83 - } 81 + isInitialized = true;
84 -
85 - /**
86 - * Obtains the unique instance of ControlMetricsFactory.
87 - *
88 - * @param metricsService metric service
89 - * @param deviceService device service
90 - * @return instance of ControlMetricsFactory
91 - */
92 - public static ControlMetricsFactory getInstance(MetricsService metricsService,
93 - DeviceService deviceService) {
94 - if (uniqueInstance == null) {
95 - synchronized (ControlMetricsFactory.class) {
96 - if (uniqueInstance == null) {
97 - uniqueInstance = new ControlMetricsFactory(metricsService, deviceService);
98 - }
99 } 82 }
100 } 83 }
101 - return uniqueInstance;
102 - }
103 -
104 - /**
105 - * Sets system specification.
106 - *
107 - * @param cmss ControlMetricsSystemSpec object
108 - */
109 - public void setSystemSpec(ControlMetricsSystemSpec cmss) {
110 - if (this.cmss == null) {
111 - this.cmss = cmss;
112 - }
113 } 84 }
114 85
115 /** 86 /**
...@@ -579,4 +550,17 @@ public final class ControlMetricsFactory { ...@@ -579,4 +550,17 @@ public final class ControlMetricsFactory {
579 public MetricsAggregator replyPacket(DeviceId deviceId) { 550 public MetricsAggregator replyPacket(DeviceId deviceId) {
580 return replyPacket.get(deviceId); 551 return replyPacket.get(deviceId);
581 } 552 }
553 +
554 + /**
555 + * Returns an instance of control metrics factory.
556 + *
557 + * @return instance of control metrics factory
558 + */
559 + public static ControlMetricsFactory getInstance() {
560 + return SingletonHelper.INSTANCE;
561 + }
562 +
563 + private static class SingletonHelper {
564 + private static final ControlMetricsFactory INSTANCE = new ControlMetricsFactory();
565 + }
582 } 566 }
...\ No newline at end of file ...\ No newline at end of file
......
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.cpman.impl;
17 +
18 +import org.onosproject.cpman.SystemInfo;
19 +
20 +/**
21 + * Implementation class of storing system specification.
22 + */
23 +public final class DefaultSystemInfo implements SystemInfo {
24 + private int numOfCores;
25 + private int numOfCpus;
26 + private int cpuSpeedMhz;
27 + private int totalMemoryMbytes;
28 +
29 + private DefaultSystemInfo(int numOfCores, int numOfCpus,
30 + int cpuSpeedMhz, int totalMemoryMbytes) {
31 + this.numOfCores = numOfCores;
32 + this.numOfCpus = numOfCpus;
33 + this.cpuSpeedMhz = cpuSpeedMhz;
34 + this.totalMemoryMbytes = totalMemoryMbytes;
35 + }
36 +
37 + @Override
38 + public int coreCount() {
39 + return this.numOfCores;
40 + }
41 +
42 + @Override
43 + public int cpuCount() {
44 + return this.numOfCpus;
45 + }
46 +
47 + @Override
48 + public int cpuSpeed() {
49 + return this.cpuSpeedMhz;
50 + }
51 +
52 + @Override
53 + public int totalMemory() {
54 + return this.totalMemoryMbytes;
55 + }
56 +
57 + /**
58 + * ControlMetricsSystemSpec builder class.
59 + */
60 + public static final class Builder implements SystemInfo.Builder {
61 + private int numOfCores;
62 + private int numOfCpus;
63 + private int cpuSpeedMHz;
64 + private int totalMemoryBytes;
65 +
66 + @Override
67 + public SystemInfo.Builder numOfCores(int numOfCores) {
68 + this.numOfCores = numOfCores;
69 + return this;
70 + }
71 +
72 + @Override
73 + public Builder numOfCpus(int numOfCpus) {
74 + this.numOfCpus = numOfCpus;
75 + return this;
76 + }
77 +
78 + @Override
79 + public Builder cpuSpeed(int cpuSpeedMhz) {
80 + this.cpuSpeedMHz = cpuSpeedMhz;
81 + return this;
82 + }
83 +
84 + @Override
85 + public Builder totalMemory(int totalMemoryBytes) {
86 + this.totalMemoryBytes = totalMemoryBytes;
87 + return this;
88 + }
89 +
90 + @Override
91 + public DefaultSystemInfo build() {
92 + return new DefaultSystemInfo(numOfCores, numOfCpus, cpuSpeedMHz, totalMemoryBytes);
93 + }
94 + }
95 +}
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.cpman.impl;
17 +
18 +import org.onosproject.cpman.SystemInfo;
19 +import org.slf4j.Logger;
20 +
21 +import static org.slf4j.LoggerFactory.getLogger;
22 +
23 +/**
24 + * A factory class which instantiates a system info object.
25 + */
26 +public final class SystemInfoFactory {
27 +
28 + private final Logger log = getLogger(getClass());
29 +
30 + private SystemInfo systemInfo;
31 +
32 + // non-instantiable (except for our Singleton)
33 + private SystemInfoFactory() {
34 + }
35 +
36 + /**
37 + * Returns system information.
38 + *
39 + * @return reference object of system info
40 + */
41 + public SystemInfo getSystemInfo() {
42 + synchronized (systemInfo) {
43 + return this.systemInfo;
44 + }
45 + }
46 +
47 + /**
48 + * Set system information only if it is empty.
49 + *
50 + * @param systemInfo reference object of system info
51 + */
52 + public void setSystemInfo(SystemInfo systemInfo) {
53 + synchronized (systemInfo) {
54 + if (this.systemInfo == null) {
55 + this.systemInfo = systemInfo;
56 + } else {
57 + log.warn("System information has already been set");
58 + }
59 + }
60 + }
61 +
62 + /**
63 + * Returns an instance of system info factory.
64 + *
65 + * @return instance of system info factory
66 + */
67 + public static SystemInfoFactory getInstance() {
68 + return SingletonHelper.INSTANCE;
69 + }
70 +
71 + private static class SingletonHelper {
72 + private static final SystemInfoFactory INSTANCE = new SystemInfoFactory();
73 + }
74 +}
...@@ -22,7 +22,9 @@ import org.onosproject.cpman.ControlMetric; ...@@ -22,7 +22,9 @@ import org.onosproject.cpman.ControlMetric;
22 import org.onosproject.cpman.ControlMetricType; 22 import org.onosproject.cpman.ControlMetricType;
23 import org.onosproject.cpman.ControlPlaneMonitorService; 23 import org.onosproject.cpman.ControlPlaneMonitorService;
24 import org.onosproject.cpman.MetricValue; 24 import org.onosproject.cpman.MetricValue;
25 -import org.onosproject.cpman.impl.ControlMetricsSystemSpec; 25 +import org.onosproject.cpman.SystemInfo;
26 +import org.onosproject.cpman.impl.DefaultSystemInfo;
27 +import org.onosproject.cpman.impl.SystemInfoFactory;
26 import org.onosproject.rest.AbstractWebResource; 28 import org.onosproject.rest.AbstractWebResource;
27 29
28 import javax.ws.rs.Consumes; 30 import javax.ws.rs.Consumes;
...@@ -227,19 +229,19 @@ public class ControlMetricsCollectorWebResource extends AbstractWebResource { ...@@ -227,19 +229,19 @@ public class ControlMetricsCollectorWebResource extends AbstractWebResource {
227 } 229 }
228 230
229 /** 231 /**
230 - * Collects system specifications. 232 + * Collects system information.
231 - * The system specs include the various control metrics 233 + * The system information includes the various control metrics
232 * which do not require aggregation. 234 * which do not require aggregation.
233 * 235 *
234 * @param stream JSON stream 236 * @param stream JSON stream
235 * @return 200 OK 237 * @return 200 OK
236 - * @onos.rsModel SystemSpecsPost 238 + * @onos.rsModel SystemInfoPost
237 */ 239 */
238 @POST 240 @POST
239 - @Path("system_specs") 241 + @Path("system_info")
240 @Consumes(MediaType.APPLICATION_JSON) 242 @Consumes(MediaType.APPLICATION_JSON)
241 @Produces(MediaType.APPLICATION_JSON) 243 @Produces(MediaType.APPLICATION_JSON)
242 - public Response systemSpecs(InputStream stream) { 244 + public Response systemInfo(InputStream stream) {
243 ObjectNode root = mapper().createObjectNode(); 245 ObjectNode root = mapper().createObjectNode();
244 246
245 try { 247 try {
...@@ -249,15 +251,17 @@ public class ControlMetricsCollectorWebResource extends AbstractWebResource { ...@@ -249,15 +251,17 @@ public class ControlMetricsCollectorWebResource extends AbstractWebResource {
249 JsonNode cpuSpeed = jsonTree.get("cpuSpeed"); 251 JsonNode cpuSpeed = jsonTree.get("cpuSpeed");
250 JsonNode totalMemory = jsonTree.get("totalMemory"); 252 JsonNode totalMemory = jsonTree.get("totalMemory");
251 253
252 - if (numOfCores != null && numOfCpus != null && cpuSpeed != null && totalMemory != null) { 254 + if (numOfCores != null && numOfCpus != null &&
253 - ControlMetricsSystemSpec.Builder builder = new ControlMetricsSystemSpec.Builder(); 255 + cpuSpeed != null && totalMemory != null) {
254 - ControlMetricsSystemSpec cmss = builder.numOfCores(numOfCores.asInt()) 256 + SystemInfo systemInfo = new DefaultSystemInfo.Builder()
257 + .numOfCores(numOfCores.asInt())
255 .numOfCpus(numOfCpus.asInt()) 258 .numOfCpus(numOfCpus.asInt())
256 .cpuSpeed(cpuSpeed.asInt()) 259 .cpuSpeed(cpuSpeed.asInt())
257 - .totalMemory(totalMemory.asLong()) 260 + .totalMemory(totalMemory.asInt())
258 .build(); 261 .build();
259 - // TODO: need to implement spec store
260 262
263 + // try to store the system info.
264 + SystemInfoFactory.getInstance().setSystemInfo(systemInfo);
261 } else { 265 } else {
262 throw new IllegalArgumentException(INVALID_SYSTEM_SPECS); 266 throw new IllegalArgumentException(INVALID_SYSTEM_SPECS);
263 } 267 }
......
...@@ -27,6 +27,8 @@ import org.onlab.osgi.ServiceDirectory; ...@@ -27,6 +27,8 @@ import org.onlab.osgi.ServiceDirectory;
27 import org.onlab.osgi.TestServiceDirectory; 27 import org.onlab.osgi.TestServiceDirectory;
28 import org.onlab.rest.BaseResource; 28 import org.onlab.rest.BaseResource;
29 import org.onosproject.cpman.ControlPlaneMonitorService; 29 import org.onosproject.cpman.ControlPlaneMonitorService;
30 +import org.onosproject.cpman.SystemInfo;
31 +import org.onosproject.cpman.impl.SystemInfoFactory;
30 import org.onosproject.net.DeviceId; 32 import org.onosproject.net.DeviceId;
31 33
32 import javax.ws.rs.core.MediaType; 34 import javax.ws.rs.core.MediaType;
...@@ -123,8 +125,14 @@ public class ControlMetricsCollectorResourceTest extends JerseyTest { ...@@ -123,8 +125,14 @@ public class ControlMetricsCollectorResourceTest extends JerseyTest {
123 } 125 }
124 126
125 @Test 127 @Test
126 - public void testSystemSpecsPost() { 128 + public void testSystemInfoPost() {
127 - basePostTest("system-spec-post.json", PREFIX + "/system_specs"); 129 + basePostTest("system-info-post.json", PREFIX + "/system_info");
130 +
131 + SystemInfo si = SystemInfoFactory.getInstance().getSystemInfo();
132 + assertThat(si.cpuSpeed(), is(2048));
133 + assertThat(si.coreCount(), is(6));
134 + assertThat(si.cpuCount(), is(2));
135 + assertThat(si.totalMemory(), is(4096));
128 } 136 }
129 137
130 private ClientResponse baseTest(String jsonFile, String path) { 138 private ClientResponse baseTest(String jsonFile, String path) {
......