Jian Li

[ONOS-3662] Add CLI for control plane manager

- Implement CLI for enumerating control plane metrics stats
- Implement ControlResourceTypeCompleter
- Implement DiskResourceNameCompleter
- Implement NetworkResourceNameCompleter
- Implement ControlMessageDeviceIdCompleter
- Extract the control resource type and metrics in a separated
  class for the sake of simplicity.

Change-Id: Ic505191a74bd463091b0e5c75e11f1824bafb816
...@@ -41,57 +41,57 @@ public enum ControlMetricType { ...@@ -41,57 +41,57 @@ public enum ControlMetricType {
41 /* Number of CPU cores. */ 41 /* Number of CPU cores. */
42 NUM_OF_CORES, 42 NUM_OF_CORES,
43 43
44 - /* Number of CPUs. **/ 44 + /* Number of CPUs. */
45 NUM_OF_CPUS, 45 NUM_OF_CPUS,
46 46
47 - /* CPU Speed. **/ 47 + /* CPU Speed. */
48 CPU_SPEED, 48 CPU_SPEED,
49 49
50 - /* CPU Load. **/ 50 + /* CPU Load. */
51 CPU_LOAD, 51 CPU_LOAD,
52 52
53 - /* Total Amount of CPU Up Time. **/ 53 + /* Total Amount of CPU Up Time. */
54 TOTAL_CPU_TIME, 54 TOTAL_CPU_TIME,
55 55
56 - /* System CPU Up Time. **/ 56 + /* System CPU Up Time. */
57 SYS_CPU_TIME, 57 SYS_CPU_TIME,
58 58
59 - /* User CPU Up Time. **/ 59 + /* User CPU Up Time. */
60 USER_CPU_TIME, 60 USER_CPU_TIME,
61 61
62 - /* CPU Idle Time. **/ 62 + /* CPU Idle Time. */
63 CPU_IDLE_TIME, 63 CPU_IDLE_TIME,
64 64
65 /* Ratio of Used Memory Amount. */ 65 /* Ratio of Used Memory Amount. */
66 MEMORY_USED_RATIO, 66 MEMORY_USED_RATIO,
67 67
68 - /* Ratio of Free Memory Amount. **/ 68 + /* Ratio of Free Memory Amount. */
69 MEMORY_FREE_RATIO, 69 MEMORY_FREE_RATIO,
70 70
71 - /* Used Memory Amount. **/ 71 + /* Used Memory Amount. */
72 MEMORY_USED, 72 MEMORY_USED,
73 73
74 - /* Free Memory Amount. **/ 74 + /* Free Memory Amount. */
75 MEMORY_FREE, 75 MEMORY_FREE,
76 76
77 - /* Total Amount of Memory. **/ 77 + /* Total Amount of Memory. */
78 MEMORY_TOTAL, 78 MEMORY_TOTAL,
79 79
80 - /* Disk Read Bytes. **/ 80 + /* Disk Read Bytes. */
81 DISK_READ_BYTES, 81 DISK_READ_BYTES,
82 82
83 - /* Disk Write Bytes. **/ 83 + /* Disk Write Bytes. */
84 DISK_WRITE_BYTES, 84 DISK_WRITE_BYTES,
85 85
86 - /* Network Incoming Bytes. **/ 86 + /* Network Incoming Bytes. */
87 NW_INCOMING_BYTES, 87 NW_INCOMING_BYTES,
88 88
89 - /* Network Outgoing Bytes. **/ 89 + /* Network Outgoing Bytes. */
90 NW_OUTGOING_BYTES, 90 NW_OUTGOING_BYTES,
91 91
92 - /* Network Incoming Packets. **/ 92 + /* Network Incoming Packets. */
93 NW_INCOMING_PACKETS, 93 NW_INCOMING_PACKETS,
94 94
95 - /* Network Outgoing Packets. **/ 95 + /* Network Outgoing Packets. */
96 NW_OUTGOING_PACKETS 96 NW_OUTGOING_PACKETS
97 } 97 }
......
...@@ -19,6 +19,9 @@ import org.onosproject.cluster.NodeId; ...@@ -19,6 +19,9 @@ import org.onosproject.cluster.NodeId;
19 import org.onosproject.net.DeviceId; 19 import org.onosproject.net.DeviceId;
20 20
21 import java.util.Optional; 21 import java.util.Optional;
22 +import java.util.Set;
23 +
24 +import static org.onosproject.cpman.ControlResource.*;
22 25
23 /** 26 /**
24 * Control Plane Statistics Service Interface. 27 * Control Plane Statistics Service Interface.
...@@ -71,4 +74,11 @@ public interface ControlPlaneMonitorService { ...@@ -71,4 +74,11 @@ public interface ControlPlaneMonitorService {
71 * @return control plane load 74 * @return control plane load
72 */ 75 */
73 ControlLoad getLoad(NodeId nodeId, ControlMetricType type, String resourceName); 76 ControlLoad getLoad(NodeId nodeId, ControlMetricType type, String resourceName);
77 +
78 + /**
79 + * Obtains a list of names of available resources.
80 + *
81 + * @return a collection of names of available resources
82 + */
83 + Set<String> availableResources(Type resourceType);
74 } 84 }
...\ 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;
17 +
18 +import com.google.common.collect.ImmutableSet;
19 +
20 +import java.util.Set;
21 +
22 +import static org.onosproject.cpman.ControlMetricType.CPU_IDLE_TIME;
23 +import static org.onosproject.cpman.ControlMetricType.CPU_LOAD;
24 +import static org.onosproject.cpman.ControlMetricType.DISK_READ_BYTES;
25 +import static org.onosproject.cpman.ControlMetricType.DISK_WRITE_BYTES;
26 +import static org.onosproject.cpman.ControlMetricType.FLOW_MOD_PACKET;
27 +import static org.onosproject.cpman.ControlMetricType.FLOW_REMOVED_PACKET;
28 +import static org.onosproject.cpman.ControlMetricType.INBOUND_PACKET;
29 +import static org.onosproject.cpman.ControlMetricType.MEMORY_FREE;
30 +import static org.onosproject.cpman.ControlMetricType.MEMORY_FREE_RATIO;
31 +import static org.onosproject.cpman.ControlMetricType.MEMORY_USED;
32 +import static org.onosproject.cpman.ControlMetricType.MEMORY_USED_RATIO;
33 +import static org.onosproject.cpman.ControlMetricType.NW_INCOMING_BYTES;
34 +import static org.onosproject.cpman.ControlMetricType.NW_INCOMING_PACKETS;
35 +import static org.onosproject.cpman.ControlMetricType.NW_OUTGOING_BYTES;
36 +import static org.onosproject.cpman.ControlMetricType.NW_OUTGOING_PACKETS;
37 +import static org.onosproject.cpman.ControlMetricType.OUTBOUND_PACKET;
38 +import static org.onosproject.cpman.ControlMetricType.REPLY_PACKET;
39 +import static org.onosproject.cpman.ControlMetricType.REQUEST_PACKET;
40 +import static org.onosproject.cpman.ControlMetricType.SYS_CPU_TIME;
41 +import static org.onosproject.cpman.ControlMetricType.TOTAL_CPU_TIME;
42 +import static org.onosproject.cpman.ControlMetricType.USER_CPU_TIME;
43 +
44 +/**
45 + * A set of resource type used in control plane.
46 + */
47 +public final class ControlResource {
48 +
49 + private ControlResource() {}
50 +
51 + /**
52 + * Control resource type.
53 + */
54 + public enum Type {
55 + /* CPU resource */
56 + CPU,
57 +
58 + /* Memory resource */
59 + MEMORY,
60 +
61 + /* Disk resource */
62 + DISK,
63 +
64 + /* Network resource */
65 + NETWORK,
66 +
67 + /* Control message resource */
68 + CONTROL_MESSAGE
69 + }
70 +
71 + /* A collection of CPU related metric types */
72 + public static final Set<ControlMetricType> CPU_METRICS =
73 + ImmutableSet.of(CPU_IDLE_TIME, CPU_LOAD, SYS_CPU_TIME,
74 + USER_CPU_TIME, TOTAL_CPU_TIME);
75 +
76 + /* A collection of memory related metric types */
77 + public static final Set<ControlMetricType> MEMORY_METRICS =
78 + ImmutableSet.of(MEMORY_FREE, MEMORY_FREE_RATIO, MEMORY_USED,
79 + MEMORY_USED_RATIO);
80 +
81 + /* A collection of disk related metric types */
82 + public static final Set<ControlMetricType> DISK_METRICS =
83 + ImmutableSet.of(DISK_READ_BYTES, DISK_WRITE_BYTES);
84 +
85 + /* A collection of network related metric types */
86 + public static final Set<ControlMetricType> NETWORK_METRICS =
87 + ImmutableSet.of(NW_INCOMING_BYTES, NW_OUTGOING_BYTES,
88 + NW_INCOMING_PACKETS, NW_OUTGOING_PACKETS);
89 +
90 + /* A collection of control message related metric types */
91 + public static final Set<ControlMetricType> CONTROL_MESSAGE_METRICS =
92 + ImmutableSet.of(INBOUND_PACKET, OUTBOUND_PACKET, FLOW_MOD_PACKET,
93 + FLOW_REMOVED_PACKET, REQUEST_PACKET, REPLY_PACKET);
94 +}
...@@ -58,13 +58,14 @@ ...@@ -58,13 +58,14 @@
58 <artifactId>onos-cli</artifactId> 58 <artifactId>onos-cli</artifactId>
59 <version>${project.version}</version> 59 <version>${project.version}</version>
60 </dependency> 60 </dependency>
61 -
62 <dependency> 61 <dependency>
63 <groupId>org.apache.karaf.shell</groupId> 62 <groupId>org.apache.karaf.shell</groupId>
64 <artifactId>org.apache.karaf.shell.console</artifactId> 63 <artifactId>org.apache.karaf.shell.console</artifactId>
65 - <scope>compile</scope>
66 </dependency> 64 </dependency>
67 - 65 + <dependency>
66 + <groupId>org.apache.felix</groupId>
67 + <artifactId>org.apache.felix.scr.annotations</artifactId>
68 + </dependency>
68 <dependency> 69 <dependency>
69 <groupId>org.onosproject</groupId> 70 <groupId>org.onosproject</groupId>
70 <artifactId>onlab-junit</artifactId> 71 <artifactId>onlab-junit</artifactId>
...@@ -180,6 +181,7 @@ ...@@ -180,6 +181,7 @@
180 com.fasterxml.jackson.core, 181 com.fasterxml.jackson.core,
181 org.apache.karaf.shell.commands, 182 org.apache.karaf.shell.commands,
182 org.apache.karaf.shell.console, 183 org.apache.karaf.shell.console,
184 + org.apache.karaf.shell.console.completer,
183 com.google.common.*, 185 com.google.common.*,
184 org.onlab.packet.*, 186 org.onlab.packet.*,
185 org.onlab.rest.*, 187 org.onlab.rest.*,
......
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.cli;
17 +
18 +import org.apache.karaf.shell.console.completer.ArgumentCompleter;
19 +import org.apache.karaf.shell.console.completer.StringsCompleter;
20 +import org.onosproject.cli.AbstractCompleter;
21 +import org.onosproject.cli.AbstractShellCommand;
22 +import org.onosproject.cpman.ControlPlaneMonitorService;
23 +
24 +import java.util.List;
25 +import java.util.Set;
26 +import java.util.SortedSet;
27 +
28 +import static org.onosproject.cpman.ControlResource.Type;
29 +
30 +/**
31 + * Device identification completer for control plane manager.
32 + */
33 +public class ControlMessageDeviceIdCompleter extends AbstractCompleter {
34 + @Override
35 + public int complete(String buffer, int cursor, List<String> candidates) {
36 + // delegate string completer
37 + StringsCompleter delegate = new StringsCompleter();
38 +
39 + // Resource type is the second argument.
40 + ArgumentCompleter.ArgumentList list = getArgumentList();
41 + String type = list.getArguments()[1];
42 +
43 + if (Type.CONTROL_MESSAGE.toString().toLowerCase().equals(type)) {
44 + ControlPlaneMonitorService monitorService =
45 + AbstractShellCommand.get(ControlPlaneMonitorService.class);
46 +
47 + Set<String> set = monitorService.availableResources(Type.CONTROL_MESSAGE);
48 +
49 + SortedSet<String> strings = delegate.getStrings();
50 + if (set != null) {
51 + set.forEach(s -> strings.add(s));
52 + }
53 + }
54 + return delegate.complete(buffer, cursor, candidates);
55 + }
56 +}
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.cli;
17 +
18 +import org.apache.karaf.shell.commands.Argument;
19 +import org.apache.karaf.shell.commands.Command;
20 +import org.onosproject.cli.AbstractShellCommand;
21 +import org.onosproject.cluster.ClusterService;
22 +import org.onosproject.cluster.NodeId;
23 +import org.onosproject.cpman.ControlLoad;
24 +import org.onosproject.cpman.ControlMetricType;
25 +import org.onosproject.cpman.ControlPlaneMonitorService;
26 +import org.onosproject.net.DeviceId;
27 +
28 +import java.util.Optional;
29 +import java.util.Set;
30 +
31 +import static org.onosproject.cpman.ControlResource.CONTROL_MESSAGE_METRICS;
32 +import static org.onosproject.cpman.ControlResource.CPU_METRICS;
33 +import static org.onosproject.cpman.ControlResource.DISK_METRICS;
34 +import static org.onosproject.cpman.ControlResource.MEMORY_METRICS;
35 +import static org.onosproject.cpman.ControlResource.NETWORK_METRICS;
36 +
37 +/**
38 + * Lists all stats information of control plane metrics.
39 + */
40 +@Command(scope = "onos", name = "cpman-stats-list",
41 + description = "Lists control metrics statistics")
42 +public class ControlMetricsStatsListCommand extends AbstractShellCommand {
43 +
44 + private static final String FMT = "metricType=%s, latestValue=%d, " +
45 + "averageValue=%d, latestTime=%s";
46 + private static final String INVALID_TYPE = "Invalid control resource type.";
47 +
48 + @Argument(index = 0, name = "type",
49 + description = "Resource type (cpu|memory|disk|network|control_message)",
50 + required = true, multiValued = false)
51 + String type = null;
52 +
53 + @Argument(index = 1, name = "name", description = "Resource name (or Device Id)",
54 + required = false, multiValued = false)
55 + String name = null;
56 +
57 + @Override
58 + protected void execute() {
59 + ControlPlaneMonitorService service = get(ControlPlaneMonitorService.class);
60 + ClusterService clusterService = get(ClusterService.class);
61 + NodeId nodeId = clusterService.getLocalNode().id();
62 + switch (type) {
63 + case "cpu":
64 + printMetricsStats(service, nodeId, CPU_METRICS);
65 + break;
66 + case "memory":
67 + printMetricsStats(service, nodeId, MEMORY_METRICS);
68 + break;
69 + case "disk":
70 + printMetricsStats(service, nodeId, DISK_METRICS, name);
71 + break;
72 + case "network":
73 + printMetricsStats(service, nodeId, NETWORK_METRICS, name);
74 + break;
75 + case "control_message":
76 + if (name != null) {
77 + printMetricsStats(service, nodeId, CONTROL_MESSAGE_METRICS, DeviceId.deviceId(name));
78 + }
79 + break;
80 + default:
81 + print(INVALID_TYPE);
82 + break;
83 + }
84 + }
85 +
86 + private void printMetricsStats(ControlPlaneMonitorService service, NodeId nodeId,
87 + Set<ControlMetricType> typeSet) {
88 + printMetricsStats(service, nodeId, typeSet, null, null);
89 + }
90 +
91 + private void printMetricsStats(ControlPlaneMonitorService service, NodeId nodeId,
92 + Set<ControlMetricType> typeSet, String name) {
93 + printMetricsStats(service, nodeId, typeSet, name, null);
94 + }
95 +
96 + private void printMetricsStats(ControlPlaneMonitorService service, NodeId nodeId,
97 + Set<ControlMetricType> typeSet, DeviceId did) {
98 + printMetricsStats(service, nodeId, typeSet, null, did);
99 + }
100 +
101 + private void printMetricsStats(ControlPlaneMonitorService service, NodeId nodeId,
102 + Set<ControlMetricType> typeSet, String name, DeviceId did) {
103 + if (name == null && did == null) {
104 + typeSet.forEach(s -> print(s, service.getLoad(nodeId, s, Optional.ofNullable(null))));
105 + } else if (name == null && did != null) {
106 + typeSet.forEach(s -> print(s, service.getLoad(nodeId, s, Optional.of(did))));
107 + } else if (name != null && did == null) {
108 + typeSet.forEach(s -> print(s, service.getLoad(nodeId, s, name)));
109 + }
110 + }
111 +
112 + private void print(ControlMetricType type, ControlLoad cl) {
113 + if (cl != null) {
114 + print(FMT, type.toString(), cl.latest(), cl.average(), cl.time());
115 + }
116 + }
117 +}
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.cli;
17 +
18 +import com.google.common.collect.ImmutableList;
19 +import org.onosproject.cli.AbstractChoicesCompleter;
20 +
21 +import java.util.List;
22 +import java.util.stream.Collectors;
23 +
24 +import static org.onosproject.cpman.ControlResource.Type;
25 +
26 +/**
27 + * Control resource type completer.
28 + */
29 +public class ControlResourceTypeCompleter extends AbstractChoicesCompleter {
30 +
31 + private static final List<Type> RESOURCE_TYPES =
32 + ImmutableList.of(Type.CPU, Type.MEMORY, Type.DISK, Type.NETWORK,
33 + Type.CONTROL_MESSAGE);
34 +
35 + @Override
36 + protected List<String> choices() {
37 + return RESOURCE_TYPES.stream().map(type ->
38 + type.toString().toLowerCase()).collect(Collectors.toList());
39 + }
40 +}
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.cli;
17 +
18 +import org.apache.karaf.shell.console.completer.ArgumentCompleter;
19 +import org.apache.karaf.shell.console.completer.StringsCompleter;
20 +import org.onosproject.cli.AbstractCompleter;
21 +import org.onosproject.cli.AbstractShellCommand;
22 +import org.onosproject.cpman.ControlPlaneMonitorService;
23 +
24 +import java.util.List;
25 +import java.util.Set;
26 +import java.util.SortedSet;
27 +
28 +import static org.onosproject.cpman.ControlResource.Type;
29 +/**
30 + * Disk resource name completer.
31 + */
32 +public class DiskResourceNameCompleter extends AbstractCompleter {
33 + @Override
34 + public int complete(String buffer, int cursor, List<String> candidates) {
35 + // delegate string completer
36 + StringsCompleter delegate = new StringsCompleter();
37 +
38 + // Resource type is the second argument.
39 + ArgumentCompleter.ArgumentList list = getArgumentList();
40 + String type = list.getArguments()[1];
41 +
42 + if (Type.DISK.toString().toLowerCase().equals(type)) {
43 + ControlPlaneMonitorService monitorService =
44 + AbstractShellCommand.get(ControlPlaneMonitorService.class);
45 +
46 + Set<String> set = monitorService.availableResources(Type.DISK);
47 + SortedSet<String> strings = delegate.getStrings();
48 +
49 + if (set != null) {
50 + set.forEach(s -> strings.add(s));
51 + }
52 + }
53 + return delegate.complete(buffer, cursor, candidates);
54 + }
55 +}
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.cli;
17 +
18 +import org.apache.karaf.shell.console.completer.ArgumentCompleter;
19 +import org.apache.karaf.shell.console.completer.StringsCompleter;
20 +import org.onosproject.cli.AbstractCompleter;
21 +import org.onosproject.cli.AbstractShellCommand;
22 +import org.onosproject.cpman.ControlPlaneMonitorService;
23 +
24 +import java.util.List;
25 +import java.util.Set;
26 +import java.util.SortedSet;
27 +
28 +import static org.onosproject.cpman.ControlResource.Type;
29 +
30 +/**
31 + * Network resource name completer.
32 + */
33 +public class NetworkResourceNameCompleter extends AbstractCompleter {
34 + @Override
35 + public int complete(String buffer, int cursor, List<String> candidates) {
36 + // delegate string completer
37 + StringsCompleter delegate = new StringsCompleter();
38 +
39 + // Resource type is the second argument.
40 + ArgumentCompleter.ArgumentList list = getArgumentList();
41 + String type = list.getArguments()[1];
42 +
43 + if (Type.NETWORK.toString().toLowerCase().equals(type)) {
44 + ControlPlaneMonitorService monitorService =
45 + AbstractShellCommand.get(ControlPlaneMonitorService.class);
46 +
47 + Set<String> set = monitorService.availableResources(Type.NETWORK);
48 + SortedSet<String> strings = delegate.getStrings();
49 +
50 + if (set != null) {
51 + set.forEach(s -> strings.add(s));
52 + }
53 + }
54 +
55 + return delegate.complete(buffer, cursor, candidates);
56 + }
57 +}
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 +/**
17 + * CLI implementation for control plane manager.
18 + */
19 +package org.onosproject.cpman.cli;
...\ No newline at end of file ...\ No newline at end of file
...@@ -16,6 +16,8 @@ ...@@ -16,6 +16,8 @@
16 package org.onosproject.cpman.impl; 16 package org.onosproject.cpman.impl;
17 17
18 import com.google.common.collect.ImmutableSet; 18 import com.google.common.collect.ImmutableSet;
19 +import com.google.common.collect.Maps;
20 +import com.google.common.collect.Sets;
19 import org.apache.felix.scr.annotations.Activate; 21 import org.apache.felix.scr.annotations.Activate;
20 import org.apache.felix.scr.annotations.Component; 22 import org.apache.felix.scr.annotations.Component;
21 import org.apache.felix.scr.annotations.Deactivate; 23 import org.apache.felix.scr.annotations.Deactivate;
...@@ -37,29 +39,9 @@ import org.slf4j.LoggerFactory; ...@@ -37,29 +39,9 @@ import org.slf4j.LoggerFactory;
37 import java.util.Map; 39 import java.util.Map;
38 import java.util.Optional; 40 import java.util.Optional;
39 import java.util.Set; 41 import java.util.Set;
40 -import java.util.concurrent.ConcurrentHashMap; 42 +import java.util.stream.Collectors;
41 - 43 +
42 -import static org.onosproject.cpman.ControlMetricType.CPU_IDLE_TIME; 44 +import static org.onosproject.cpman.ControlResource.*;
43 -import static org.onosproject.cpman.ControlMetricType.CPU_LOAD;
44 -import static org.onosproject.cpman.ControlMetricType.DISK_READ_BYTES;
45 -import static org.onosproject.cpman.ControlMetricType.DISK_WRITE_BYTES;
46 -import static org.onosproject.cpman.ControlMetricType.FLOW_MOD_PACKET;
47 -import static org.onosproject.cpman.ControlMetricType.FLOW_REMOVED_PACKET;
48 -import static org.onosproject.cpman.ControlMetricType.INBOUND_PACKET;
49 -import static org.onosproject.cpman.ControlMetricType.MEMORY_FREE;
50 -import static org.onosproject.cpman.ControlMetricType.MEMORY_FREE_RATIO;
51 -import static org.onosproject.cpman.ControlMetricType.MEMORY_USED;
52 -import static org.onosproject.cpman.ControlMetricType.MEMORY_USED_RATIO;
53 -import static org.onosproject.cpman.ControlMetricType.NW_INCOMING_BYTES;
54 -import static org.onosproject.cpman.ControlMetricType.NW_INCOMING_PACKETS;
55 -import static org.onosproject.cpman.ControlMetricType.NW_OUTGOING_BYTES;
56 -import static org.onosproject.cpman.ControlMetricType.NW_OUTGOING_PACKETS;
57 -import static org.onosproject.cpman.ControlMetricType.OUTBOUND_PACKET;
58 -import static org.onosproject.cpman.ControlMetricType.REPLY_PACKET;
59 -import static org.onosproject.cpman.ControlMetricType.REQUEST_PACKET;
60 -import static org.onosproject.cpman.ControlMetricType.SYS_CPU_TIME;
61 -import static org.onosproject.cpman.ControlMetricType.TOTAL_CPU_TIME;
62 -import static org.onosproject.cpman.ControlMetricType.USER_CPU_TIME;
63 45
64 /** 46 /**
65 * Control plane monitoring service class. 47 * Control plane monitoring service class.
...@@ -78,45 +60,34 @@ public class ControlPlaneMonitor implements ControlPlaneMonitorService { ...@@ -78,45 +60,34 @@ public class ControlPlaneMonitor implements ControlPlaneMonitorService {
78 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) 60 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
79 protected ClusterService clusterService; 61 protected ClusterService clusterService;
80 62
81 - private static final String CPU = "Cpu"; 63 + private static final Set RESOURCE_TYPE_SET =
82 - private static final String MEMORY = "Memory"; 64 + ImmutableSet.of(Type.CONTROL_MESSAGE, Type.DISK, Type.NETWORK);
83 - private static final String CTRL_MSG = "ControlMessage"; 65 +
84 - private static final String DISK = "Disk";
85 - private static final String NETWORK = "Network";
86 -
87 - private static final Set<ControlMetricType> CPU_METRICS =
88 - ImmutableSet.of(CPU_IDLE_TIME, CPU_LOAD, SYS_CPU_TIME,
89 - USER_CPU_TIME, TOTAL_CPU_TIME);
90 - private static final Set<ControlMetricType> MEMORY_METRICS =
91 - ImmutableSet.of(MEMORY_FREE, MEMORY_FREE_RATIO, MEMORY_USED,
92 - MEMORY_USED_RATIO);
93 - private static final Set<ControlMetricType> DISK_METRICS =
94 - ImmutableSet.of(DISK_READ_BYTES, DISK_WRITE_BYTES);
95 - private static final Set<ControlMetricType> NETWORK_METRICS =
96 - ImmutableSet.of(NW_INCOMING_BYTES, NW_OUTGOING_BYTES,
97 - NW_INCOMING_PACKETS, NW_OUTGOING_PACKETS);
98 - private static final Set<ControlMetricType> CTRL_MSGS =
99 - ImmutableSet.of(INBOUND_PACKET, OUTBOUND_PACKET, FLOW_MOD_PACKET,
100 - FLOW_REMOVED_PACKET, REQUEST_PACKET, REPLY_PACKET);
101 private Map<ControlMetricType, Double> cpuBuf; 66 private Map<ControlMetricType, Double> cpuBuf;
102 private Map<ControlMetricType, Double> memoryBuf; 67 private Map<ControlMetricType, Double> memoryBuf;
103 private Map<String, Map<ControlMetricType, Double>> diskBuf; 68 private Map<String, Map<ControlMetricType, Double>> diskBuf;
104 private Map<String, Map<ControlMetricType, Double>> networkBuf; 69 private Map<String, Map<ControlMetricType, Double>> networkBuf;
105 private Map<DeviceId, Map<ControlMetricType, Double>> ctrlMsgBuf; 70 private Map<DeviceId, Map<ControlMetricType, Double>> ctrlMsgBuf;
106 71
72 + private Map<Type, Set<String>> availableResourceMap;
73 + private Set<DeviceId> availableDeviceIdSet;
74 +
107 @Activate 75 @Activate
108 public void activate() { 76 public void activate() {
109 - cpuMetrics = genMDbBuilder(CPU, CPU_METRICS); 77 + cpuMetrics = genMDbBuilder(Type.CPU, CPU_METRICS);
110 - memoryMetrics = genMDbBuilder(MEMORY, MEMORY_METRICS); 78 + memoryMetrics = genMDbBuilder(Type.MEMORY, MEMORY_METRICS);
111 - controlMessageMap = new ConcurrentHashMap<>(); 79 + controlMessageMap = Maps.newConcurrentMap();
112 - diskMetricsMap = new ConcurrentHashMap<>(); 80 + diskMetricsMap = Maps.newConcurrentMap();
113 - networkMetricsMap = new ConcurrentHashMap<>(); 81 + networkMetricsMap = Maps.newConcurrentMap();
114 - 82 +
115 - cpuBuf = new ConcurrentHashMap<>(); 83 + cpuBuf = Maps.newConcurrentMap();
116 - memoryBuf = new ConcurrentHashMap<>(); 84 + memoryBuf = Maps.newConcurrentMap();
117 - diskBuf = new ConcurrentHashMap<>(); 85 + diskBuf = Maps.newConcurrentMap();
118 - networkBuf = new ConcurrentHashMap<>(); 86 + networkBuf = Maps.newConcurrentMap();
119 - ctrlMsgBuf = new ConcurrentHashMap<>(); 87 + ctrlMsgBuf = Maps.newConcurrentMap();
88 +
89 + availableResourceMap = Maps.newConcurrentMap();
90 + availableDeviceIdSet = Sets.newConcurrentHashSet();
120 91
121 log.info("Started"); 92 log.info("Started");
122 } 93 }
...@@ -140,10 +111,14 @@ public class ControlPlaneMonitor implements ControlPlaneMonitorService { ...@@ -140,10 +111,14 @@ public class ControlPlaneMonitor implements ControlPlaneMonitorService {
140 if (deviceId.isPresent()) { 111 if (deviceId.isPresent()) {
141 112
142 // insert a new device entry if we cannot find any 113 // insert a new device entry if we cannot find any
143 - ctrlMsgBuf.putIfAbsent(deviceId.get(), new ConcurrentHashMap<>()); 114 + ctrlMsgBuf.putIfAbsent(deviceId.get(), Maps.newConcurrentMap());
144 115
145 // update control message metrics 116 // update control message metrics
146 - if (CTRL_MSGS.contains(cm.metricType())) { 117 + if (CONTROL_MESSAGE_METRICS.contains(cm.metricType())) {
118 +
119 + if (!availableDeviceIdSet.contains(deviceId.get())) {
120 + availableDeviceIdSet.add(deviceId.get());
121 + }
147 122
148 // we will accumulate the metric value into buffer first 123 // we will accumulate the metric value into buffer first
149 ctrlMsgBuf.get(deviceId.get()).putIfAbsent(cm.metricType(), 124 ctrlMsgBuf.get(deviceId.get()).putIfAbsent(cm.metricType(),
...@@ -151,7 +126,8 @@ public class ControlPlaneMonitor implements ControlPlaneMonitorService { ...@@ -151,7 +126,8 @@ public class ControlPlaneMonitor implements ControlPlaneMonitorService {
151 126
152 // if buffer contains all control message metrics, 127 // if buffer contains all control message metrics,
153 // we simply set and update the values into MetricsDatabase. 128 // we simply set and update the values into MetricsDatabase.
154 - if (ctrlMsgBuf.get(deviceId.get()).keySet().containsAll(CTRL_MSGS)) { 129 + if (ctrlMsgBuf.get(deviceId.get()).keySet()
130 + .containsAll(CONTROL_MESSAGE_METRICS)) {
155 updateControlMessages(ctrlMsgBuf.get(deviceId.get()), deviceId.get()); 131 updateControlMessages(ctrlMsgBuf.get(deviceId.get()), deviceId.get());
156 ctrlMsgBuf.get(deviceId.get()).clear(); 132 ctrlMsgBuf.get(deviceId.get()).clear();
157 } 133 }
...@@ -185,7 +161,14 @@ public class ControlPlaneMonitor implements ControlPlaneMonitorService { ...@@ -185,7 +161,14 @@ public class ControlPlaneMonitor implements ControlPlaneMonitorService {
185 String resourceName) { 161 String resourceName) {
186 // update disk metrics 162 // update disk metrics
187 if (DISK_METRICS.contains(cm.metricType())) { 163 if (DISK_METRICS.contains(cm.metricType())) {
188 - diskBuf.putIfAbsent(resourceName, new ConcurrentHashMap<>()); 164 + diskBuf.putIfAbsent(resourceName, Maps.newConcurrentMap());
165 +
166 + availableResourceMap.putIfAbsent(Type.DISK, Sets.newHashSet());
167 + availableResourceMap.computeIfPresent(Type.DISK, (k, v) -> {
168 + v.add(resourceName);
169 + return v;
170 + });
171 +
189 diskBuf.get(resourceName).putIfAbsent(cm.metricType(), 172 diskBuf.get(resourceName).putIfAbsent(cm.metricType(),
190 (double) cm.metricValue().getLoad()); 173 (double) cm.metricValue().getLoad());
191 if (diskBuf.get(resourceName).keySet().containsAll(DISK_METRICS)) { 174 if (diskBuf.get(resourceName).keySet().containsAll(DISK_METRICS)) {
...@@ -196,7 +179,14 @@ public class ControlPlaneMonitor implements ControlPlaneMonitorService { ...@@ -196,7 +179,14 @@ public class ControlPlaneMonitor implements ControlPlaneMonitorService {
196 179
197 // update network metrics 180 // update network metrics
198 if (NETWORK_METRICS.contains(cm.metricType())) { 181 if (NETWORK_METRICS.contains(cm.metricType())) {
199 - networkBuf.putIfAbsent(resourceName, new ConcurrentHashMap<>()); 182 + networkBuf.putIfAbsent(resourceName, Maps.newConcurrentMap());
183 +
184 + availableResourceMap.putIfAbsent(Type.NETWORK, Sets.newHashSet());
185 + availableResourceMap.computeIfPresent(Type.NETWORK, (k, v) -> {
186 + v.add(resourceName);
187 + return v;
188 + });
189 +
200 networkBuf.get(resourceName).putIfAbsent(cm.metricType(), 190 networkBuf.get(resourceName).putIfAbsent(cm.metricType(),
201 (double) cm.metricValue().getLoad()); 191 (double) cm.metricValue().getLoad());
202 if (networkBuf.get(resourceName).keySet().containsAll(NETWORK_METRICS)) { 192 if (networkBuf.get(resourceName).keySet().containsAll(NETWORK_METRICS)) {
...@@ -213,7 +203,8 @@ public class ControlPlaneMonitor implements ControlPlaneMonitorService { ...@@ -213,7 +203,8 @@ public class ControlPlaneMonitor implements ControlPlaneMonitorService {
213 if (clusterService.getLocalNode().equals(node)) { 203 if (clusterService.getLocalNode().equals(node)) {
214 204
215 if (deviceId.isPresent()) { 205 if (deviceId.isPresent()) {
216 - if (CTRL_MSGS.contains(type)) { 206 + if (CONTROL_MESSAGE_METRICS.contains(type) &&
207 + availableDeviceIdSet.contains(deviceId.get())) {
217 return new DefaultControlLoad(controlMessageMap.get(deviceId.get()), type); 208 return new DefaultControlLoad(controlMessageMap.get(deviceId.get()), type);
218 } 209 }
219 } else { 210 } else {
...@@ -238,11 +229,13 @@ public class ControlPlaneMonitor implements ControlPlaneMonitorService { ...@@ -238,11 +229,13 @@ public class ControlPlaneMonitor implements ControlPlaneMonitorService {
238 public ControlLoad getLoad(NodeId nodeId, ControlMetricType type, 229 public ControlLoad getLoad(NodeId nodeId, ControlMetricType type,
239 String resourceName) { 230 String resourceName) {
240 if (clusterService.getLocalNode().id().equals(nodeId)) { 231 if (clusterService.getLocalNode().id().equals(nodeId)) {
241 - if (DISK_METRICS.contains(type)) { 232 + if (DISK_METRICS.contains(type) &&
233 + availableResources(Type.DISK).contains(resourceName)) {
242 return new DefaultControlLoad(diskMetricsMap.get(resourceName), type); 234 return new DefaultControlLoad(diskMetricsMap.get(resourceName), type);
243 } 235 }
244 236
245 - if (NETWORK_METRICS.contains(type)) { 237 + if (NETWORK_METRICS.contains(type) &&
238 + availableResources(Type.NETWORK).contains(resourceName)) {
246 return new DefaultControlLoad(networkMetricsMap.get(resourceName), type); 239 return new DefaultControlLoad(networkMetricsMap.get(resourceName), type);
247 } 240 }
248 } else { 241 } else {
...@@ -252,34 +245,49 @@ public class ControlPlaneMonitor implements ControlPlaneMonitorService { ...@@ -252,34 +245,49 @@ public class ControlPlaneMonitor implements ControlPlaneMonitorService {
252 return null; 245 return null;
253 } 246 }
254 247
255 - private MetricsDatabase genMDbBuilder(String metricName, 248 + @Override
249 + public Set<String> availableResources(Type resourceType) {
250 + if (RESOURCE_TYPE_SET.contains(resourceType)) {
251 + if (Type.CONTROL_MESSAGE.equals(resourceType)) {
252 + return availableDeviceIdSet.stream().map(id ->
253 + id.toString()).collect(Collectors.toSet());
254 + } else {
255 + return availableResourceMap.get(resourceType);
256 + }
257 + }
258 + return null;
259 + }
260 +
261 + private MetricsDatabase genMDbBuilder(Type resourceType,
256 Set<ControlMetricType> metricTypes) { 262 Set<ControlMetricType> metricTypes) {
257 MetricsDatabase.Builder builder = new DefaultMetricsDatabase.Builder(); 263 MetricsDatabase.Builder builder = new DefaultMetricsDatabase.Builder();
258 - builder.withMetricName(metricName); 264 + builder.withMetricName(resourceType.toString());
259 metricTypes.forEach(type -> builder.addMetricType(type.toString())); 265 metricTypes.forEach(type -> builder.addMetricType(type.toString()));
260 return builder.build(); 266 return builder.build();
261 } 267 }
262 268
263 private void updateNetworkMetrics(Map<ControlMetricType, Double> metricMap, 269 private void updateNetworkMetrics(Map<ControlMetricType, Double> metricMap,
264 String resName) { 270 String resName) {
265 - networkMetricsMap.putIfAbsent(resName, genMDbBuilder(NETWORK, NETWORK_METRICS)); 271 + networkMetricsMap.putIfAbsent(resName,
272 + genMDbBuilder(Type.NETWORK, NETWORK_METRICS));
266 networkMetricsMap.get(resName).updateMetrics(convertMap(metricMap)); 273 networkMetricsMap.get(resName).updateMetrics(convertMap(metricMap));
267 } 274 }
268 275
269 private void updateDiskMetrics(Map<ControlMetricType, Double> metricMap, 276 private void updateDiskMetrics(Map<ControlMetricType, Double> metricMap,
270 String resName) { 277 String resName) {
271 - diskMetricsMap.putIfAbsent(resName, genMDbBuilder(DISK, DISK_METRICS)); 278 + diskMetricsMap.putIfAbsent(resName, genMDbBuilder(Type.DISK, DISK_METRICS));
272 diskMetricsMap.get(resName).updateMetrics(convertMap(metricMap)); 279 diskMetricsMap.get(resName).updateMetrics(convertMap(metricMap));
273 } 280 }
274 281
275 private void updateControlMessages(Map<ControlMetricType, Double> metricMap, 282 private void updateControlMessages(Map<ControlMetricType, Double> metricMap,
276 DeviceId devId) { 283 DeviceId devId) {
277 - controlMessageMap.putIfAbsent(devId, genMDbBuilder(CTRL_MSG, CTRL_MSGS)); 284 + controlMessageMap.putIfAbsent(devId,
285 + genMDbBuilder(Type.CONTROL_MESSAGE, CONTROL_MESSAGE_METRICS));
278 controlMessageMap.get(devId).updateMetrics(convertMap(metricMap)); 286 controlMessageMap.get(devId).updateMetrics(convertMap(metricMap));
279 } 287 }
280 288
281 private Map convertMap(Map<ControlMetricType, Double> map) { 289 private Map convertMap(Map<ControlMetricType, Double> map) {
282 - Map newMap = new ConcurrentHashMap<>(); 290 + Map newMap = Maps.newConcurrentMap();
283 map.forEach((k, v) -> newMap.putIfAbsent(k.toString(), v)); 291 map.forEach((k, v) -> newMap.putIfAbsent(k.toString(), v));
284 return newMap; 292 return newMap;
285 } 293 }
......
...@@ -231,7 +231,7 @@ public final class DefaultMetricsDatabase implements MetricsDatabase { ...@@ -231,7 +231,7 @@ public final class DefaultMetricsDatabase implements MetricsDatabase {
231 } 231 }
232 232
233 public static final class Builder implements MetricsDatabase.Builder { 233 public static final class Builder implements MetricsDatabase.Builder {
234 - private static final int RESOLUTION = 60; // seconds 234 + private static final int RESOLUTION_IN_SECOND = 60;
235 private static final String STORING_METHOD = "MEMORY"; 235 private static final String STORING_METHOD = "MEMORY";
236 private static final DsType SOURCE_TYPE = DsType.GAUGE; 236 private static final DsType SOURCE_TYPE = DsType.GAUGE;
237 private static final String DB_PATH = "CPMAN"; 237 private static final String DB_PATH = "CPMAN";
...@@ -259,7 +259,7 @@ public final class DefaultMetricsDatabase implements MetricsDatabase { ...@@ -259,7 +259,7 @@ public final class DefaultMetricsDatabase implements MetricsDatabase {
259 this.metricName = metricName; 259 this.metricName = metricName;
260 260
261 // define the resolution of monitored metrics 261 // define the resolution of monitored metrics
262 - rrdDef = new RrdDef(DB_PATH + "_" + metricName, RESOLUTION); 262 + rrdDef = new RrdDef(DB_PATH + "_" + metricName, RESOLUTION_IN_SECOND);
263 return this; 263 return this;
264 } 264 }
265 265
...@@ -279,7 +279,7 @@ public final class DefaultMetricsDatabase implements MetricsDatabase { ...@@ -279,7 +279,7 @@ public final class DefaultMetricsDatabase implements MetricsDatabase {
279 IntStream.range(0, dsDefs.size()).forEach(i -> dsDefArray[i] = dsDefs.get(i)); 279 IntStream.range(0, dsDefs.size()).forEach(i -> dsDefArray[i] = dsDefs.get(i));
280 280
281 rrdDef.addDatasource(dsDefArray); 281 rrdDef.addDatasource(dsDefArray);
282 - rrdDef.setStep(RESOLUTION); 282 + rrdDef.setStep(RESOLUTION_IN_SECOND);
283 283
284 // raw archive, no aggregation is required 284 // raw archive, no aggregation is required
285 ArcDef rawArchive = new ArcDef(CONSOL_FUNCTION, XFF_VALUE, 285 ArcDef rawArchive = new ArcDef(CONSOL_FUNCTION, XFF_VALUE,
...@@ -296,7 +296,7 @@ public final class DefaultMetricsDatabase implements MetricsDatabase { ...@@ -296,7 +296,7 @@ public final class DefaultMetricsDatabase implements MetricsDatabase {
296 } 296 }
297 297
298 private DsDef defineSchema(String metricType) { 298 private DsDef defineSchema(String metricType) {
299 - return new DsDef(metricType, SOURCE_TYPE, RESOLUTION, 299 + return new DsDef(metricType, SOURCE_TYPE, RESOLUTION_IN_SECOND,
300 MIN_VALUE, MAX_VALUE); 300 MIN_VALUE, MAX_VALUE);
301 } 301 }
302 } 302 }
......
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 +<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">
17 +
18 + <command-bundle xmlns="http://karaf.apache.org/xmlns/shell/v1.1.0">
19 + <command>
20 + <action class="org.onosproject.cpman.cli.ControlMetricsStatsListCommand"/>
21 + <completers>
22 + <ref component-id="controlResourceTypeCompleter"/>
23 + <ref component-id="networkResourceNameCompleter"/>
24 + <ref component-id="diskResourceNameCompleter"/>
25 + <ref component-id="controlMessageDeviceIdCompleter"/>
26 + </completers>
27 + </command>
28 + </command-bundle>
29 +
30 + <bean id="controlResourceTypeCompleter" class="org.onosproject.cpman.cli.ControlResourceTypeCompleter"/>
31 + <bean id="networkResourceNameCompleter" class="org.onosproject.cpman.cli.NetworkResourceNameCompleter"/>
32 + <bean id="diskResourceNameCompleter" class="org.onosproject.cpman.cli.DiskResourceNameCompleter"/>
33 + <bean id="controlMessageDeviceIdCompleter" class="org.onosproject.cpman.cli.ControlMessageDeviceIdCompleter"/>
34 +</blueprint>
...\ No newline at end of file ...\ No newline at end of file
...@@ -28,6 +28,7 @@ import org.onosproject.cpman.MetricValue; ...@@ -28,6 +28,7 @@ import org.onosproject.cpman.MetricValue;
28 import org.onosproject.net.DeviceId; 28 import org.onosproject.net.DeviceId;
29 29
30 import java.util.Optional; 30 import java.util.Optional;
31 +import java.util.Set;
31 32
32 import static org.easymock.EasyMock.anyObject; 33 import static org.easymock.EasyMock.anyObject;
33 import static org.easymock.EasyMock.createMock; 34 import static org.easymock.EasyMock.createMock;
...@@ -35,27 +36,8 @@ import static org.easymock.EasyMock.expect; ...@@ -35,27 +36,8 @@ import static org.easymock.EasyMock.expect;
35 import static org.easymock.EasyMock.replay; 36 import static org.easymock.EasyMock.replay;
36 import static org.hamcrest.Matchers.is; 37 import static org.hamcrest.Matchers.is;
37 import static org.junit.Assert.assertThat; 38 import static org.junit.Assert.assertThat;
38 -import static org.onosproject.cpman.ControlMetricType.CPU_IDLE_TIME; 39 +
39 -import static org.onosproject.cpman.ControlMetricType.CPU_LOAD; 40 +import static org.onosproject.cpman.ControlResource.*;
40 -import static org.onosproject.cpman.ControlMetricType.DISK_READ_BYTES;
41 -import static org.onosproject.cpman.ControlMetricType.DISK_WRITE_BYTES;
42 -import static org.onosproject.cpman.ControlMetricType.FLOW_MOD_PACKET;
43 -import static org.onosproject.cpman.ControlMetricType.FLOW_REMOVED_PACKET;
44 -import static org.onosproject.cpman.ControlMetricType.INBOUND_PACKET;
45 -import static org.onosproject.cpman.ControlMetricType.MEMORY_FREE;
46 -import static org.onosproject.cpman.ControlMetricType.MEMORY_FREE_RATIO;
47 -import static org.onosproject.cpman.ControlMetricType.MEMORY_USED;
48 -import static org.onosproject.cpman.ControlMetricType.MEMORY_USED_RATIO;
49 -import static org.onosproject.cpman.ControlMetricType.NW_INCOMING_BYTES;
50 -import static org.onosproject.cpman.ControlMetricType.NW_INCOMING_PACKETS;
51 -import static org.onosproject.cpman.ControlMetricType.NW_OUTGOING_BYTES;
52 -import static org.onosproject.cpman.ControlMetricType.NW_OUTGOING_PACKETS;
53 -import static org.onosproject.cpman.ControlMetricType.OUTBOUND_PACKET;
54 -import static org.onosproject.cpman.ControlMetricType.REPLY_PACKET;
55 -import static org.onosproject.cpman.ControlMetricType.REQUEST_PACKET;
56 -import static org.onosproject.cpman.ControlMetricType.SYS_CPU_TIME;
57 -import static org.onosproject.cpman.ControlMetricType.TOTAL_CPU_TIME;
58 -import static org.onosproject.cpman.ControlMetricType.USER_CPU_TIME;
59 41
60 /** 42 /**
61 * Unit test of control plane monitoring service. 43 * Unit test of control plane monitoring service.
...@@ -67,23 +49,12 @@ public class ControlPlaneMonitorTest { ...@@ -67,23 +49,12 @@ public class ControlPlaneMonitorTest {
67 private ClusterService mockClusterService; 49 private ClusterService mockClusterService;
68 private ControllerNode mockControllerNode; 50 private ControllerNode mockControllerNode;
69 private NodeId nodeId; 51 private NodeId nodeId;
70 - private static final ImmutableSet<ControlMetricType> CPU_METRICS =
71 - ImmutableSet.of(CPU_IDLE_TIME, CPU_LOAD, SYS_CPU_TIME,
72 - USER_CPU_TIME, TOTAL_CPU_TIME);
73 - private static final ImmutableSet<ControlMetricType> MEMORY_METRICS =
74 - ImmutableSet.of(MEMORY_FREE, MEMORY_FREE_RATIO, MEMORY_USED,
75 - MEMORY_USED_RATIO);
76 - private static final ImmutableSet<ControlMetricType> DISK_METRICS =
77 - ImmutableSet.of(DISK_READ_BYTES, DISK_WRITE_BYTES);
78 - private static final ImmutableSet<ControlMetricType> NETWORK_METRICS =
79 - ImmutableSet.of(NW_INCOMING_BYTES, NW_OUTGOING_BYTES,
80 - NW_INCOMING_PACKETS, NW_OUTGOING_PACKETS);
81 - private static final ImmutableSet<ControlMetricType> CTRL_MSGS =
82 - ImmutableSet.of(INBOUND_PACKET, OUTBOUND_PACKET, FLOW_MOD_PACKET,
83 - FLOW_REMOVED_PACKET, REQUEST_PACKET, REPLY_PACKET);
84 52
53 + /**
54 + * Sets up the services required by control plane monitor.
55 + */
85 @Before 56 @Before
86 - public void setup() throws Exception { 57 + public void setup() {
87 monitor = new ControlPlaneMonitor(); 58 monitor = new ControlPlaneMonitor();
88 monitor.activate(); 59 monitor.activate();
89 60
...@@ -134,13 +105,13 @@ public class ControlPlaneMonitorTest { ...@@ -134,13 +105,13 @@ public class ControlPlaneMonitorTest {
134 assertThat(monitor.getLoad(nodeId, cmt, Optional.ofNullable(null)).latest(), is(mv.getLoad())); 105 assertThat(monitor.getLoad(nodeId, cmt, Optional.ofNullable(null)).latest(), is(mv.getLoad()));
135 } 106 }
136 107
137 - private void testUpdateMetricWithResource(ControlMetricType cmt, MetricValue mv, String resoureName) { 108 + private void testUpdateMetricWithResource(ControlMetricType cmt, MetricValue mv, String resourceName) {
138 ControlMetric cm = new ControlMetric(cmt, mv); 109 ControlMetric cm = new ControlMetric(cmt, mv);
139 - monitor.updateMetric(cm, UPDATE_INTERVAL, resoureName); 110 + monitor.updateMetric(cm, UPDATE_INTERVAL, resourceName);
140 } 111 }
141 112
142 - private void testLoadMetricWithResource(ControlMetricType cmt, MetricValue mv, String resoureName) { 113 + private void testLoadMetricWithResource(ControlMetricType cmt, MetricValue mv, String resourceName) {
143 - assertThat(monitor.getLoad(nodeId, cmt, resoureName).latest(), is(mv.getLoad())); 114 + assertThat(monitor.getLoad(nodeId, cmt, resourceName).latest(), is(mv.getLoad()));
144 } 115 }
145 116
146 private void testUpdateMetricWithId(ControlMetricType cmt, MetricValue mv, DeviceId did) { 117 private void testUpdateMetricWithId(ControlMetricType cmt, MetricValue mv, DeviceId did) {
...@@ -152,6 +123,9 @@ public class ControlPlaneMonitorTest { ...@@ -152,6 +123,9 @@ public class ControlPlaneMonitorTest {
152 assertThat(monitor.getLoad(nodeId, cmt, Optional.of(did)).latest(), is(mv.getLoad())); 123 assertThat(monitor.getLoad(nodeId, cmt, Optional.of(did)).latest(), is(mv.getLoad()));
153 } 124 }
154 125
126 + /**
127 + * Tests cpu metric update and load function.
128 + */
155 @Test 129 @Test
156 public void testCpuMetric() { 130 public void testCpuMetric() {
157 MetricValue mv = new MetricValue.Builder().load(30).add(); 131 MetricValue mv = new MetricValue.Builder().load(30).add();
...@@ -160,6 +134,9 @@ public class ControlPlaneMonitorTest { ...@@ -160,6 +134,9 @@ public class ControlPlaneMonitorTest {
160 CPU_METRICS.forEach(cmt -> testLoadMetricWithoutId(cmt, mv)); 134 CPU_METRICS.forEach(cmt -> testLoadMetricWithoutId(cmt, mv));
161 } 135 }
162 136
137 + /**
138 + * Tests memory metric update and load function.
139 + */
163 @Test 140 @Test
164 public void testMemoryMetric() { 141 public void testMemoryMetric() {
165 MetricValue mv = new MetricValue.Builder().load(40).add(); 142 MetricValue mv = new MetricValue.Builder().load(40).add();
...@@ -168,11 +145,14 @@ public class ControlPlaneMonitorTest { ...@@ -168,11 +145,14 @@ public class ControlPlaneMonitorTest {
168 MEMORY_METRICS.forEach(cmt -> testLoadMetricWithoutId(cmt, mv)); 145 MEMORY_METRICS.forEach(cmt -> testLoadMetricWithoutId(cmt, mv));
169 } 146 }
170 147
148 + /**
149 + * Tests disk metric update and load function.
150 + */
171 @Test 151 @Test
172 public void testDiskMetric() { 152 public void testDiskMetric() {
173 MetricValue mv = new MetricValue.Builder().load(50).add(); 153 MetricValue mv = new MetricValue.Builder().load(50).add();
174 154
175 - ImmutableSet<String> set = ImmutableSet.of("disk1", "disk2"); 155 + Set<String> set = ImmutableSet.of("disk1", "disk2");
176 156
177 set.forEach(disk -> DISK_METRICS.forEach(cmt -> 157 set.forEach(disk -> DISK_METRICS.forEach(cmt ->
178 testUpdateMetricWithResource(cmt, mv, disk))); 158 testUpdateMetricWithResource(cmt, mv, disk)));
...@@ -181,11 +161,14 @@ public class ControlPlaneMonitorTest { ...@@ -181,11 +161,14 @@ public class ControlPlaneMonitorTest {
181 testLoadMetricWithResource(cmt, mv, disk))); 161 testLoadMetricWithResource(cmt, mv, disk)));
182 } 162 }
183 163
164 + /**
165 + * Tests network metric update and load function.
166 + */
184 @Test 167 @Test
185 public void testNetworkMetric() { 168 public void testNetworkMetric() {
186 MetricValue mv = new MetricValue.Builder().load(10).add(); 169 MetricValue mv = new MetricValue.Builder().load(10).add();
187 170
188 - ImmutableSet<String> set = ImmutableSet.of("eth0", "eth1"); 171 + Set<String> set = ImmutableSet.of("eth0", "eth1");
189 172
190 set.forEach(network -> NETWORK_METRICS.forEach(cmt -> 173 set.forEach(network -> NETWORK_METRICS.forEach(cmt ->
191 testUpdateMetricWithResource(cmt, mv, network))); 174 testUpdateMetricWithResource(cmt, mv, network)));
...@@ -194,17 +177,40 @@ public class ControlPlaneMonitorTest { ...@@ -194,17 +177,40 @@ public class ControlPlaneMonitorTest {
194 testLoadMetricWithResource(cmt, mv, network))); 177 testLoadMetricWithResource(cmt, mv, network)));
195 } 178 }
196 179
180 + /**
181 + * Tests control message update and load function.
182 + */
197 @Test 183 @Test
198 - public void testUpdateControlMessage() { 184 + public void testControlMessage() {
199 MetricValue mv = new MetricValue.Builder().load(10).add(); 185 MetricValue mv = new MetricValue.Builder().load(10).add();
186 + Set<DeviceId> set = ImmutableSet.of(DeviceId.deviceId("of:0000000000000001"),
187 + DeviceId.deviceId("of:0000000000000002"));
200 188
201 - ImmutableSet<String> set = ImmutableSet.of("of:0000000000000001", 189 + set.forEach(devId -> CONTROL_MESSAGE_METRICS.forEach(cmt ->
202 - "of:0000000000000002"); 190 + testUpdateMetricWithId(cmt, mv, devId)));
203 191
204 - set.forEach(ctrlMsg -> CTRL_MSGS.forEach(cmt -> 192 + set.forEach(devId -> CONTROL_MESSAGE_METRICS.forEach(cmt ->
205 - testUpdateMetricWithId(cmt, mv, DeviceId.deviceId(ctrlMsg)))); 193 + testLoadMetricWithId(cmt, mv, devId)));
194 + }
195 +
196 + /**
197 + * Tests available resource update and load function.
198 + */
199 + @Test
200 + public void testAvailableResources() {
201 + MetricValue mv = new MetricValue.Builder().load(50).add();
202 +
203 + Set<String> diskSet = ImmutableSet.of("disk1", "disk2");
204 +
205 + diskSet.forEach(disk -> DISK_METRICS.forEach(cmt ->
206 + testUpdateMetricWithResource(cmt, mv, disk)));
207 +
208 + Set<String> networkSet = ImmutableSet.of("eth0", "eth1");
209 +
210 + networkSet.forEach(network -> NETWORK_METRICS.forEach(cmt ->
211 + testUpdateMetricWithResource(cmt, mv, network)));
206 212
207 - set.forEach(ctrlMsg -> CTRL_MSGS.forEach(cmt -> 213 + assertThat(monitor.availableResources(Type.DISK), is(diskSet));
208 - testLoadMetricWithId(cmt, mv, DeviceId.deviceId(ctrlMsg)))); 214 + assertThat(monitor.availableResources(Type.NETWORK), is(networkSet));
209 } 215 }
210 } 216 }
......
...@@ -39,7 +39,7 @@ public class MetricsDatabaseTest { ...@@ -39,7 +39,7 @@ public class MetricsDatabaseTest {
39 private static final String MEMORY_USED_PERC = "usedPerc"; 39 private static final String MEMORY_USED_PERC = "usedPerc";
40 40
41 /** 41 /**
42 - * Initializes the MetricsDatabase instance. 42 + * Initializes metrics database instance.
43 */ 43 */
44 @Before 44 @Before
45 public void setUp() { 45 public void setUp() {
......