Bri Prebilic Cole | 9fb594a | 2015-04-14 09:15:54 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 3 | * |
| 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 |
| 7 | * |
| 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. |
| 15 | */ |
| 16 | |
| 17 | package org.onosproject.ui.impl; |
| 18 | |
| 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | import com.google.common.collect.ImmutableSet; |
| 22 | import org.onosproject.net.ConnectPoint; |
| 23 | import org.onosproject.net.Link; |
| 24 | import org.onosproject.net.link.LinkService; |
| 25 | |
| 26 | import java.util.ArrayList; |
| 27 | import java.util.Arrays; |
| 28 | import java.util.List; |
| 29 | |
| 30 | /** |
| 31 | * Message handler for link view related messages. |
| 32 | */ |
| 33 | public class LinkViewMessageHandler extends AbstractTabularViewMessageHandler { |
| 34 | |
| 35 | /** |
| 36 | * Creates a new message handler for the link messages. |
| 37 | */ |
| 38 | protected LinkViewMessageHandler() { |
| 39 | super(ImmutableSet.of("linkDataRequest")); |
| 40 | } |
| 41 | |
| 42 | @Override |
| 43 | public void process(ObjectNode message) { |
| 44 | ObjectNode payload = payload(message); |
| 45 | String sortCol = string(payload, "sortCol", "src"); |
| 46 | String sortDir = string(payload, "sortDir", "asc"); |
| 47 | |
| 48 | LinkService service = get(LinkService.class); |
| 49 | TableRow[] rows = generateTableRows(service); |
| 50 | RowComparator rc = |
| 51 | new RowComparator(sortCol, RowComparator.direction(sortDir)); |
| 52 | Arrays.sort(rows, rc); |
| 53 | ArrayNode links = generateArrayNode(rows); |
| 54 | ObjectNode rootNode = mapper.createObjectNode(); |
| 55 | rootNode.set("links", links); |
| 56 | |
| 57 | connection().sendMessage("linkDataResponse", 0, rootNode); |
| 58 | } |
| 59 | |
| 60 | private TableRow[] generateTableRows(LinkService service) { |
| 61 | List<TableRow> list = new ArrayList<>(); |
| 62 | for (Link link : service.getLinks()) { |
| 63 | list.add(new LinkTableRow(link)); |
| 64 | } |
| 65 | return list.toArray(new TableRow[list.size()]); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * TableRow implementation for {@link Link links}. |
| 70 | */ |
| 71 | private static class LinkTableRow extends AbstractTableRow { |
| 72 | |
| 73 | private static final String SOURCE = "src"; |
| 74 | private static final String DEST = "dst"; |
| 75 | private static final String TYPE = "type"; |
| 76 | private static final String STATE = "state"; |
| 77 | private static final String DURABLE = "durable"; |
| 78 | |
| 79 | private static final String[] COL_IDS = { |
| 80 | SOURCE, DEST, TYPE, STATE, DURABLE |
| 81 | }; |
| 82 | |
| 83 | public LinkTableRow(Link l) { |
| 84 | ConnectPoint src = l.src(); |
| 85 | ConnectPoint dst = l.dst(); |
| 86 | |
| 87 | add(SOURCE, src.elementId().toString() + "/" + src.port().toString()); |
| 88 | add(DEST, dst.elementId().toString() + "/" + dst.port().toString()); |
| 89 | add(TYPE, l.type().toString()); |
| 90 | add(STATE, l.state().toString()); |
| 91 | add(DURABLE, Boolean.toString(l.isDurable())); |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | protected String[] columnIds() { |
| 96 | return COL_IDS; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | } |