tom | f5c9d92 | 2014-10-03 15:22:03 -0700 | [diff] [blame] | 1 | package org.onlab.onos.cli.net; |
| 2 | |
Thomas Vachuska | 6ce7304 | 2014-10-21 10:01:49 -0700 | [diff] [blame^] | 3 | import com.fasterxml.jackson.databind.JsonNode; |
| 4 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 5 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 6 | import com.fasterxml.jackson.databind.node.ObjectNode; |
tom | f5c9d92 | 2014-10-03 15:22:03 -0700 | [diff] [blame] | 7 | import org.apache.karaf.shell.commands.Command; |
| 8 | import org.onlab.onos.cli.AbstractShellCommand; |
Thomas Vachuska | 6ce7304 | 2014-10-21 10:01:49 -0700 | [diff] [blame^] | 9 | import org.onlab.onos.net.ConnectPoint; |
| 10 | import org.onlab.onos.net.Link; |
| 11 | import org.onlab.onos.net.NetworkResource; |
| 12 | import org.onlab.onos.net.intent.ConnectivityIntent; |
tom | f5c9d92 | 2014-10-03 15:22:03 -0700 | [diff] [blame] | 13 | import org.onlab.onos.net.intent.Intent; |
| 14 | import org.onlab.onos.net.intent.IntentService; |
Brian O'Connor | a4cab07 | 2014-10-03 18:46:39 -0700 | [diff] [blame] | 15 | import org.onlab.onos.net.intent.IntentState; |
Thomas Vachuska | 6ce7304 | 2014-10-21 10:01:49 -0700 | [diff] [blame^] | 16 | import org.onlab.onos.net.intent.LinkCollectionIntent; |
| 17 | import org.onlab.onos.net.intent.MultiPointToSinglePointIntent; |
| 18 | import org.onlab.onos.net.intent.PathIntent; |
| 19 | import org.onlab.onos.net.intent.PointToPointIntent; |
| 20 | import org.onlab.onos.net.intent.SinglePointToMultiPointIntent; |
| 21 | |
| 22 | import java.util.Set; |
tom | f5c9d92 | 2014-10-03 15:22:03 -0700 | [diff] [blame] | 23 | |
| 24 | /** |
| 25 | * Lists the inventory of intents and their states. |
| 26 | */ |
| 27 | @Command(scope = "onos", name = "intents", |
| 28 | description = "Lists the inventory of intents and their states") |
| 29 | public class IntentsListCommand extends AbstractShellCommand { |
| 30 | |
| 31 | @Override |
| 32 | protected void execute() { |
| 33 | IntentService service = get(IntentService.class); |
Thomas Vachuska | 6ce7304 | 2014-10-21 10:01:49 -0700 | [diff] [blame^] | 34 | if (outputJson()) { |
| 35 | print("%s", json(service, service.getIntents())); |
| 36 | } else { |
| 37 | for (Intent intent : service.getIntents()) { |
| 38 | IntentState state = service.getIntentState(intent.id()); |
| 39 | print("id=%s, state=%s, type=%s, appId=%s", |
| 40 | intent.id(), state, intent.getClass().getSimpleName(), |
| 41 | intent.appId().name()); |
| 42 | printDetails(intent); |
| 43 | } |
tom | f5c9d92 | 2014-10-03 15:22:03 -0700 | [diff] [blame] | 44 | } |
| 45 | } |
| 46 | |
Thomas Vachuska | 6ce7304 | 2014-10-21 10:01:49 -0700 | [diff] [blame^] | 47 | private void printDetails(Intent intent) { |
| 48 | if (intent.resources() != null && !intent.resources().isEmpty()) { |
| 49 | print(" resources=%s", intent.resources()); |
| 50 | } |
| 51 | if (intent instanceof ConnectivityIntent) { |
| 52 | ConnectivityIntent ci = (ConnectivityIntent) intent; |
| 53 | print(" selector=%s", ci.selector().criteria()); |
| 54 | print(" treatment=%s", ci.treatment().instructions()); |
| 55 | } |
| 56 | |
| 57 | if (intent instanceof PointToPointIntent) { |
| 58 | PointToPointIntent pi = (PointToPointIntent) intent; |
| 59 | print(" ingress=%s, egress=%s", pi.ingressPoint(), pi.egressPoint()); |
| 60 | } else if (intent instanceof MultiPointToSinglePointIntent) { |
| 61 | MultiPointToSinglePointIntent pi = (MultiPointToSinglePointIntent) intent; |
| 62 | print(" ingress=%s, egress=%s", pi.ingressPoints(), pi.egressPoint()); |
| 63 | } else if (intent instanceof SinglePointToMultiPointIntent) { |
| 64 | SinglePointToMultiPointIntent pi = (SinglePointToMultiPointIntent) intent; |
| 65 | print(" ingress=%s, egress=%s", pi.ingressPoint(), pi.egressPoints()); |
| 66 | } else if (intent instanceof PathIntent) { |
| 67 | PathIntent pi = (PathIntent) intent; |
| 68 | print(" path=%s, cost=%d", pi.path().links(), pi.path().cost()); |
| 69 | } else if (intent instanceof LinkCollectionIntent) { |
| 70 | LinkCollectionIntent li = (LinkCollectionIntent) intent; |
| 71 | print(" links=%s", li.links()); |
| 72 | print(" egress=%s", li.egressPoint()); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | // Produces JSON array of the specified intents. |
| 77 | private JsonNode json(IntentService service, Iterable<Intent> intents) { |
| 78 | ObjectMapper mapper = new ObjectMapper(); |
| 79 | ArrayNode result = mapper.createArrayNode(); |
| 80 | for (Intent intent : intents) { |
| 81 | result.add(json(service, mapper, intent)); |
| 82 | } |
| 83 | return result; |
| 84 | } |
| 85 | |
| 86 | private JsonNode json(IntentService service, ObjectMapper mapper, Intent intent) { |
| 87 | ObjectNode result = mapper.createObjectNode() |
| 88 | .put("id", intent.id().toString()) |
| 89 | .put("state", service.getIntentState(intent.id()).toString()) |
| 90 | .put("type", intent.getClass().getSimpleName()) |
| 91 | .put("appId", intent.appId().name()); |
| 92 | |
| 93 | if (intent.resources() != null && !intent.resources().isEmpty()) { |
| 94 | ArrayNode rnode = mapper.createArrayNode(); |
| 95 | for (NetworkResource resource : intent.resources()) { |
| 96 | rnode.add(resource.toString()); |
| 97 | } |
| 98 | result.set("resources", rnode); |
| 99 | } |
| 100 | |
| 101 | if (intent instanceof ConnectivityIntent) { |
| 102 | ConnectivityIntent ci = (ConnectivityIntent) intent; |
| 103 | if (!ci.selector().criteria().isEmpty()) { |
| 104 | result.put("selector", ci.selector().criteria().toString()); |
| 105 | } |
| 106 | if (!ci.treatment().instructions().isEmpty()) { |
| 107 | result.put("treatment", ci.treatment().instructions().toString()); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | if (intent instanceof PathIntent) { |
| 112 | PathIntent pi = (PathIntent) intent; |
| 113 | ArrayNode pnode = mapper.createArrayNode(); |
| 114 | for (Link link : pi.path().links()) { |
| 115 | pnode.add(link.toString()); |
| 116 | } |
| 117 | result.set("path", pnode); |
| 118 | |
| 119 | } else if (intent instanceof PointToPointIntent) { |
| 120 | PointToPointIntent pi = (PointToPointIntent) intent; |
| 121 | result.set("ingress", LinksListCommand.json(mapper, pi.ingressPoint())); |
| 122 | result.set("egress", LinksListCommand.json(mapper, pi.egressPoint())); |
| 123 | |
| 124 | } else if (intent instanceof MultiPointToSinglePointIntent) { |
| 125 | MultiPointToSinglePointIntent pi = (MultiPointToSinglePointIntent) intent; |
| 126 | result.set("ingress", json(mapper, pi.ingressPoints())); |
| 127 | result.set("egress", LinksListCommand.json(mapper, pi.egressPoint())); |
| 128 | |
| 129 | } else if (intent instanceof SinglePointToMultiPointIntent) { |
| 130 | SinglePointToMultiPointIntent pi = (SinglePointToMultiPointIntent) intent; |
| 131 | result.set("ingress", LinksListCommand.json(mapper, pi.ingressPoint())); |
| 132 | result.set("egress", json(mapper, pi.egressPoints())); |
| 133 | |
| 134 | } else if (intent instanceof LinkCollectionIntent) { |
| 135 | LinkCollectionIntent li = (LinkCollectionIntent) intent; |
| 136 | result.set("links", LinksListCommand.json(li.links())); |
| 137 | } |
| 138 | |
| 139 | return result; |
| 140 | } |
| 141 | |
| 142 | private JsonNode json(ObjectMapper mapper, Set<ConnectPoint> connectPoints) { |
| 143 | ArrayNode result = mapper.createArrayNode(); |
| 144 | for (ConnectPoint cp : connectPoints) { |
| 145 | result.add(LinksListCommand.json(mapper, cp)); |
| 146 | } |
| 147 | return result; |
| 148 | } |
| 149 | |
tom | f5c9d92 | 2014-10-03 15:22:03 -0700 | [diff] [blame] | 150 | } |