blob: e7e6e233f9342619495e5cf7b1da8f88d99479aa [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070018import java.util.concurrent.TimeUnit;
19
20import com.codahale.metrics.Gauge;
21import com.codahale.metrics.Meter;
22import com.codahale.metrics.json.MetricsModule;
23import com.fasterxml.jackson.core.JsonProcessingException;
24import com.fasterxml.jackson.databind.JsonNode;
25import com.fasterxml.jackson.databind.ObjectMapper;
26import com.fasterxml.jackson.databind.node.ObjectNode;
Ray Milkey86ad7bb2018-09-27 12:32:28 -070027import org.apache.karaf.shell.api.action.Command;
Ray Milkey7a2dee52018-09-28 10:58:28 -070028import org.apache.karaf.shell.api.action.lifecycle.Service;
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 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070036@Service
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070037@Command(scope = "onos", name = "intents-events-metrics",
38 description = "Lists intent events metrics")
39public class IntentEventsMetricsCommand extends AbstractShellCommand {
40
41 private static final String FORMAT_GAUGE =
42 "Intent %s Event Timestamp (ms from epoch)=%d";
43 private static final String FORMAT_METER =
44 "Intent %s Events count=%d rate(events/sec) mean=%f m1=%f m5=%f m15=%f";
45
46 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070047 protected void doExecute() {
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070048 IntentMetricsService service = get(IntentMetricsService.class);
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070049
50 if (outputJson()) {
51 ObjectMapper mapper = new ObjectMapper()
52 .registerModule(new MetricsModule(TimeUnit.SECONDS,
53 TimeUnit.MILLISECONDS,
54 false));
55 ObjectNode result = mapper.createObjectNode();
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070056 result = json(mapper, result, "intentSubmitted",
57 service.intentSubmittedEventMetric());
58 result = json(mapper, result, "intentInstalled",
59 service.intentInstalledEventMetric());
Pavlin Radoslavov0f8a1e42015-01-13 11:52:52 -080060 result = json(mapper, result, "intentFailed",
61 service.intentFailedEventMetric());
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070062 result = json(mapper, result, "intentWithdrawRequested",
63 service.intentWithdrawRequestedEventMetric());
64 result = json(mapper, result, "intentWithdrawn",
65 service.intentWithdrawnEventMetric());
Pavlin Radoslavov166c5472015-03-18 11:47:50 -070066 result = json(mapper, result, "intentPurged",
67 service.intentPurgedEventMetric());
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070068 print("%s", result);
69 } else {
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070070 printEventMetric("Submitted",
71 service.intentSubmittedEventMetric());
72 printEventMetric("Installed",
73 service.intentInstalledEventMetric());
Pavlin Radoslavov0f8a1e42015-01-13 11:52:52 -080074 printEventMetric("Failed",
75 service.intentFailedEventMetric());
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070076 printEventMetric("Withdraw Requested",
77 service.intentWithdrawRequestedEventMetric());
78 printEventMetric("Withdrawn",
79 service.intentWithdrawnEventMetric());
Pavlin Radoslavov166c5472015-03-18 11:47:50 -070080 printEventMetric("Purged",
81 service.intentPurgedEventMetric());
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070082 }
83 }
84
85 /**
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070086 * Produces JSON node for an Event Metric.
87 *
88 * @param mapper the JSON object mapper to use
89 * @param objectNode the JSON object node to use
90 * @param propertyPrefix the property prefix to use
91 * @param eventMetric the Event Metric with the data
92 * @return JSON object node for the Event Metric
93 */
94 private ObjectNode json(ObjectMapper mapper, ObjectNode objectNode,
95 String propertyPrefix, EventMetric eventMetric) {
96 String gaugeName = propertyPrefix + "Timestamp";
97 String meterName = propertyPrefix + "Rate";
98 Gauge<Long> gauge = eventMetric.lastEventTimestampGauge();
99 Meter meter = eventMetric.eventRateMeter();
100
Ray Milkey9d810f62015-02-13 11:20:58 -0800101 objectNode.set(gaugeName, json(mapper, gauge));
102 objectNode.set(meterName, json(mapper, meter));
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700103 return objectNode;
104 }
105
106 /**
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700107 * Produces JSON node for an Object.
108 *
109 * @param mapper the JSON object mapper to use
110 * @param object the Object with the data
111 * @return JSON node for the Object
112 */
113 private JsonNode json(ObjectMapper mapper, Object object) {
114 //
115 // NOTE: The API for custom serializers is incomplete,
116 // hence we have to parse the JSON string to create JsonNode.
117 //
118 try {
119 final String objectJson = mapper.writeValueAsString(object);
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700120 JsonNode jsonNode = mapper.readTree(objectJson);
121 return jsonNode;
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700122 } catch (JsonProcessingException e) {
123 log.error("Error writing value as JSON string", e);
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700124 }
125 return null;
126 }
127
128 /**
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700129 * Prints an Event Metric.
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700130 *
131 * @param operationStr the string with the intent operation to print
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700132 * @param eventMetric the Event Metric to print
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700133 */
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700134 private void printEventMetric(String operationStr,
135 EventMetric eventMetric) {
136 Gauge<Long> gauge = eventMetric.lastEventTimestampGauge();
137 Meter meter = eventMetric.eventRateMeter();
138 TimeUnit rateUnit = TimeUnit.SECONDS;
139 double rateFactor = rateUnit.toSeconds(1);
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700140
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700141 // Print the Gauge
142 print(FORMAT_GAUGE, operationStr, gauge.getValue());
143
144 // Print the Meter
145 print(FORMAT_METER, operationStr, meter.getCount(),
146 meter.getMeanRate() * rateFactor,
147 meter.getOneMinuteRate() * rateFactor,
148 meter.getFiveMinuteRate() * rateFactor,
149 meter.getFifteenMinuteRate() * rateFactor);
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700150 }
151}