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; |
| 33 | import java.util.HashMap; |
| 34 | import java.util.HashSet; |
| 35 | import java.util.List; |
| 36 | import java.util.Map; |
| 37 | import java.util.Set; |
| 38 | |
| 39 | /** |
| 40 | * Message handler for device view related messages. |
| 41 | */ |
| 42 | public class DriverViewMessageHandler extends UiMessageHandler { |
| 43 | |
| 44 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 45 | |
| 46 | private static final String DRIVER_DATA_REQUEST = "driverDataRequest"; |
| 47 | private static final String DRIVER_DATA_RESPONSE = "driverDataResponse"; |
| 48 | |
| 49 | private static final String DRIVERS = "drivers"; |
| 50 | private static final String BEHAVIOURS = "behaviours"; |
| 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() |
| 62 | // new DetailRequestHandler() |
| 63 | ); |
| 64 | } |
| 65 | |
| 66 | // handler for device table requests |
| 67 | private final class DataRequestHandler extends RequestHandler { |
| 68 | |
| 69 | private DataRequestHandler() { |
| 70 | super(DRIVER_DATA_REQUEST); |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public void process(long sid, ObjectNode payload) { |
| 75 | // Search for drivers producing two artifacts: |
| 76 | // 1) list of abstract behaviours as column listing |
| 77 | // 2) sparse matrix of drivers-to-concrete behaviours |
| 78 | |
| 79 | DriverService driverService = get(DriverService.class); |
| 80 | |
| 81 | // Collect all behaviours for all drivers |
| 82 | Map<Driver, Set<Class<? extends Behaviour>>> driverBehaviours = new HashMap<>(); |
| 83 | driverService.getDrivers().forEach(d -> driverBehaviours.put(d, d.behaviours())); |
| 84 | |
| 85 | // Order all drivers |
| 86 | List<Driver> drivers = orderDrivers(driverBehaviours.keySet()); |
| 87 | |
| 88 | // Produce a union of all behaviours (and order them) |
| 89 | List<Class<? extends Behaviour>> behaviours = orderBehaviours(driverBehaviours.values()); |
| 90 | |
| 91 | // Produce a JSON structure and send it |
| 92 | sendMessage(DRIVER_DATA_RESPONSE, 0, driversJson(driverBehaviours, drivers, behaviours)); |
| 93 | } |
| 94 | |
| 95 | private List<Driver> orderDrivers(Set<Driver> drivers) { |
| 96 | // For now order by alphanumeric name of the driver |
| 97 | List<Driver> ordered = new ArrayList<>(drivers); |
| 98 | ordered.sort(DRIVER_BY_NAME); |
| 99 | return ordered; |
| 100 | } |
| 101 | |
| 102 | private List<Class<? extends Behaviour>> |
| 103 | orderBehaviours(Collection<Set<Class<? extends Behaviour>>> behaviours) { |
| 104 | // For now order by alphanumeric name of the abstract behaviour simple name |
| 105 | Set<Class<? extends Behaviour>> allBehaviours = new HashSet<>(); |
| 106 | behaviours.forEach(allBehaviours::addAll); |
| 107 | List<Class<? extends Behaviour>> ordered = new ArrayList<>(allBehaviours); |
| 108 | ordered.sort(BEHAVIOUR_BY_NAME); |
| 109 | return ordered; |
| 110 | } |
| 111 | |
| 112 | private ObjectNode driversJson(Map<Driver, Set<Class<? extends Behaviour>>> driverBehaviours, |
| 113 | List<Driver> drivers, |
| 114 | List<Class<? extends Behaviour>> behaviours) { |
| 115 | ObjectNode root = objectNode(); |
| 116 | addBehaviours(root, behaviours); |
| 117 | addDrivers(root, drivers); |
| 118 | addRelationships(root, drivers, behaviours, driverBehaviours); |
| 119 | return root; |
| 120 | } |
| 121 | |
| 122 | private void addBehaviours(ObjectNode root, List<Class<? extends Behaviour>> behaviours) { |
| 123 | ArrayNode array = arrayNode(); |
| 124 | root.set(BEHAVIOURS, array); |
| 125 | behaviours.forEach(b -> array.add(b.getSimpleName())); |
| 126 | } |
| 127 | |
| 128 | private void addDrivers(ObjectNode root, List<Driver> drivers) { |
| 129 | ArrayNode array = arrayNode(); |
| 130 | root.set(DRIVERS, array); |
| 131 | drivers.forEach(d -> array.add(d.name())); |
| 132 | } |
| 133 | |
| 134 | private void addRelationships(ObjectNode root, |
| 135 | List<Driver> drivers, List<Class<? extends Behaviour>> behaviours, |
| 136 | Map<Driver, Set<Class<? extends Behaviour>>> driverBehaviours) { |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | } |