Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * 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 Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * 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 Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame^] | 16 | package org.onosproject.metrics.intent.cli; |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 17 | |
| 18 | import java.util.List; |
| 19 | |
| 20 | import com.fasterxml.jackson.databind.JsonNode; |
| 21 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 22 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 23 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 24 | import org.apache.karaf.shell.commands.Command; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame^] | 25 | import org.onosproject.cli.AbstractShellCommand; |
| 26 | import org.onosproject.metrics.intent.IntentMetricsService; |
| 27 | import org.onosproject.net.intent.IntentEvent; |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 28 | |
| 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") |
| 34 | public 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 | } |