Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame^] | 1 | /* |
| 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 | */ |
tom | f5c9d92 | 2014-10-03 15:22:03 -0700 | [diff] [blame] | 19 | package org.onlab.onos.cli.net; |
| 20 | |
Thomas Vachuska | 6ce7304 | 2014-10-21 10:01:49 -0700 | [diff] [blame] | 21 | import com.fasterxml.jackson.databind.JsonNode; |
| 22 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 23 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 24 | import com.fasterxml.jackson.databind.node.ObjectNode; |
tom | f5c9d92 | 2014-10-03 15:22:03 -0700 | [diff] [blame] | 25 | import org.apache.karaf.shell.commands.Command; |
| 26 | import org.onlab.onos.cli.AbstractShellCommand; |
Thomas Vachuska | 6ce7304 | 2014-10-21 10:01:49 -0700 | [diff] [blame] | 27 | import org.onlab.onos.net.ConnectPoint; |
| 28 | import org.onlab.onos.net.Link; |
| 29 | import org.onlab.onos.net.NetworkResource; |
| 30 | import org.onlab.onos.net.intent.ConnectivityIntent; |
tom | f5c9d92 | 2014-10-03 15:22:03 -0700 | [diff] [blame] | 31 | import org.onlab.onos.net.intent.Intent; |
| 32 | import org.onlab.onos.net.intent.IntentService; |
Brian O'Connor | a4cab07 | 2014-10-03 18:46:39 -0700 | [diff] [blame] | 33 | import org.onlab.onos.net.intent.IntentState; |
Thomas Vachuska | 6ce7304 | 2014-10-21 10:01:49 -0700 | [diff] [blame] | 34 | import org.onlab.onos.net.intent.LinkCollectionIntent; |
| 35 | import org.onlab.onos.net.intent.MultiPointToSinglePointIntent; |
| 36 | import org.onlab.onos.net.intent.PathIntent; |
| 37 | import org.onlab.onos.net.intent.PointToPointIntent; |
| 38 | import org.onlab.onos.net.intent.SinglePointToMultiPointIntent; |
| 39 | |
Thomas Vachuska | 10d4abc | 2014-10-21 12:47:26 -0700 | [diff] [blame] | 40 | import java.util.List; |
Thomas Vachuska | 6ce7304 | 2014-10-21 10:01:49 -0700 | [diff] [blame] | 41 | import java.util.Set; |
tom | f5c9d92 | 2014-10-03 15:22:03 -0700 | [diff] [blame] | 42 | |
| 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") |
| 48 | public class IntentsListCommand extends AbstractShellCommand { |
| 49 | |
| 50 | @Override |
| 51 | protected void execute() { |
| 52 | IntentService service = get(IntentService.class); |
Thomas Vachuska | 6ce7304 | 2014-10-21 10:01:49 -0700 | [diff] [blame] | 53 | 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 Vachuska | 10d4abc | 2014-10-21 12:47:26 -0700 | [diff] [blame] | 61 | printDetails(service, intent); |
Thomas Vachuska | 6ce7304 | 2014-10-21 10:01:49 -0700 | [diff] [blame] | 62 | } |
tom | f5c9d92 | 2014-10-03 15:22:03 -0700 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | |
Thomas Vachuska | 10d4abc | 2014-10-21 12:47:26 -0700 | [diff] [blame] | 66 | private void printDetails(IntentService service, Intent intent) { |
Thomas Vachuska | 6ce7304 | 2014-10-21 10:01:49 -0700 | [diff] [blame] | 67 | 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 Vachuska | 10d4abc | 2014-10-21 12:47:26 -0700 | [diff] [blame] | 72 | 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 Vachuska | 6ce7304 | 2014-10-21 10:01:49 -0700 | [diff] [blame] | 78 | } |
| 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 Vachuska | 10d4abc | 2014-10-21 12:47:26 -0700 | [diff] [blame] | 97 | |
| 98 | List<Intent> installable = service.getInstallableIntents(intent.id()); |
| 99 | if (installable != null && !installable.isEmpty()) { |
| 100 | print(" installable=%s", installable); |
| 101 | } |
Thomas Vachuska | 6ce7304 | 2014-10-21 10:01:49 -0700 | [diff] [blame] | 102 | } |
| 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 Vachuska | 6ce7304 | 2014-10-21 10:01:49 -0700 | [diff] [blame] | 117 | .put("type", intent.getClass().getSimpleName()) |
| 118 | .put("appId", intent.appId().name()); |
| 119 | |
Thomas Vachuska | 10d4abc | 2014-10-21 12:47:26 -0700 | [diff] [blame] | 120 | IntentState state = service.getIntentState(intent.id()); |
| 121 | if (state != null) { |
| 122 | result.put("state", state.toString()); |
| 123 | } |
| 124 | |
Thomas Vachuska | 6ce7304 | 2014-10-21 10:01:49 -0700 | [diff] [blame] | 125 | 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 Vachuska | 10d4abc | 2014-10-21 12:47:26 -0700 | [diff] [blame] | 171 | List<Intent> installable = service.getInstallableIntents(intent.id()); |
| 172 | if (installable != null && !installable.isEmpty()) { |
| 173 | result.set("installable", json(service, installable)); |
| 174 | } |
Thomas Vachuska | 6ce7304 | 2014-10-21 10:01:49 -0700 | [diff] [blame] | 175 | 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 | |
tom | f5c9d92 | 2014-10-03 15:22:03 -0700 | [diff] [blame] | 186 | } |