Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * 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 Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * 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 Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 15 | */ |
tom | f5c9d92 | 2014-10-03 15:22:03 -0700 | [diff] [blame] | 16 | package org.onlab.onos.cli.net; |
| 17 | |
Thomas Vachuska | 6ce7304 | 2014-10-21 10:01:49 -0700 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 21 | import com.fasterxml.jackson.databind.node.ObjectNode; |
tom | f5c9d92 | 2014-10-03 15:22:03 -0700 | [diff] [blame] | 22 | import org.apache.karaf.shell.commands.Command; |
Brian O'Connor | fe0f4b1 | 2014-10-30 21:19:02 -0700 | [diff] [blame] | 23 | import org.apache.karaf.shell.commands.Option; |
tom | f5c9d92 | 2014-10-03 15:22:03 -0700 | [diff] [blame] | 24 | import org.onlab.onos.cli.AbstractShellCommand; |
Thomas Vachuska | 6ce7304 | 2014-10-21 10:01:49 -0700 | [diff] [blame] | 25 | import org.onlab.onos.net.ConnectPoint; |
| 26 | import org.onlab.onos.net.Link; |
| 27 | import org.onlab.onos.net.NetworkResource; |
| 28 | import org.onlab.onos.net.intent.ConnectivityIntent; |
tom | f5c9d92 | 2014-10-03 15:22:03 -0700 | [diff] [blame] | 29 | import org.onlab.onos.net.intent.Intent; |
| 30 | import org.onlab.onos.net.intent.IntentService; |
Brian O'Connor | a4cab07 | 2014-10-03 18:46:39 -0700 | [diff] [blame] | 31 | import org.onlab.onos.net.intent.IntentState; |
Thomas Vachuska | 6ce7304 | 2014-10-21 10:01:49 -0700 | [diff] [blame] | 32 | import org.onlab.onos.net.intent.LinkCollectionIntent; |
| 33 | import org.onlab.onos.net.intent.MultiPointToSinglePointIntent; |
| 34 | import org.onlab.onos.net.intent.PathIntent; |
| 35 | import org.onlab.onos.net.intent.PointToPointIntent; |
| 36 | import org.onlab.onos.net.intent.SinglePointToMultiPointIntent; |
| 37 | |
Thomas Vachuska | 10d4abc | 2014-10-21 12:47:26 -0700 | [diff] [blame] | 38 | import java.util.List; |
Thomas Vachuska | 6ce7304 | 2014-10-21 10:01:49 -0700 | [diff] [blame] | 39 | import java.util.Set; |
tom | f5c9d92 | 2014-10-03 15:22:03 -0700 | [diff] [blame] | 40 | |
| 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") |
| 46 | public class IntentsListCommand extends AbstractShellCommand { |
| 47 | |
Brian O'Connor | fe0f4b1 | 2014-10-30 21:19:02 -0700 | [diff] [blame] | 48 | @Option(name = "-i", aliases = "--installable", description = "Output Installable Intents", |
| 49 | required = false, multiValued = false) |
| 50 | private boolean showInstallable = false; |
| 51 | |
| 52 | |
tom | f5c9d92 | 2014-10-03 15:22:03 -0700 | [diff] [blame] | 53 | @Override |
| 54 | protected void execute() { |
| 55 | IntentService service = get(IntentService.class); |
Thomas Vachuska | 6ce7304 | 2014-10-21 10:01:49 -0700 | [diff] [blame] | 56 | 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 Vachuska | 10d4abc | 2014-10-21 12:47:26 -0700 | [diff] [blame] | 64 | printDetails(service, intent); |
Thomas Vachuska | 6ce7304 | 2014-10-21 10:01:49 -0700 | [diff] [blame] | 65 | } |
tom | f5c9d92 | 2014-10-03 15:22:03 -0700 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | |
Thomas Vachuska | 10d4abc | 2014-10-21 12:47:26 -0700 | [diff] [blame] | 69 | private void printDetails(IntentService service, Intent intent) { |
Thomas Vachuska | 6ce7304 | 2014-10-21 10:01:49 -0700 | [diff] [blame] | 70 | 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 Vachuska | 10d4abc | 2014-10-21 12:47:26 -0700 | [diff] [blame] | 75 | 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 Vachuska | 6ce7304 | 2014-10-21 10:01:49 -0700 | [diff] [blame] | 81 | } |
| 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 Vachuska | 10d4abc | 2014-10-21 12:47:26 -0700 | [diff] [blame] | 100 | |
| 101 | List<Intent> installable = service.getInstallableIntents(intent.id()); |
Brian O'Connor | fe0f4b1 | 2014-10-30 21:19:02 -0700 | [diff] [blame] | 102 | if (showInstallable && installable != null && !installable.isEmpty()) { |
Thomas Vachuska | 10d4abc | 2014-10-21 12:47:26 -0700 | [diff] [blame] | 103 | print(" installable=%s", installable); |
| 104 | } |
Thomas Vachuska | 6ce7304 | 2014-10-21 10:01:49 -0700 | [diff] [blame] | 105 | } |
| 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 Vachuska | 6ce7304 | 2014-10-21 10:01:49 -0700 | [diff] [blame] | 120 | .put("type", intent.getClass().getSimpleName()) |
| 121 | .put("appId", intent.appId().name()); |
| 122 | |
Thomas Vachuska | 10d4abc | 2014-10-21 12:47:26 -0700 | [diff] [blame] | 123 | IntentState state = service.getIntentState(intent.id()); |
| 124 | if (state != null) { |
| 125 | result.put("state", state.toString()); |
| 126 | } |
| 127 | |
Thomas Vachuska | 6ce7304 | 2014-10-21 10:01:49 -0700 | [diff] [blame] | 128 | 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 Vachuska | 10d4abc | 2014-10-21 12:47:26 -0700 | [diff] [blame] | 174 | List<Intent> installable = service.getInstallableIntents(intent.id()); |
| 175 | if (installable != null && !installable.isEmpty()) { |
| 176 | result.set("installable", json(service, installable)); |
| 177 | } |
Thomas Vachuska | 6ce7304 | 2014-10-21 10:01:49 -0700 | [diff] [blame] | 178 | 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 | |
tom | f5c9d92 | 2014-10-03 15:22:03 -0700 | [diff] [blame] | 189 | } |