blob: ab9fb83d9ef9dd564a20f24d5205f20c443844f7 [file] [log] [blame]
tomf5c9d922014-10-03 15:22:03 -07001package org.onlab.onos.cli.net;
2
Thomas Vachuska6ce73042014-10-21 10:01:49 -07003import com.fasterxml.jackson.databind.JsonNode;
4import com.fasterxml.jackson.databind.ObjectMapper;
5import com.fasterxml.jackson.databind.node.ArrayNode;
6import com.fasterxml.jackson.databind.node.ObjectNode;
tomf5c9d922014-10-03 15:22:03 -07007import org.apache.karaf.shell.commands.Command;
8import org.onlab.onos.cli.AbstractShellCommand;
Thomas Vachuska6ce73042014-10-21 10:01:49 -07009import org.onlab.onos.net.ConnectPoint;
10import org.onlab.onos.net.Link;
11import org.onlab.onos.net.NetworkResource;
12import org.onlab.onos.net.intent.ConnectivityIntent;
tomf5c9d922014-10-03 15:22:03 -070013import org.onlab.onos.net.intent.Intent;
14import org.onlab.onos.net.intent.IntentService;
Brian O'Connora4cab072014-10-03 18:46:39 -070015import org.onlab.onos.net.intent.IntentState;
Thomas Vachuska6ce73042014-10-21 10:01:49 -070016import org.onlab.onos.net.intent.LinkCollectionIntent;
17import org.onlab.onos.net.intent.MultiPointToSinglePointIntent;
18import org.onlab.onos.net.intent.PathIntent;
19import org.onlab.onos.net.intent.PointToPointIntent;
20import org.onlab.onos.net.intent.SinglePointToMultiPointIntent;
21
Thomas Vachuska10d4abc2014-10-21 12:47:26 -070022import java.util.List;
Thomas Vachuska6ce73042014-10-21 10:01:49 -070023import java.util.Set;
tomf5c9d922014-10-03 15:22:03 -070024
25/**
26 * Lists the inventory of intents and their states.
27 */
28@Command(scope = "onos", name = "intents",
29 description = "Lists the inventory of intents and their states")
30public class IntentsListCommand extends AbstractShellCommand {
31
32 @Override
33 protected void execute() {
34 IntentService service = get(IntentService.class);
Thomas Vachuska6ce73042014-10-21 10:01:49 -070035 if (outputJson()) {
36 print("%s", json(service, service.getIntents()));
37 } else {
38 for (Intent intent : service.getIntents()) {
39 IntentState state = service.getIntentState(intent.id());
40 print("id=%s, state=%s, type=%s, appId=%s",
41 intent.id(), state, intent.getClass().getSimpleName(),
42 intent.appId().name());
Thomas Vachuska10d4abc2014-10-21 12:47:26 -070043 printDetails(service, intent);
Thomas Vachuska6ce73042014-10-21 10:01:49 -070044 }
tomf5c9d922014-10-03 15:22:03 -070045 }
46 }
47
Thomas Vachuska10d4abc2014-10-21 12:47:26 -070048 private void printDetails(IntentService service, Intent intent) {
Thomas Vachuska6ce73042014-10-21 10:01:49 -070049 if (intent.resources() != null && !intent.resources().isEmpty()) {
50 print(" resources=%s", intent.resources());
51 }
52 if (intent instanceof ConnectivityIntent) {
53 ConnectivityIntent ci = (ConnectivityIntent) intent;
Thomas Vachuska10d4abc2014-10-21 12:47:26 -070054 if (!ci.selector().criteria().isEmpty()) {
55 print(" selector=%s", ci.selector().criteria());
56 }
57 if (!ci.treatment().instructions().isEmpty()) {
58 print(" treatment=%s", ci.treatment().instructions());
59 }
Thomas Vachuska6ce73042014-10-21 10:01:49 -070060 }
61
62 if (intent instanceof PointToPointIntent) {
63 PointToPointIntent pi = (PointToPointIntent) intent;
64 print(" ingress=%s, egress=%s", pi.ingressPoint(), pi.egressPoint());
65 } else if (intent instanceof MultiPointToSinglePointIntent) {
66 MultiPointToSinglePointIntent pi = (MultiPointToSinglePointIntent) intent;
67 print(" ingress=%s, egress=%s", pi.ingressPoints(), pi.egressPoint());
68 } else if (intent instanceof SinglePointToMultiPointIntent) {
69 SinglePointToMultiPointIntent pi = (SinglePointToMultiPointIntent) intent;
70 print(" ingress=%s, egress=%s", pi.ingressPoint(), pi.egressPoints());
71 } else if (intent instanceof PathIntent) {
72 PathIntent pi = (PathIntent) intent;
73 print(" path=%s, cost=%d", pi.path().links(), pi.path().cost());
74 } else if (intent instanceof LinkCollectionIntent) {
75 LinkCollectionIntent li = (LinkCollectionIntent) intent;
76 print(" links=%s", li.links());
77 print(" egress=%s", li.egressPoint());
78 }
Thomas Vachuska10d4abc2014-10-21 12:47:26 -070079
80 List<Intent> installable = service.getInstallableIntents(intent.id());
81 if (installable != null && !installable.isEmpty()) {
82 print(" installable=%s", installable);
83 }
Thomas Vachuska6ce73042014-10-21 10:01:49 -070084 }
85
86 // Produces JSON array of the specified intents.
87 private JsonNode json(IntentService service, Iterable<Intent> intents) {
88 ObjectMapper mapper = new ObjectMapper();
89 ArrayNode result = mapper.createArrayNode();
90 for (Intent intent : intents) {
91 result.add(json(service, mapper, intent));
92 }
93 return result;
94 }
95
96 private JsonNode json(IntentService service, ObjectMapper mapper, Intent intent) {
97 ObjectNode result = mapper.createObjectNode()
98 .put("id", intent.id().toString())
Thomas Vachuska6ce73042014-10-21 10:01:49 -070099 .put("type", intent.getClass().getSimpleName())
100 .put("appId", intent.appId().name());
101
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700102 IntentState state = service.getIntentState(intent.id());
103 if (state != null) {
104 result.put("state", state.toString());
105 }
106
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700107 if (intent.resources() != null && !intent.resources().isEmpty()) {
108 ArrayNode rnode = mapper.createArrayNode();
109 for (NetworkResource resource : intent.resources()) {
110 rnode.add(resource.toString());
111 }
112 result.set("resources", rnode);
113 }
114
115 if (intent instanceof ConnectivityIntent) {
116 ConnectivityIntent ci = (ConnectivityIntent) intent;
117 if (!ci.selector().criteria().isEmpty()) {
118 result.put("selector", ci.selector().criteria().toString());
119 }
120 if (!ci.treatment().instructions().isEmpty()) {
121 result.put("treatment", ci.treatment().instructions().toString());
122 }
123 }
124
125 if (intent instanceof PathIntent) {
126 PathIntent pi = (PathIntent) intent;
127 ArrayNode pnode = mapper.createArrayNode();
128 for (Link link : pi.path().links()) {
129 pnode.add(link.toString());
130 }
131 result.set("path", pnode);
132
133 } else if (intent instanceof PointToPointIntent) {
134 PointToPointIntent pi = (PointToPointIntent) intent;
135 result.set("ingress", LinksListCommand.json(mapper, pi.ingressPoint()));
136 result.set("egress", LinksListCommand.json(mapper, pi.egressPoint()));
137
138 } else if (intent instanceof MultiPointToSinglePointIntent) {
139 MultiPointToSinglePointIntent pi = (MultiPointToSinglePointIntent) intent;
140 result.set("ingress", json(mapper, pi.ingressPoints()));
141 result.set("egress", LinksListCommand.json(mapper, pi.egressPoint()));
142
143 } else if (intent instanceof SinglePointToMultiPointIntent) {
144 SinglePointToMultiPointIntent pi = (SinglePointToMultiPointIntent) intent;
145 result.set("ingress", LinksListCommand.json(mapper, pi.ingressPoint()));
146 result.set("egress", json(mapper, pi.egressPoints()));
147
148 } else if (intent instanceof LinkCollectionIntent) {
149 LinkCollectionIntent li = (LinkCollectionIntent) intent;
150 result.set("links", LinksListCommand.json(li.links()));
151 }
152
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700153 List<Intent> installable = service.getInstallableIntents(intent.id());
154 if (installable != null && !installable.isEmpty()) {
155 result.set("installable", json(service, installable));
156 }
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700157 return result;
158 }
159
160 private JsonNode json(ObjectMapper mapper, Set<ConnectPoint> connectPoints) {
161 ArrayNode result = mapper.createArrayNode();
162 for (ConnectPoint cp : connectPoints) {
163 result.add(LinksListCommand.json(mapper, cp));
164 }
165 return result;
166 }
167
tomf5c9d922014-10-03 15:22:03 -0700168}