blob: f5c65e5f55312957e0f386b61c5933d60d964c62 [file] [log] [blame]
Thomas Vachuska7d693f52014-10-21 19:17:57 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska7d693f52014-10-21 19:17:57 -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 Vachuska7d693f52014-10-21 19:17:57 -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 Vachuska7d693f52014-10-21 19:17:57 -070015 */
tomf5c9d922014-10-03 15:22:03 -070016package org.onlab.onos.cli.net;
17
Thomas Vachuska6ce73042014-10-21 10:01:49 -070018import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.ObjectMapper;
20import com.fasterxml.jackson.databind.node.ArrayNode;
21import com.fasterxml.jackson.databind.node.ObjectNode;
tomf5c9d922014-10-03 15:22:03 -070022import org.apache.karaf.shell.commands.Command;
Brian O'Connorfe0f4b12014-10-30 21:19:02 -070023import org.apache.karaf.shell.commands.Option;
tomf5c9d922014-10-03 15:22:03 -070024import org.onlab.onos.cli.AbstractShellCommand;
Thomas Vachuska6ce73042014-10-21 10:01:49 -070025import org.onlab.onos.net.ConnectPoint;
26import org.onlab.onos.net.Link;
27import org.onlab.onos.net.NetworkResource;
28import org.onlab.onos.net.intent.ConnectivityIntent;
tomf5c9d922014-10-03 15:22:03 -070029import org.onlab.onos.net.intent.Intent;
30import org.onlab.onos.net.intent.IntentService;
Brian O'Connora4cab072014-10-03 18:46:39 -070031import org.onlab.onos.net.intent.IntentState;
Thomas Vachuska6ce73042014-10-21 10:01:49 -070032import org.onlab.onos.net.intent.LinkCollectionIntent;
33import org.onlab.onos.net.intent.MultiPointToSinglePointIntent;
34import org.onlab.onos.net.intent.PathIntent;
35import org.onlab.onos.net.intent.PointToPointIntent;
36import org.onlab.onos.net.intent.SinglePointToMultiPointIntent;
37
Thomas Vachuska10d4abc2014-10-21 12:47:26 -070038import java.util.List;
Thomas Vachuska6ce73042014-10-21 10:01:49 -070039import java.util.Set;
tomf5c9d922014-10-03 15:22:03 -070040
41/**
42 * Lists the inventory of intents and their states.
43 */
44@Command(scope = "onos", name = "intents",
45 description = "Lists the inventory of intents and their states")
46public class IntentsListCommand extends AbstractShellCommand {
47
Brian O'Connorfe0f4b12014-10-30 21:19:02 -070048 @Option(name = "-i", aliases = "--installable", description = "Output Installable Intents",
49 required = false, multiValued = false)
50 private boolean showInstallable = false;
51
52
tomf5c9d922014-10-03 15:22:03 -070053 @Override
54 protected void execute() {
55 IntentService service = get(IntentService.class);
Thomas Vachuska6ce73042014-10-21 10:01:49 -070056 if (outputJson()) {
57 print("%s", json(service, service.getIntents()));
58 } else {
59 for (Intent intent : service.getIntents()) {
60 IntentState state = service.getIntentState(intent.id());
61 print("id=%s, state=%s, type=%s, appId=%s",
62 intent.id(), state, intent.getClass().getSimpleName(),
63 intent.appId().name());
Thomas Vachuska10d4abc2014-10-21 12:47:26 -070064 printDetails(service, intent);
Thomas Vachuska6ce73042014-10-21 10:01:49 -070065 }
tomf5c9d922014-10-03 15:22:03 -070066 }
67 }
68
Thomas Vachuska10d4abc2014-10-21 12:47:26 -070069 private void printDetails(IntentService service, Intent intent) {
Thomas Vachuska6ce73042014-10-21 10:01:49 -070070 if (intent.resources() != null && !intent.resources().isEmpty()) {
71 print(" resources=%s", intent.resources());
72 }
73 if (intent instanceof ConnectivityIntent) {
74 ConnectivityIntent ci = (ConnectivityIntent) intent;
Thomas Vachuska10d4abc2014-10-21 12:47:26 -070075 if (!ci.selector().criteria().isEmpty()) {
76 print(" selector=%s", ci.selector().criteria());
77 }
78 if (!ci.treatment().instructions().isEmpty()) {
79 print(" treatment=%s", ci.treatment().instructions());
80 }
Thomas Vachuska6ce73042014-10-21 10:01:49 -070081 }
82
83 if (intent instanceof PointToPointIntent) {
84 PointToPointIntent pi = (PointToPointIntent) intent;
85 print(" ingress=%s, egress=%s", pi.ingressPoint(), pi.egressPoint());
86 } else if (intent instanceof MultiPointToSinglePointIntent) {
87 MultiPointToSinglePointIntent pi = (MultiPointToSinglePointIntent) intent;
88 print(" ingress=%s, egress=%s", pi.ingressPoints(), pi.egressPoint());
89 } else if (intent instanceof SinglePointToMultiPointIntent) {
90 SinglePointToMultiPointIntent pi = (SinglePointToMultiPointIntent) intent;
91 print(" ingress=%s, egress=%s", pi.ingressPoint(), pi.egressPoints());
92 } else if (intent instanceof PathIntent) {
93 PathIntent pi = (PathIntent) intent;
94 print(" path=%s, cost=%d", pi.path().links(), pi.path().cost());
95 } else if (intent instanceof LinkCollectionIntent) {
96 LinkCollectionIntent li = (LinkCollectionIntent) intent;
97 print(" links=%s", li.links());
98 print(" egress=%s", li.egressPoint());
99 }
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700100
101 List<Intent> installable = service.getInstallableIntents(intent.id());
Brian O'Connorfe0f4b12014-10-30 21:19:02 -0700102 if (showInstallable && installable != null && !installable.isEmpty()) {
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700103 print(" installable=%s", installable);
104 }
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700105 }
106
107 // Produces JSON array of the specified intents.
108 private JsonNode json(IntentService service, Iterable<Intent> intents) {
109 ObjectMapper mapper = new ObjectMapper();
110 ArrayNode result = mapper.createArrayNode();
111 for (Intent intent : intents) {
112 result.add(json(service, mapper, intent));
113 }
114 return result;
115 }
116
117 private JsonNode json(IntentService service, ObjectMapper mapper, Intent intent) {
118 ObjectNode result = mapper.createObjectNode()
119 .put("id", intent.id().toString())
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700120 .put("type", intent.getClass().getSimpleName())
121 .put("appId", intent.appId().name());
122
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700123 IntentState state = service.getIntentState(intent.id());
124 if (state != null) {
125 result.put("state", state.toString());
126 }
127
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700128 if (intent.resources() != null && !intent.resources().isEmpty()) {
129 ArrayNode rnode = mapper.createArrayNode();
130 for (NetworkResource resource : intent.resources()) {
131 rnode.add(resource.toString());
132 }
133 result.set("resources", rnode);
134 }
135
136 if (intent instanceof ConnectivityIntent) {
137 ConnectivityIntent ci = (ConnectivityIntent) intent;
138 if (!ci.selector().criteria().isEmpty()) {
139 result.put("selector", ci.selector().criteria().toString());
140 }
141 if (!ci.treatment().instructions().isEmpty()) {
142 result.put("treatment", ci.treatment().instructions().toString());
143 }
144 }
145
146 if (intent instanceof PathIntent) {
147 PathIntent pi = (PathIntent) intent;
148 ArrayNode pnode = mapper.createArrayNode();
149 for (Link link : pi.path().links()) {
150 pnode.add(link.toString());
151 }
152 result.set("path", pnode);
153
154 } else if (intent instanceof PointToPointIntent) {
155 PointToPointIntent pi = (PointToPointIntent) intent;
156 result.set("ingress", LinksListCommand.json(mapper, pi.ingressPoint()));
157 result.set("egress", LinksListCommand.json(mapper, pi.egressPoint()));
158
159 } else if (intent instanceof MultiPointToSinglePointIntent) {
160 MultiPointToSinglePointIntent pi = (MultiPointToSinglePointIntent) intent;
161 result.set("ingress", json(mapper, pi.ingressPoints()));
162 result.set("egress", LinksListCommand.json(mapper, pi.egressPoint()));
163
164 } else if (intent instanceof SinglePointToMultiPointIntent) {
165 SinglePointToMultiPointIntent pi = (SinglePointToMultiPointIntent) intent;
166 result.set("ingress", LinksListCommand.json(mapper, pi.ingressPoint()));
167 result.set("egress", json(mapper, pi.egressPoints()));
168
169 } else if (intent instanceof LinkCollectionIntent) {
170 LinkCollectionIntent li = (LinkCollectionIntent) intent;
171 result.set("links", LinksListCommand.json(li.links()));
172 }
173
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700174 List<Intent> installable = service.getInstallableIntents(intent.id());
175 if (installable != null && !installable.isEmpty()) {
176 result.set("installable", json(service, installable));
177 }
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700178 return result;
179 }
180
181 private JsonNode json(ObjectMapper mapper, Set<ConnectPoint> connectPoints) {
182 ArrayNode result = mapper.createArrayNode();
183 for (ConnectPoint cp : connectPoints) {
184 result.add(LinksListCommand.json(mapper, cp));
185 }
186 return result;
187 }
188
tomf5c9d922014-10-03 15:22:03 -0700189}