blob: 932566b44ae7a88d0259c7ab37ddde3cf2ac101e [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());
59 result = json(mapper, result, "intentWithdrawRequested",
60 service.intentWithdrawRequestedEventMetric());
61 result = json(mapper, result, "intentWithdrawn",
62 service.intentWithdrawnEventMetric());
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070063 print("%s", result);
64 } else {
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070065 printEventMetric("Submitted",
66 service.intentSubmittedEventMetric());
67 printEventMetric("Installed",
68 service.intentInstalledEventMetric());
69 printEventMetric("Withdraw Requested",
70 service.intentWithdrawRequestedEventMetric());
71 printEventMetric("Withdrawn",
72 service.intentWithdrawnEventMetric());
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070073 }
74 }
75
76 /**
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070077 * Produces JSON node for an Event Metric.
78 *
79 * @param mapper the JSON object mapper to use
80 * @param objectNode the JSON object node to use
81 * @param propertyPrefix the property prefix to use
82 * @param eventMetric the Event Metric with the data
83 * @return JSON object node for the Event Metric
84 */
85 private ObjectNode json(ObjectMapper mapper, ObjectNode objectNode,
86 String propertyPrefix, EventMetric eventMetric) {
87 String gaugeName = propertyPrefix + "Timestamp";
88 String meterName = propertyPrefix + "Rate";
89 Gauge<Long> gauge = eventMetric.lastEventTimestampGauge();
90 Meter meter = eventMetric.eventRateMeter();
91
92 objectNode.put(gaugeName, json(mapper, gauge));
93 objectNode.put(meterName, json(mapper, meter));
94 return objectNode;
95 }
96
97 /**
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070098 * Produces JSON node for an Object.
99 *
100 * @param mapper the JSON object mapper to use
101 * @param object the Object with the data
102 * @return JSON node for the Object
103 */
104 private JsonNode json(ObjectMapper mapper, Object object) {
105 //
106 // NOTE: The API for custom serializers is incomplete,
107 // hence we have to parse the JSON string to create JsonNode.
108 //
109 try {
110 final String objectJson = mapper.writeValueAsString(object);
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700111 JsonNode jsonNode = mapper.readTree(objectJson);
112 return jsonNode;
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700113 } catch (JsonProcessingException e) {
114 log.error("Error writing value as JSON string", e);
115 } catch (IOException e) {
116 log.error("Error writing value as JSON string", e);
117 }
118 return null;
119 }
120
121 /**
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700122 * Prints an Event Metric.
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700123 *
124 * @param operationStr the string with the intent operation to print
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700125 * @param eventMetric the Event Metric to print
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700126 */
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700127 private void printEventMetric(String operationStr,
128 EventMetric eventMetric) {
129 Gauge<Long> gauge = eventMetric.lastEventTimestampGauge();
130 Meter meter = eventMetric.eventRateMeter();
131 TimeUnit rateUnit = TimeUnit.SECONDS;
132 double rateFactor = rateUnit.toSeconds(1);
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700133
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700134 // Print the Gauge
135 print(FORMAT_GAUGE, operationStr, gauge.getValue());
136
137 // Print the Meter
138 print(FORMAT_METER, operationStr, meter.getCount(),
139 meter.getMeanRate() * rateFactor,
140 meter.getOneMinuteRate() * rateFactor,
141 meter.getFiveMinuteRate() * rateFactor,
142 meter.getFifteenMinuteRate() * rateFactor);
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700143 }
144}