Thomas Vachuska | 7266738 | 2016-02-22 14:27:48 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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.drivermatrix; |
| 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.driver.Behaviour; |
| 23 | import org.onosproject.net.driver.Driver; |
| 24 | import org.onosproject.net.driver.DriverService; |
| 25 | import org.onosproject.ui.RequestHandler; |
| 26 | import org.onosproject.ui.UiMessageHandler; |
| 27 | import org.slf4j.Logger; |
| 28 | import org.slf4j.LoggerFactory; |
| 29 | |
| 30 | import java.util.ArrayList; |
| 31 | import java.util.Collection; |
| 32 | import java.util.Comparator; |
Thomas Vachuska | 7266738 | 2016-02-22 14:27:48 -0800 | [diff] [blame] | 33 | import java.util.HashSet; |
| 34 | import java.util.List; |
Thomas Vachuska | 7266738 | 2016-02-22 14:27:48 -0800 | [diff] [blame] | 35 | import java.util.Set; |
| 36 | |
| 37 | /** |
Simon Hunt | a2b11a5 | 2016-02-22 22:05:47 -0800 | [diff] [blame^] | 38 | * Message handler for driver matrix view related messages. |
Thomas Vachuska | 7266738 | 2016-02-22 14:27:48 -0800 | [diff] [blame] | 39 | */ |
| 40 | public class DriverViewMessageHandler extends UiMessageHandler { |
| 41 | |
| 42 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 43 | |
Simon Hunt | a2b11a5 | 2016-02-22 22:05:47 -0800 | [diff] [blame^] | 44 | private static final int ONE = 1; |
Thomas Vachuska | 7266738 | 2016-02-22 14:27:48 -0800 | [diff] [blame] | 45 | private static final String DRIVER_DATA_REQUEST = "driverDataRequest"; |
| 46 | private static final String DRIVER_DATA_RESPONSE = "driverDataResponse"; |
| 47 | |
| 48 | private static final String DRIVERS = "drivers"; |
| 49 | private static final String BEHAVIOURS = "behaviours"; |
Simon Hunt | a2b11a5 | 2016-02-22 22:05:47 -0800 | [diff] [blame^] | 50 | private static final String MATRIX = "matrix"; |
Thomas Vachuska | 7266738 | 2016-02-22 14:27:48 -0800 | [diff] [blame] | 51 | |
| 52 | private static final Comparator<? super Class<? extends Behaviour>> BEHAVIOUR_BY_NAME = |
| 53 | (o1, o2) -> o1.getSimpleName().compareTo(o2.getSimpleName()); |
| 54 | private static final Comparator<? super Driver> DRIVER_BY_NAME = |
| 55 | (o1, o2) -> o1.name().compareTo(o2.name()); |
| 56 | |
| 57 | |
| 58 | @Override |
| 59 | protected Collection<RequestHandler> createRequestHandlers() { |
| 60 | return ImmutableSet.of( |
| 61 | new DataRequestHandler() |
Simon Hunt | a2b11a5 | 2016-02-22 22:05:47 -0800 | [diff] [blame^] | 62 | // TODO: for row selection, produce data for detail panel |
Thomas Vachuska | 7266738 | 2016-02-22 14:27:48 -0800 | [diff] [blame] | 63 | // new DetailRequestHandler() |
| 64 | ); |
| 65 | } |
| 66 | |
Simon Hunt | a2b11a5 | 2016-02-22 22:05:47 -0800 | [diff] [blame^] | 67 | // handler for driver matrix requests |
Thomas Vachuska | 7266738 | 2016-02-22 14:27:48 -0800 | [diff] [blame] | 68 | private final class DataRequestHandler extends RequestHandler { |
| 69 | |
| 70 | private DataRequestHandler() { |
| 71 | super(DRIVER_DATA_REQUEST); |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public void process(long sid, ObjectNode payload) { |
Thomas Vachuska | 7266738 | 2016-02-22 14:27:48 -0800 | [diff] [blame] | 76 | DriverService driverService = get(DriverService.class); |
| 77 | |
Simon Hunt | a2b11a5 | 2016-02-22 22:05:47 -0800 | [diff] [blame^] | 78 | List<Driver> drivers = new ArrayList<>(driverService.getDrivers()); |
| 79 | drivers = orderDrivers(drivers); |
Thomas Vachuska | 7266738 | 2016-02-22 14:27:48 -0800 | [diff] [blame] | 80 | |
| 81 | // Produce a union of all behaviours (and order them) |
Simon Hunt | a2b11a5 | 2016-02-22 22:05:47 -0800 | [diff] [blame^] | 82 | List<Class<? extends Behaviour>> behaviours = orderBehaviours(drivers); |
Thomas Vachuska | 7266738 | 2016-02-22 14:27:48 -0800 | [diff] [blame] | 83 | |
| 84 | // Produce a JSON structure and send it |
Simon Hunt | a2b11a5 | 2016-02-22 22:05:47 -0800 | [diff] [blame^] | 85 | sendMessage(DRIVER_DATA_RESPONSE, 0, driversJson(drivers, behaviours)); |
Thomas Vachuska | 7266738 | 2016-02-22 14:27:48 -0800 | [diff] [blame] | 86 | } |
| 87 | |
Simon Hunt | a2b11a5 | 2016-02-22 22:05:47 -0800 | [diff] [blame^] | 88 | private List<Driver> orderDrivers(List<Driver> drivers) { |
Thomas Vachuska | 7266738 | 2016-02-22 14:27:48 -0800 | [diff] [blame] | 89 | // For now order by alphanumeric name of the driver |
Simon Hunt | a2b11a5 | 2016-02-22 22:05:47 -0800 | [diff] [blame^] | 90 | drivers.sort(DRIVER_BY_NAME); |
| 91 | return drivers; |
Thomas Vachuska | 7266738 | 2016-02-22 14:27:48 -0800 | [diff] [blame] | 92 | } |
| 93 | |
Simon Hunt | a2b11a5 | 2016-02-22 22:05:47 -0800 | [diff] [blame^] | 94 | private List<Class<? extends Behaviour>> orderBehaviours(List<Driver> drivers) { |
| 95 | // first, produce a set-union of all behaviours from all drivers... |
Thomas Vachuska | 7266738 | 2016-02-22 14:27:48 -0800 | [diff] [blame] | 96 | Set<Class<? extends Behaviour>> allBehaviours = new HashSet<>(); |
Simon Hunt | a2b11a5 | 2016-02-22 22:05:47 -0800 | [diff] [blame^] | 97 | drivers.forEach(d -> allBehaviours.addAll(d.behaviours())); |
| 98 | |
| 99 | // for now, order by alphanumeric name of the abstract behaviour simple name |
Thomas Vachuska | 7266738 | 2016-02-22 14:27:48 -0800 | [diff] [blame] | 100 | List<Class<? extends Behaviour>> ordered = new ArrayList<>(allBehaviours); |
| 101 | ordered.sort(BEHAVIOUR_BY_NAME); |
| 102 | return ordered; |
| 103 | } |
| 104 | |
Simon Hunt | a2b11a5 | 2016-02-22 22:05:47 -0800 | [diff] [blame^] | 105 | private ObjectNode driversJson(List<Driver> drivers, |
Thomas Vachuska | 7266738 | 2016-02-22 14:27:48 -0800 | [diff] [blame] | 106 | List<Class<? extends Behaviour>> behaviours) { |
| 107 | ObjectNode root = objectNode(); |
| 108 | addBehaviours(root, behaviours); |
| 109 | addDrivers(root, drivers); |
Simon Hunt | a2b11a5 | 2016-02-22 22:05:47 -0800 | [diff] [blame^] | 110 | addMatrixCells(root, drivers); |
Thomas Vachuska | 7266738 | 2016-02-22 14:27:48 -0800 | [diff] [blame] | 111 | return root; |
| 112 | } |
| 113 | |
Simon Hunt | a2b11a5 | 2016-02-22 22:05:47 -0800 | [diff] [blame^] | 114 | private void addBehaviours(ObjectNode root, |
| 115 | List<Class<? extends Behaviour>> behaviours) { |
Thomas Vachuska | 7266738 | 2016-02-22 14:27:48 -0800 | [diff] [blame] | 116 | ArrayNode array = arrayNode(); |
| 117 | root.set(BEHAVIOURS, array); |
| 118 | behaviours.forEach(b -> array.add(b.getSimpleName())); |
| 119 | } |
| 120 | |
| 121 | private void addDrivers(ObjectNode root, List<Driver> drivers) { |
| 122 | ArrayNode array = arrayNode(); |
| 123 | root.set(DRIVERS, array); |
| 124 | drivers.forEach(d -> array.add(d.name())); |
| 125 | } |
| 126 | |
Simon Hunt | a2b11a5 | 2016-02-22 22:05:47 -0800 | [diff] [blame^] | 127 | private void addMatrixCells(ObjectNode root, List<Driver> drivers) { |
| 128 | ObjectNode matrix = objectNode(); |
| 129 | root.set(MATRIX, matrix); |
| 130 | |
| 131 | drivers.forEach(d -> { |
| 132 | ObjectNode dnode = objectNode(); |
| 133 | matrix.set(d.name(), dnode); |
| 134 | |
| 135 | d.behaviours().forEach(b -> { |
| 136 | // TODO: can put a payload here, rather than a '1' marker |
| 137 | dnode.put(b.getSimpleName(), ONE); |
| 138 | }); |
| 139 | }); |
Thomas Vachuska | 7266738 | 2016-02-22 14:27:48 -0800 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
| 143 | } |