blob: 3c8457ad0b3b964154c260d6284de275706559dc [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.util.List;
19
20import com.fasterxml.jackson.databind.JsonNode;
21import com.fasterxml.jackson.databind.ObjectMapper;
22import com.fasterxml.jackson.databind.node.ArrayNode;
23import com.fasterxml.jackson.databind.node.ObjectNode;
24import org.apache.karaf.shell.commands.Command;
Brian O'Connorabafb502014-12-02 22:26:20 -080025import org.onosproject.cli.AbstractShellCommand;
26import org.onosproject.metrics.intent.IntentMetricsService;
27import org.onosproject.net.intent.IntentEvent;
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070028
29/**
30 * Command to show the list of last intent events.
31 */
32@Command(scope = "onos", name = "intents-events",
33 description = "Lists the last intent events")
34public class IntentEventsListCommand extends AbstractShellCommand {
35
36 private static final String FORMAT_EVENT = "Event=%s";
37
38 @Override
39 protected void execute() {
40 IntentMetricsService service = get(IntentMetricsService.class);
41
42 if (outputJson()) {
43 print("%s", json(service.getEvents()));
44 } else {
45 for (IntentEvent event : service.getEvents()) {
46 print(FORMAT_EVENT, event);
47 print(""); // Extra empty line for clarity
48 }
49 }
50 }
51
52 /**
53 * Produces a JSON array of intent events.
54 *
55 * @param intentEvents the intent events with the data
56 * @return JSON array with the intent events
57 */
58 private JsonNode json(List<IntentEvent> intentEvents) {
59 ObjectMapper mapper = new ObjectMapper();
60 ArrayNode result = mapper.createArrayNode();
61
62 for (IntentEvent event : intentEvents) {
63 result.add(json(mapper, event));
64 }
65 return result;
66 }
67
68 /**
69 * Produces JSON object for a intent event.
70 *
71 * @param mapper the JSON object mapper to use
72 * @param intentEvent the intent event with the data
73 * @return JSON object for the intent event
74 */
75 private ObjectNode json(ObjectMapper mapper, IntentEvent intentEvent) {
76 ObjectNode result = mapper.createObjectNode();
77
78 result.put("time", intentEvent.time())
79 .put("type", intentEvent.type().toString())
80 .put("event", intentEvent.toString());
81 return result;
82 }
83}