Jian Li
Committed by Gerrit Code Review

[ONOS-4176] Implement influxdb retriever for querying metrics

Change-Id: Ia1f3fc4fb3c76fafd003320940b8fe16b039ddae
...@@ -21,5 +21,10 @@ ...@@ -21,5 +21,10 @@
21 <bundle>mvn:${project.groupId}/onos-app-influxdb/${project.version}</bundle> 21 <bundle>mvn:${project.groupId}/onos-app-influxdb/${project.version}</bundle>
22 <bundle>wrap:mvn:com.izettle/metrics-influxdb/1.1.1$Bundle-SymbolicName=metrics-influxdb&amp;Bundle-Version=1.1.1</bundle> 22 <bundle>wrap:mvn:com.izettle/metrics-influxdb/1.1.1$Bundle-SymbolicName=metrics-influxdb&amp;Bundle-Version=1.1.1</bundle>
23 <bundle>mvn:commons-codec/commons-codec/1.10</bundle> 23 <bundle>mvn:commons-codec/commons-codec/1.10</bundle>
24 + <bundle>wrap:mvn:org.influxdb/influxdb-java/2.1$Bundle-SymbolicName=influxdb-java&amp;Bundle-Version=2.1</bundle>
25 + <bundle>wrap:mvn:com.squareup.retrofit/retrofit/1.9.0$Bundle-SymbolicName=retrofit&amp;Bundle-Version=1.9.0</bundle>
26 + <bundle>wrap:mvn:com.squareup.okhttp/okhttp/2.4.0$Bundle-SymbolicName=okhttp&amp;Bundle-Version=2.4.0</bundle>
27 + <bundle>wrap:mvn:com.squareup.okio/okio/1.4.0$Bundle-SymbolicName=okio&amp;Bundle-Version=1.4.0</bundle>
28 + <bundle>mvn:com.google.code.gson/gson/2.3.1</bundle>
24 </feature> 29 </feature>
25 </features> 30 </features>
......
...@@ -71,6 +71,11 @@ ...@@ -71,6 +71,11 @@
71 <version>1.1.1</version> 71 <version>1.1.1</version>
72 </dependency> 72 </dependency>
73 <dependency> 73 <dependency>
74 + <groupId>org.influxdb</groupId>
75 + <artifactId>influxdb-java</artifactId>
76 + <version>2.1</version>
77 + </dependency>
78 + <dependency>
74 <groupId>org.onosproject</groupId> 79 <groupId>org.onosproject</groupId>
75 <artifactId>onos-api</artifactId> 80 <artifactId>onos-api</artifactId>
76 <version>${project.version}</version> 81 <version>${project.version}</version>
......
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.influxdbmetrics;
17 +
18 +import org.apache.commons.lang.StringUtils;
19 +import org.joda.time.DateTime;
20 +import org.joda.time.format.DateTimeFormat;
21 +import org.joda.time.format.DateTimeFormatter;
22 +
23 +import static com.google.common.base.Preconditions.checkNotNull;
24 +
25 +/**
26 + * Default implementation of influx metric.
27 + */
28 +public final class DefaultInfluxMetric implements InfluxMetric {
29 +
30 + private double oneMinRate;
31 + private DateTime time;
32 +
33 + private DefaultInfluxMetric(double oneMinRate, DateTime time) {
34 + this.oneMinRate = oneMinRate;
35 + this.time = time;
36 + }
37 +
38 + @Override
39 + public double oneMinRate() {
40 + return oneMinRate;
41 + }
42 +
43 + @Override
44 + public DateTime time() {
45 + return time;
46 + }
47 +
48 + public static final class Builder implements InfluxMetric.Builder {
49 +
50 + private double oneMinRate;
51 + private String timestamp;
52 + private static final String TIMESTAMP_MSG = "Must specify a timestamp.";
53 + private static final String ONE_MIN_RATE_MSG = "Must specify one minute rate.";
54 +
55 + public Builder() {
56 + }
57 +
58 + @Override
59 + public InfluxMetric.Builder oneMinRate(double rate) {
60 + this.oneMinRate = rate;
61 + return this;
62 + }
63 +
64 + @Override
65 + public InfluxMetric.Builder time(String time) {
66 + this.timestamp = time;
67 + return this;
68 + }
69 +
70 + @Override
71 + public InfluxMetric build() {
72 + checkNotNull(oneMinRate, ONE_MIN_RATE_MSG);
73 + checkNotNull(timestamp, TIMESTAMP_MSG);
74 +
75 + return new DefaultInfluxMetric(oneMinRate, parseTime(timestamp));
76 + }
77 +
78 + private DateTime parseTime(String time) {
79 + String reformatTime = StringUtils.replace(StringUtils.replace(time, "T", " "), "Z", "");
80 + DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
81 + return formatter.parseDateTime(reformatTime);
82 + }
83 + }
84 +}
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.influxdbmetrics;
17 +
18 +import org.onosproject.cluster.NodeId;
19 +
20 +import java.util.List;
21 +import java.util.Map;
22 +import java.util.concurrent.TimeUnit;
23 +
24 +/**
25 + * A Metric retriever interface for querying metrics value from influxDB server.
26 + */
27 +public interface InfluxDbMetricsRetriever {
28 +
29 + /**
30 + * Returns last metric values from all nodes.
31 + *
32 + * @return all metrics from all nodes
33 + */
34 + Map<NodeId, Map<String, InfluxMetric>> allMetrics();
35 +
36 + /**
37 + * Returns last metric values from a node.
38 + *
39 + * @param nodeId node identification
40 + * @return all metrics from a given node
41 + */
42 + Map<String, InfluxMetric> metricsByNodeId(NodeId nodeId);
43 +
44 + /**
45 + * Returns a collection of last metric values from all nodes.
46 + *
47 + * @param metricName metric name
48 + * @return a collection of metrics from all nodes
49 + */
50 + Map<NodeId, InfluxMetric> metricsByName(String metricName);
51 +
52 + /**
53 + * Returns a last metric value from a given node.
54 + *
55 + * @param nodeId node identification
56 + * @param metricName metric name
57 + * @return a metric value from a given node
58 + */
59 + InfluxMetric metric(NodeId nodeId, String metricName);
60 +
61 + /**
62 + * Returns metric values of all nodes within a given period of time.
63 + *
64 + * @param period projected period
65 + * @param unit time unit
66 + * @return all metric values of all nodes
67 + */
68 + Map<NodeId, Map<String, List<InfluxMetric>>> allMetrics(int period, TimeUnit unit);
69 +
70 + /**
71 + * Returns metric values of a node within a given period of time.
72 + *
73 + * @param nodeId node identification
74 + * @param period projected period
75 + * @param unit time unit
76 + * @return metric value of a node
77 + */
78 + Map<String, List<InfluxMetric>> metricsByNodeId(NodeId nodeId, int period, TimeUnit unit);
79 +
80 + /**
81 + * Returns a collection of last metric values of all nodes within a given period of time.
82 + *
83 + * @param metricName metric name
84 + * @param period projected period
85 + * @param unit time unit
86 + * @return metric value of all nodes
87 + */
88 + Map<NodeId, List<InfluxMetric>> metricsByName(String metricName, int period, TimeUnit unit);
89 +
90 + /**
91 + * Returns metric value of a given node within a given period of time.
92 + *
93 + * @param nodeId node identification
94 + * @param metricName metric name
95 + * @param period projected period
96 + * @param unit time unit
97 + * @return metric value of a node
98 + */
99 + List<InfluxMetric> metric(NodeId nodeId, String metricName, int period, TimeUnit unit);
100 +}
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.influxdbmetrics;
17 +
18 +import org.joda.time.DateTime;
19 +
20 +/**
21 + * Metric that represents all values queried from influx database.
22 + */
23 +public interface InfluxMetric {
24 +
25 + /**
26 + * Returns one minute rate of the given metric.
27 + *
28 + * @return one minute rate of the given metric
29 + */
30 + double oneMinRate();
31 +
32 +
33 + /**
34 + * Returns collected timestamp of the given metric.
35 + *
36 + * @return collected timestamp of the given metric
37 + */
38 + DateTime time();
39 +
40 + /**
41 + * A builder of InfluxMetric.
42 + */
43 + interface Builder {
44 +
45 + /**
46 + * Sets one minute rate.
47 + *
48 + * @param rate one minute rate
49 + * @return builder object
50 + */
51 + Builder oneMinRate(double rate);
52 +
53 + /**
54 + * Sets collected timestamp.
55 + *
56 + * @param time timestamp
57 + * @return builder object
58 + */
59 + Builder time(String time);
60 +
61 + /**
62 + * Builds a influx metric instance.
63 + *
64 + * @return influx metric instance
65 + */
66 + InfluxMetric build();
67 + }
68 +}