Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 19 | package org.onlab.onos.metrics.intent.cli; |
| 20 | |
| 21 | import java.util.List; |
| 22 | |
| 23 | import com.fasterxml.jackson.databind.JsonNode; |
| 24 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 25 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 26 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 27 | import org.apache.karaf.shell.commands.Command; |
| 28 | import org.onlab.onos.cli.AbstractShellCommand; |
| 29 | import org.onlab.onos.metrics.intent.IntentMetricsService; |
| 30 | import org.onlab.onos.net.intent.IntentEvent; |
| 31 | |
| 32 | /** |
| 33 | * Command to show the list of last intent events. |
| 34 | */ |
| 35 | @Command(scope = "onos", name = "intents-events", |
| 36 | description = "Lists the last intent events") |
| 37 | public class IntentEventsListCommand extends AbstractShellCommand { |
| 38 | |
| 39 | private static final String FORMAT_EVENT = "Event=%s"; |
| 40 | |
| 41 | @Override |
| 42 | protected void execute() { |
| 43 | IntentMetricsService service = get(IntentMetricsService.class); |
| 44 | |
| 45 | if (outputJson()) { |
| 46 | print("%s", json(service.getEvents())); |
| 47 | } else { |
| 48 | for (IntentEvent event : service.getEvents()) { |
| 49 | print(FORMAT_EVENT, event); |
| 50 | print(""); // Extra empty line for clarity |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Produces a JSON array of intent events. |
| 57 | * |
| 58 | * @param intentEvents the intent events with the data |
| 59 | * @return JSON array with the intent events |
| 60 | */ |
| 61 | private JsonNode json(List<IntentEvent> intentEvents) { |
| 62 | ObjectMapper mapper = new ObjectMapper(); |
| 63 | ArrayNode result = mapper.createArrayNode(); |
| 64 | |
| 65 | for (IntentEvent event : intentEvents) { |
| 66 | result.add(json(mapper, event)); |
| 67 | } |
| 68 | return result; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Produces JSON object for a intent event. |
| 73 | * |
| 74 | * @param mapper the JSON object mapper to use |
| 75 | * @param intentEvent the intent event with the data |
| 76 | * @return JSON object for the intent event |
| 77 | */ |
| 78 | private ObjectNode json(ObjectMapper mapper, IntentEvent intentEvent) { |
| 79 | ObjectNode result = mapper.createObjectNode(); |
| 80 | |
| 81 | result.put("time", intentEvent.time()) |
| 82 | .put("type", intentEvent.type().toString()) |
| 83 | .put("event", intentEvent.toString()); |
| 84 | return result; |
| 85 | } |
| 86 | } |