blob: e18cc5e4c23a05ab6273ea15f585756f2b5427fc [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 Vachuskaedc944c2014-11-04 15:42:25 -080081 if (ci.constraints() != null && !ci.constraints().isEmpty()) {
82 print(" constraints=%s", ci.constraints());
83 }
Thomas Vachuska6ce73042014-10-21 10:01:49 -070084 }
85
86 if (intent instanceof PointToPointIntent) {
87 PointToPointIntent pi = (PointToPointIntent) intent;
88 print(" ingress=%s, egress=%s", pi.ingressPoint(), pi.egressPoint());
89 } else if (intent instanceof MultiPointToSinglePointIntent) {
90 MultiPointToSinglePointIntent pi = (MultiPointToSinglePointIntent) intent;
91 print(" ingress=%s, egress=%s", pi.ingressPoints(), pi.egressPoint());
92 } else if (intent instanceof SinglePointToMultiPointIntent) {
93 SinglePointToMultiPointIntent pi = (SinglePointToMultiPointIntent) intent;
94 print(" ingress=%s, egress=%s", pi.ingressPoint(), pi.egressPoints());
95 } else if (intent instanceof PathIntent) {
96 PathIntent pi = (PathIntent) intent;
97 print(" path=%s, cost=%d", pi.path().links(), pi.path().cost());
98 } else if (intent instanceof LinkCollectionIntent) {
99 LinkCollectionIntent li = (LinkCollectionIntent) intent;
100 print(" links=%s", li.links());
101 print(" egress=%s", li.egressPoint());
102 }
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700103
104 List<Intent> installable = service.getInstallableIntents(intent.id());
Brian O'Connorfe0f4b12014-10-30 21:19:02 -0700105 if (showInstallable && installable != null && !installable.isEmpty()) {
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700106 print(" installable=%s", installable);
107 }
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700108 }
109
110 // Produces JSON array of the specified intents.
111 private JsonNode json(IntentService service, Iterable<Intent> intents) {
112 ObjectMapper mapper = new ObjectMapper();
113 ArrayNode result = mapper.createArrayNode();
114 for (Intent intent : intents) {
115 result.add(json(service, mapper, intent));
116 }
117 return result;
118 }
119
120 private JsonNode json(IntentService service, ObjectMapper mapper, Intent intent) {
121 ObjectNode result = mapper.createObjectNode()
122 .put("id", intent.id().toString())
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700123 .put("type", intent.getClass().getSimpleName())
124 .put("appId", intent.appId().name());
125
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700126 IntentState state = service.getIntentState(intent.id());
127 if (state != null) {
128 result.put("state", state.toString());
129 }
130
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700131 if (intent.resources() != null && !intent.resources().isEmpty()) {
132 ArrayNode rnode = mapper.createArrayNode();
133 for (NetworkResource resource : intent.resources()) {
134 rnode.add(resource.toString());
135 }
136 result.set("resources", rnode);
137 }
138
139 if (intent instanceof ConnectivityIntent) {
140 ConnectivityIntent ci = (ConnectivityIntent) intent;
141 if (!ci.selector().criteria().isEmpty()) {
142 result.put("selector", ci.selector().criteria().toString());
143 }
144 if (!ci.treatment().instructions().isEmpty()) {
145 result.put("treatment", ci.treatment().instructions().toString());
146 }
147 }
148
149 if (intent instanceof PathIntent) {
150 PathIntent pi = (PathIntent) intent;
151 ArrayNode pnode = mapper.createArrayNode();
152 for (Link link : pi.path().links()) {
153 pnode.add(link.toString());
154 }
155 result.set("path", pnode);
156
157 } else if (intent instanceof PointToPointIntent) {
158 PointToPointIntent pi = (PointToPointIntent) intent;
159 result.set("ingress", LinksListCommand.json(mapper, pi.ingressPoint()));
160 result.set("egress", LinksListCommand.json(mapper, pi.egressPoint()));
161
162 } else if (intent instanceof MultiPointToSinglePointIntent) {
163 MultiPointToSinglePointIntent pi = (MultiPointToSinglePointIntent) intent;
164 result.set("ingress", json(mapper, pi.ingressPoints()));
165 result.set("egress", LinksListCommand.json(mapper, pi.egressPoint()));
166
167 } else if (intent instanceof SinglePointToMultiPointIntent) {
168 SinglePointToMultiPointIntent pi = (SinglePointToMultiPointIntent) intent;
169 result.set("ingress", LinksListCommand.json(mapper, pi.ingressPoint()));
170 result.set("egress", json(mapper, pi.egressPoints()));
171
172 } else if (intent instanceof LinkCollectionIntent) {
173 LinkCollectionIntent li = (LinkCollectionIntent) intent;
174 result.set("links", LinksListCommand.json(li.links()));
175 }
176
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700177 List<Intent> installable = service.getInstallableIntents(intent.id());
178 if (installable != null && !installable.isEmpty()) {
179 result.set("installable", json(service, installable));
180 }
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700181 return result;
182 }
183
184 private JsonNode json(ObjectMapper mapper, Set<ConnectPoint> connectPoints) {
185 ArrayNode result = mapper.createArrayNode();
186 for (ConnectPoint cp : connectPoints) {
187 result.add(LinksListCommand.json(mapper, cp));
188 }
189 return result;
190 }
191
tomf5c9d922014-10-03 15:22:03 -0700192}