blob: 88548a10e34d0e13f91a7ad45f0b32713d0db3bb [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;
23import org.onlab.onos.cli.AbstractShellCommand;
Thomas Vachuska6ce73042014-10-21 10:01:49 -070024import org.onlab.onos.net.ConnectPoint;
25import org.onlab.onos.net.Link;
26import org.onlab.onos.net.NetworkResource;
27import org.onlab.onos.net.intent.ConnectivityIntent;
tomf5c9d922014-10-03 15:22:03 -070028import org.onlab.onos.net.intent.Intent;
29import org.onlab.onos.net.intent.IntentService;
Brian O'Connora4cab072014-10-03 18:46:39 -070030import org.onlab.onos.net.intent.IntentState;
Thomas Vachuska6ce73042014-10-21 10:01:49 -070031import org.onlab.onos.net.intent.LinkCollectionIntent;
32import org.onlab.onos.net.intent.MultiPointToSinglePointIntent;
33import org.onlab.onos.net.intent.PathIntent;
34import org.onlab.onos.net.intent.PointToPointIntent;
35import org.onlab.onos.net.intent.SinglePointToMultiPointIntent;
36
Thomas Vachuska10d4abc2014-10-21 12:47:26 -070037import java.util.List;
Thomas Vachuska6ce73042014-10-21 10:01:49 -070038import java.util.Set;
tomf5c9d922014-10-03 15:22:03 -070039
40/**
41 * Lists the inventory of intents and their states.
42 */
43@Command(scope = "onos", name = "intents",
44 description = "Lists the inventory of intents and their states")
45public class IntentsListCommand extends AbstractShellCommand {
46
47 @Override
48 protected void execute() {
49 IntentService service = get(IntentService.class);
Thomas Vachuska6ce73042014-10-21 10:01:49 -070050 if (outputJson()) {
51 print("%s", json(service, service.getIntents()));
52 } else {
53 for (Intent intent : service.getIntents()) {
54 IntentState state = service.getIntentState(intent.id());
55 print("id=%s, state=%s, type=%s, appId=%s",
56 intent.id(), state, intent.getClass().getSimpleName(),
57 intent.appId().name());
Thomas Vachuska10d4abc2014-10-21 12:47:26 -070058 printDetails(service, intent);
Thomas Vachuska6ce73042014-10-21 10:01:49 -070059 }
tomf5c9d922014-10-03 15:22:03 -070060 }
61 }
62
Thomas Vachuska10d4abc2014-10-21 12:47:26 -070063 private void printDetails(IntentService service, Intent intent) {
Thomas Vachuska6ce73042014-10-21 10:01:49 -070064 if (intent.resources() != null && !intent.resources().isEmpty()) {
65 print(" resources=%s", intent.resources());
66 }
67 if (intent instanceof ConnectivityIntent) {
68 ConnectivityIntent ci = (ConnectivityIntent) intent;
Thomas Vachuska10d4abc2014-10-21 12:47:26 -070069 if (!ci.selector().criteria().isEmpty()) {
70 print(" selector=%s", ci.selector().criteria());
71 }
72 if (!ci.treatment().instructions().isEmpty()) {
73 print(" treatment=%s", ci.treatment().instructions());
74 }
Thomas Vachuska6ce73042014-10-21 10:01:49 -070075 }
76
77 if (intent instanceof PointToPointIntent) {
78 PointToPointIntent pi = (PointToPointIntent) intent;
79 print(" ingress=%s, egress=%s", pi.ingressPoint(), pi.egressPoint());
80 } else if (intent instanceof MultiPointToSinglePointIntent) {
81 MultiPointToSinglePointIntent pi = (MultiPointToSinglePointIntent) intent;
82 print(" ingress=%s, egress=%s", pi.ingressPoints(), pi.egressPoint());
83 } else if (intent instanceof SinglePointToMultiPointIntent) {
84 SinglePointToMultiPointIntent pi = (SinglePointToMultiPointIntent) intent;
85 print(" ingress=%s, egress=%s", pi.ingressPoint(), pi.egressPoints());
86 } else if (intent instanceof PathIntent) {
87 PathIntent pi = (PathIntent) intent;
88 print(" path=%s, cost=%d", pi.path().links(), pi.path().cost());
89 } else if (intent instanceof LinkCollectionIntent) {
90 LinkCollectionIntent li = (LinkCollectionIntent) intent;
91 print(" links=%s", li.links());
92 print(" egress=%s", li.egressPoint());
93 }
Thomas Vachuska10d4abc2014-10-21 12:47:26 -070094
95 List<Intent> installable = service.getInstallableIntents(intent.id());
96 if (installable != null && !installable.isEmpty()) {
97 print(" installable=%s", installable);
98 }
Thomas Vachuska6ce73042014-10-21 10:01:49 -070099 }
100
101 // Produces JSON array of the specified intents.
102 private JsonNode json(IntentService service, Iterable<Intent> intents) {
103 ObjectMapper mapper = new ObjectMapper();
104 ArrayNode result = mapper.createArrayNode();
105 for (Intent intent : intents) {
106 result.add(json(service, mapper, intent));
107 }
108 return result;
109 }
110
111 private JsonNode json(IntentService service, ObjectMapper mapper, Intent intent) {
112 ObjectNode result = mapper.createObjectNode()
113 .put("id", intent.id().toString())
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700114 .put("type", intent.getClass().getSimpleName())
115 .put("appId", intent.appId().name());
116
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700117 IntentState state = service.getIntentState(intent.id());
118 if (state != null) {
119 result.put("state", state.toString());
120 }
121
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700122 if (intent.resources() != null && !intent.resources().isEmpty()) {
123 ArrayNode rnode = mapper.createArrayNode();
124 for (NetworkResource resource : intent.resources()) {
125 rnode.add(resource.toString());
126 }
127 result.set("resources", rnode);
128 }
129
130 if (intent instanceof ConnectivityIntent) {
131 ConnectivityIntent ci = (ConnectivityIntent) intent;
132 if (!ci.selector().criteria().isEmpty()) {
133 result.put("selector", ci.selector().criteria().toString());
134 }
135 if (!ci.treatment().instructions().isEmpty()) {
136 result.put("treatment", ci.treatment().instructions().toString());
137 }
138 }
139
140 if (intent instanceof PathIntent) {
141 PathIntent pi = (PathIntent) intent;
142 ArrayNode pnode = mapper.createArrayNode();
143 for (Link link : pi.path().links()) {
144 pnode.add(link.toString());
145 }
146 result.set("path", pnode);
147
148 } else if (intent instanceof PointToPointIntent) {
149 PointToPointIntent pi = (PointToPointIntent) intent;
150 result.set("ingress", LinksListCommand.json(mapper, pi.ingressPoint()));
151 result.set("egress", LinksListCommand.json(mapper, pi.egressPoint()));
152
153 } else if (intent instanceof MultiPointToSinglePointIntent) {
154 MultiPointToSinglePointIntent pi = (MultiPointToSinglePointIntent) intent;
155 result.set("ingress", json(mapper, pi.ingressPoints()));
156 result.set("egress", LinksListCommand.json(mapper, pi.egressPoint()));
157
158 } else if (intent instanceof SinglePointToMultiPointIntent) {
159 SinglePointToMultiPointIntent pi = (SinglePointToMultiPointIntent) intent;
160 result.set("ingress", LinksListCommand.json(mapper, pi.ingressPoint()));
161 result.set("egress", json(mapper, pi.egressPoints()));
162
163 } else if (intent instanceof LinkCollectionIntent) {
164 LinkCollectionIntent li = (LinkCollectionIntent) intent;
165 result.set("links", LinksListCommand.json(li.links()));
166 }
167
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700168 List<Intent> installable = service.getInstallableIntents(intent.id());
169 if (installable != null && !installable.isEmpty()) {
170 result.set("installable", json(service, installable));
171 }
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700172 return result;
173 }
174
175 private JsonNode json(ObjectMapper mapper, Set<ConnectPoint> connectPoints) {
176 ArrayNode result = mapper.createArrayNode();
177 for (ConnectPoint cp : connectPoints) {
178 result.add(LinksListCommand.json(mapper, cp));
179 }
180 return result;
181 }
182
tomf5c9d922014-10-03 15:22:03 -0700183}