Thomas Vachuska

Adding JSON output for intent list command.

1 package org.onlab.onos.cli.net; 1 package org.onlab.onos.cli.net;
2 2
3 +import com.fasterxml.jackson.databind.JsonNode;
4 +import com.fasterxml.jackson.databind.ObjectMapper;
5 +import com.fasterxml.jackson.databind.node.ArrayNode;
6 +import com.fasterxml.jackson.databind.node.ObjectNode;
3 import org.apache.karaf.shell.commands.Command; 7 import org.apache.karaf.shell.commands.Command;
4 import org.onlab.onos.cli.AbstractShellCommand; 8 import org.onlab.onos.cli.AbstractShellCommand;
9 +import org.onlab.onos.net.ConnectPoint;
10 +import org.onlab.onos.net.Link;
11 +import org.onlab.onos.net.NetworkResource;
12 +import org.onlab.onos.net.intent.ConnectivityIntent;
5 import org.onlab.onos.net.intent.Intent; 13 import org.onlab.onos.net.intent.Intent;
6 import org.onlab.onos.net.intent.IntentService; 14 import org.onlab.onos.net.intent.IntentService;
7 import org.onlab.onos.net.intent.IntentState; 15 import org.onlab.onos.net.intent.IntentState;
16 +import org.onlab.onos.net.intent.LinkCollectionIntent;
17 +import org.onlab.onos.net.intent.MultiPointToSinglePointIntent;
18 +import org.onlab.onos.net.intent.PathIntent;
19 +import org.onlab.onos.net.intent.PointToPointIntent;
20 +import org.onlab.onos.net.intent.SinglePointToMultiPointIntent;
21 +
22 +import java.util.Set;
8 23
9 /** 24 /**
10 * Lists the inventory of intents and their states. 25 * Lists the inventory of intents and their states.
...@@ -16,11 +31,120 @@ public class IntentsListCommand extends AbstractShellCommand { ...@@ -16,11 +31,120 @@ public class IntentsListCommand extends AbstractShellCommand {
16 @Override 31 @Override
17 protected void execute() { 32 protected void execute() {
18 IntentService service = get(IntentService.class); 33 IntentService service = get(IntentService.class);
34 + if (outputJson()) {
35 + print("%s", json(service, service.getIntents()));
36 + } else {
19 for (Intent intent : service.getIntents()) { 37 for (Intent intent : service.getIntents()) {
20 IntentState state = service.getIntentState(intent.id()); 38 IntentState state = service.getIntentState(intent.id());
21 - print("id=%s, state=%s, appId=%s, %s", 39 + print("id=%s, state=%s, type=%s, appId=%s",
22 - intent.id(), state, intent.appId().name(), intent); 40 + intent.id(), state, intent.getClass().getSimpleName(),
41 + intent.appId().name());
42 + printDetails(intent);
43 + }
44 + }
45 + }
46 +
47 + private void printDetails(Intent intent) {
48 + if (intent.resources() != null && !intent.resources().isEmpty()) {
49 + print(" resources=%s", intent.resources());
50 + }
51 + if (intent instanceof ConnectivityIntent) {
52 + ConnectivityIntent ci = (ConnectivityIntent) intent;
53 + print(" selector=%s", ci.selector().criteria());
54 + print(" treatment=%s", ci.treatment().instructions());
55 + }
56 +
57 + if (intent instanceof PointToPointIntent) {
58 + PointToPointIntent pi = (PointToPointIntent) intent;
59 + print(" ingress=%s, egress=%s", pi.ingressPoint(), pi.egressPoint());
60 + } else if (intent instanceof MultiPointToSinglePointIntent) {
61 + MultiPointToSinglePointIntent pi = (MultiPointToSinglePointIntent) intent;
62 + print(" ingress=%s, egress=%s", pi.ingressPoints(), pi.egressPoint());
63 + } else if (intent instanceof SinglePointToMultiPointIntent) {
64 + SinglePointToMultiPointIntent pi = (SinglePointToMultiPointIntent) intent;
65 + print(" ingress=%s, egress=%s", pi.ingressPoint(), pi.egressPoints());
66 + } else if (intent instanceof PathIntent) {
67 + PathIntent pi = (PathIntent) intent;
68 + print(" path=%s, cost=%d", pi.path().links(), pi.path().cost());
69 + } else if (intent instanceof LinkCollectionIntent) {
70 + LinkCollectionIntent li = (LinkCollectionIntent) intent;
71 + print(" links=%s", li.links());
72 + print(" egress=%s", li.egressPoint());
73 + }
74 + }
75 +
76 + // Produces JSON array of the specified intents.
77 + private JsonNode json(IntentService service, Iterable<Intent> intents) {
78 + ObjectMapper mapper = new ObjectMapper();
79 + ArrayNode result = mapper.createArrayNode();
80 + for (Intent intent : intents) {
81 + result.add(json(service, mapper, intent));
82 + }
83 + return result;
84 + }
85 +
86 + private JsonNode json(IntentService service, ObjectMapper mapper, Intent intent) {
87 + ObjectNode result = mapper.createObjectNode()
88 + .put("id", intent.id().toString())
89 + .put("state", service.getIntentState(intent.id()).toString())
90 + .put("type", intent.getClass().getSimpleName())
91 + .put("appId", intent.appId().name());
92 +
93 + if (intent.resources() != null && !intent.resources().isEmpty()) {
94 + ArrayNode rnode = mapper.createArrayNode();
95 + for (NetworkResource resource : intent.resources()) {
96 + rnode.add(resource.toString());
97 + }
98 + result.set("resources", rnode);
99 + }
100 +
101 + if (intent instanceof ConnectivityIntent) {
102 + ConnectivityIntent ci = (ConnectivityIntent) intent;
103 + if (!ci.selector().criteria().isEmpty()) {
104 + result.put("selector", ci.selector().criteria().toString());
105 + }
106 + if (!ci.treatment().instructions().isEmpty()) {
107 + result.put("treatment", ci.treatment().instructions().toString());
108 + }
109 + }
110 +
111 + if (intent instanceof PathIntent) {
112 + PathIntent pi = (PathIntent) intent;
113 + ArrayNode pnode = mapper.createArrayNode();
114 + for (Link link : pi.path().links()) {
115 + pnode.add(link.toString());
116 + }
117 + result.set("path", pnode);
118 +
119 + } else if (intent instanceof PointToPointIntent) {
120 + PointToPointIntent pi = (PointToPointIntent) intent;
121 + result.set("ingress", LinksListCommand.json(mapper, pi.ingressPoint()));
122 + result.set("egress", LinksListCommand.json(mapper, pi.egressPoint()));
123 +
124 + } else if (intent instanceof MultiPointToSinglePointIntent) {
125 + MultiPointToSinglePointIntent pi = (MultiPointToSinglePointIntent) intent;
126 + result.set("ingress", json(mapper, pi.ingressPoints()));
127 + result.set("egress", LinksListCommand.json(mapper, pi.egressPoint()));
128 +
129 + } else if (intent instanceof SinglePointToMultiPointIntent) {
130 + SinglePointToMultiPointIntent pi = (SinglePointToMultiPointIntent) intent;
131 + result.set("ingress", LinksListCommand.json(mapper, pi.ingressPoint()));
132 + result.set("egress", json(mapper, pi.egressPoints()));
133 +
134 + } else if (intent instanceof LinkCollectionIntent) {
135 + LinkCollectionIntent li = (LinkCollectionIntent) intent;
136 + result.set("links", LinksListCommand.json(li.links()));
137 + }
138 +
139 + return result;
140 + }
141 +
142 + private JsonNode json(ObjectMapper mapper, Set<ConnectPoint> connectPoints) {
143 + ArrayNode result = mapper.createArrayNode();
144 + for (ConnectPoint cp : connectPoints) {
145 + result.add(LinksListCommand.json(mapper, cp));
23 } 146 }
147 + return result;
24 } 148 }
25 149
26 } 150 }
......