blob: 10b46cb9c5ace6766450ef1a38026eddf99dbb09 [file] [log] [blame]
Thomas Vachuska7d693f52014-10-21 19:17:57 -07001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
tomf5c9d922014-10-03 15:22:03 -070019package org.onlab.onos.cli.net;
20
Thomas Vachuska6ce73042014-10-21 10:01:49 -070021import com.fasterxml.jackson.databind.JsonNode;
22import com.fasterxml.jackson.databind.ObjectMapper;
23import com.fasterxml.jackson.databind.node.ArrayNode;
24import com.fasterxml.jackson.databind.node.ObjectNode;
tomf5c9d922014-10-03 15:22:03 -070025import org.apache.karaf.shell.commands.Command;
26import org.onlab.onos.cli.AbstractShellCommand;
Thomas Vachuska6ce73042014-10-21 10:01:49 -070027import org.onlab.onos.net.ConnectPoint;
28import org.onlab.onos.net.Link;
29import org.onlab.onos.net.NetworkResource;
30import org.onlab.onos.net.intent.ConnectivityIntent;
tomf5c9d922014-10-03 15:22:03 -070031import org.onlab.onos.net.intent.Intent;
32import org.onlab.onos.net.intent.IntentService;
Brian O'Connora4cab072014-10-03 18:46:39 -070033import org.onlab.onos.net.intent.IntentState;
Thomas Vachuska6ce73042014-10-21 10:01:49 -070034import org.onlab.onos.net.intent.LinkCollectionIntent;
35import org.onlab.onos.net.intent.MultiPointToSinglePointIntent;
36import org.onlab.onos.net.intent.PathIntent;
37import org.onlab.onos.net.intent.PointToPointIntent;
38import org.onlab.onos.net.intent.SinglePointToMultiPointIntent;
39
Thomas Vachuska10d4abc2014-10-21 12:47:26 -070040import java.util.List;
Thomas Vachuska6ce73042014-10-21 10:01:49 -070041import java.util.Set;
tomf5c9d922014-10-03 15:22:03 -070042
43/**
44 * Lists the inventory of intents and their states.
45 */
46@Command(scope = "onos", name = "intents",
47 description = "Lists the inventory of intents and their states")
48public class IntentsListCommand extends AbstractShellCommand {
49
50 @Override
51 protected void execute() {
52 IntentService service = get(IntentService.class);
Thomas Vachuska6ce73042014-10-21 10:01:49 -070053 if (outputJson()) {
54 print("%s", json(service, service.getIntents()));
55 } else {
56 for (Intent intent : service.getIntents()) {
57 IntentState state = service.getIntentState(intent.id());
58 print("id=%s, state=%s, type=%s, appId=%s",
59 intent.id(), state, intent.getClass().getSimpleName(),
60 intent.appId().name());
Thomas Vachuska10d4abc2014-10-21 12:47:26 -070061 printDetails(service, intent);
Thomas Vachuska6ce73042014-10-21 10:01:49 -070062 }
tomf5c9d922014-10-03 15:22:03 -070063 }
64 }
65
Thomas Vachuska10d4abc2014-10-21 12:47:26 -070066 private void printDetails(IntentService service, Intent intent) {
Thomas Vachuska6ce73042014-10-21 10:01:49 -070067 if (intent.resources() != null && !intent.resources().isEmpty()) {
68 print(" resources=%s", intent.resources());
69 }
70 if (intent instanceof ConnectivityIntent) {
71 ConnectivityIntent ci = (ConnectivityIntent) intent;
Thomas Vachuska10d4abc2014-10-21 12:47:26 -070072 if (!ci.selector().criteria().isEmpty()) {
73 print(" selector=%s", ci.selector().criteria());
74 }
75 if (!ci.treatment().instructions().isEmpty()) {
76 print(" treatment=%s", ci.treatment().instructions());
77 }
Thomas Vachuska6ce73042014-10-21 10:01:49 -070078 }
79
80 if (intent instanceof PointToPointIntent) {
81 PointToPointIntent pi = (PointToPointIntent) intent;
82 print(" ingress=%s, egress=%s", pi.ingressPoint(), pi.egressPoint());
83 } else if (intent instanceof MultiPointToSinglePointIntent) {
84 MultiPointToSinglePointIntent pi = (MultiPointToSinglePointIntent) intent;
85 print(" ingress=%s, egress=%s", pi.ingressPoints(), pi.egressPoint());
86 } else if (intent instanceof SinglePointToMultiPointIntent) {
87 SinglePointToMultiPointIntent pi = (SinglePointToMultiPointIntent) intent;
88 print(" ingress=%s, egress=%s", pi.ingressPoint(), pi.egressPoints());
89 } else if (intent instanceof PathIntent) {
90 PathIntent pi = (PathIntent) intent;
91 print(" path=%s, cost=%d", pi.path().links(), pi.path().cost());
92 } else if (intent instanceof LinkCollectionIntent) {
93 LinkCollectionIntent li = (LinkCollectionIntent) intent;
94 print(" links=%s", li.links());
95 print(" egress=%s", li.egressPoint());
96 }
Thomas Vachuska10d4abc2014-10-21 12:47:26 -070097
98 List<Intent> installable = service.getInstallableIntents(intent.id());
99 if (installable != null && !installable.isEmpty()) {
100 print(" installable=%s", installable);
101 }
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700102 }
103
104 // Produces JSON array of the specified intents.
105 private JsonNode json(IntentService service, Iterable<Intent> intents) {
106 ObjectMapper mapper = new ObjectMapper();
107 ArrayNode result = mapper.createArrayNode();
108 for (Intent intent : intents) {
109 result.add(json(service, mapper, intent));
110 }
111 return result;
112 }
113
114 private JsonNode json(IntentService service, ObjectMapper mapper, Intent intent) {
115 ObjectNode result = mapper.createObjectNode()
116 .put("id", intent.id().toString())
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700117 .put("type", intent.getClass().getSimpleName())
118 .put("appId", intent.appId().name());
119
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700120 IntentState state = service.getIntentState(intent.id());
121 if (state != null) {
122 result.put("state", state.toString());
123 }
124
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700125 if (intent.resources() != null && !intent.resources().isEmpty()) {
126 ArrayNode rnode = mapper.createArrayNode();
127 for (NetworkResource resource : intent.resources()) {
128 rnode.add(resource.toString());
129 }
130 result.set("resources", rnode);
131 }
132
133 if (intent instanceof ConnectivityIntent) {
134 ConnectivityIntent ci = (ConnectivityIntent) intent;
135 if (!ci.selector().criteria().isEmpty()) {
136 result.put("selector", ci.selector().criteria().toString());
137 }
138 if (!ci.treatment().instructions().isEmpty()) {
139 result.put("treatment", ci.treatment().instructions().toString());
140 }
141 }
142
143 if (intent instanceof PathIntent) {
144 PathIntent pi = (PathIntent) intent;
145 ArrayNode pnode = mapper.createArrayNode();
146 for (Link link : pi.path().links()) {
147 pnode.add(link.toString());
148 }
149 result.set("path", pnode);
150
151 } else if (intent instanceof PointToPointIntent) {
152 PointToPointIntent pi = (PointToPointIntent) intent;
153 result.set("ingress", LinksListCommand.json(mapper, pi.ingressPoint()));
154 result.set("egress", LinksListCommand.json(mapper, pi.egressPoint()));
155
156 } else if (intent instanceof MultiPointToSinglePointIntent) {
157 MultiPointToSinglePointIntent pi = (MultiPointToSinglePointIntent) intent;
158 result.set("ingress", json(mapper, pi.ingressPoints()));
159 result.set("egress", LinksListCommand.json(mapper, pi.egressPoint()));
160
161 } else if (intent instanceof SinglePointToMultiPointIntent) {
162 SinglePointToMultiPointIntent pi = (SinglePointToMultiPointIntent) intent;
163 result.set("ingress", LinksListCommand.json(mapper, pi.ingressPoint()));
164 result.set("egress", json(mapper, pi.egressPoints()));
165
166 } else if (intent instanceof LinkCollectionIntent) {
167 LinkCollectionIntent li = (LinkCollectionIntent) intent;
168 result.set("links", LinksListCommand.json(li.links()));
169 }
170
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700171 List<Intent> installable = service.getInstallableIntents(intent.id());
172 if (installable != null && !installable.isEmpty()) {
173 result.set("installable", json(service, installable));
174 }
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700175 return result;
176 }
177
178 private JsonNode json(ObjectMapper mapper, Set<ConnectPoint> connectPoints) {
179 ArrayNode result = mapper.createArrayNode();
180 for (ConnectPoint cp : connectPoints) {
181 result.add(LinksListCommand.json(mapper, cp));
182 }
183 return result;
184 }
185
tomf5c9d922014-10-03 15:22:03 -0700186}