blob: 75e3f276802d9fb0edbf16c4ffa7b694c4a3e745 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska781d18b2014-10-27 10:31:25 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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
Thomas Vachuska781d18b2014-10-27 10:31:25 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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.
Thomas Vachuska781d18b2014-10-27 10:31:25 -070015 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.metrics.intent.cli;
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070017
18import java.io.IOException;
19import java.util.concurrent.TimeUnit;
20
21import com.codahale.metrics.Gauge;
22import com.codahale.metrics.Meter;
23import com.codahale.metrics.json.MetricsModule;
24import com.fasterxml.jackson.core.JsonProcessingException;
25import com.fasterxml.jackson.databind.JsonNode;
26import com.fasterxml.jackson.databind.ObjectMapper;
27import com.fasterxml.jackson.databind.node.ObjectNode;
28import org.apache.karaf.shell.commands.Command;
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070029import org.onlab.metrics.EventMetric;
Brian O'Connorabafb502014-12-02 22:26:20 -080030import org.onosproject.cli.AbstractShellCommand;
31import org.onosproject.metrics.intent.IntentMetricsService;
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070032
33/**
34 * Command to show the intent events metrics.
35 */
36@Command(scope = "onos", name = "intents-events-metrics",
37 description = "Lists intent events metrics")
38public class IntentEventsMetricsCommand extends AbstractShellCommand {
39
40 private static final String FORMAT_GAUGE =
41 "Intent %s Event Timestamp (ms from epoch)=%d";
42 private static final String FORMAT_METER =
43 "Intent %s Events count=%d rate(events/sec) mean=%f m1=%f m5=%f m15=%f";
44
45 @Override
46 protected void execute() {
47 IntentMetricsService service = get(IntentMetricsService.class);
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070048
49 if (outputJson()) {
50 ObjectMapper mapper = new ObjectMapper()
51 .registerModule(new MetricsModule(TimeUnit.SECONDS,
52 TimeUnit.MILLISECONDS,
53 false));
54 ObjectNode result = mapper.createObjectNode();
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070055 result = json(mapper, result, "intentSubmitted",
56 service.intentSubmittedEventMetric());
57 result = json(mapper, result, "intentInstalled",
58 service.intentInstalledEventMetric());
Pavlin Radoslavov0f8a1e42015-01-13 11:52:52 -080059 result = json(mapper, result, "intentFailed",
60 service.intentFailedEventMetric());
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070061 result = json(mapper, result, "intentWithdrawRequested",
62 service.intentWithdrawRequestedEventMetric());
63 result = json(mapper, result, "intentWithdrawn",
64 service.intentWithdrawnEventMetric());
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070065 print("%s", result);
66 } else {
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070067 printEventMetric("Submitted",
68 service.intentSubmittedEventMetric());
69 printEventMetric("Installed",
70 service.intentInstalledEventMetric());
Pavlin Radoslavov0f8a1e42015-01-13 11:52:52 -080071 printEventMetric("Failed",
72 service.intentFailedEventMetric());
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070073 printEventMetric("Withdraw Requested",
74 service.intentWithdrawRequestedEventMetric());
75 printEventMetric("Withdrawn",
76 service.intentWithdrawnEventMetric());
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070077 }
78 }
79
80 /**
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070081 * Produces JSON node for an Event Metric.
82 *
83 * @param mapper the JSON object mapper to use
84 * @param objectNode the JSON object node to use
85 * @param propertyPrefix the property prefix to use
86 * @param eventMetric the Event Metric with the data
87 * @return JSON object node for the Event Metric
88 */
89 private ObjectNode json(ObjectMapper mapper, ObjectNode objectNode,
90 String propertyPrefix, EventMetric eventMetric) {
91 String gaugeName = propertyPrefix + "Timestamp";
92 String meterName = propertyPrefix + "Rate";
93 Gauge<Long> gauge = eventMetric.lastEventTimestampGauge();
94 Meter meter = eventMetric.eventRateMeter();
95
Ray Milkey9d810f62015-02-13 11:20:58 -080096 objectNode.set(gaugeName, json(mapper, gauge));
97 objectNode.set(meterName, json(mapper, meter));
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070098 return objectNode;
99 }
100
101 /**
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700102 * Produces JSON node for an Object.
103 *
104 * @param mapper the JSON object mapper to use
105 * @param object the Object with the data
106 * @return JSON node for the Object
107 */
108 private JsonNode json(ObjectMapper mapper, Object object) {
109 //
110 // NOTE: The API for custom serializers is incomplete,
111 // hence we have to parse the JSON string to create JsonNode.
112 //
113 try {
114 final String objectJson = mapper.writeValueAsString(object);
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700115 JsonNode jsonNode = mapper.readTree(objectJson);
116 return jsonNode;
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700117 } catch (JsonProcessingException e) {
118 log.error("Error writing value as JSON string", e);
119 } catch (IOException e) {
120 log.error("Error writing value as JSON string", e);
121 }
122 return null;
123 }
124
125 /**
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700126 * Prints an Event Metric.
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700127 *
128 * @param operationStr the string with the intent operation to print
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700129 * @param eventMetric the Event Metric to print
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700130 */
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700131 private void printEventMetric(String operationStr,
132 EventMetric eventMetric) {
133 Gauge<Long> gauge = eventMetric.lastEventTimestampGauge();
134 Meter meter = eventMetric.eventRateMeter();
135 TimeUnit rateUnit = TimeUnit.SECONDS;
136 double rateFactor = rateUnit.toSeconds(1);
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700137
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700138 // Print the Gauge
139 print(FORMAT_GAUGE, operationStr, gauge.getValue());
140
141 // Print the Meter
142 print(FORMAT_METER, operationStr, meter.getCount(),
143 meter.getMeanRate() * rateFactor,
144 meter.getOneMinuteRate() * rateFactor,
145 meter.getFiveMinuteRate() * rateFactor,
146 meter.getFifteenMinuteRate() * rateFactor);
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700147 }
148}