blob: 29543b94141163781250b604f1f78393fc410f67 [file] [log] [blame]
Thomas Vachuska72667382016-02-22 14:27:48 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Thomas Vachuska72667382016-02-22 14:27:48 -08003 *
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
17package org.onosproject.drivermatrix;
18
19import com.fasterxml.jackson.databind.node.ArrayNode;
20import com.fasterxml.jackson.databind.node.ObjectNode;
21import com.google.common.collect.ImmutableSet;
22import org.onosproject.net.driver.Behaviour;
23import org.onosproject.net.driver.Driver;
24import org.onosproject.net.driver.DriverService;
25import org.onosproject.ui.RequestHandler;
26import org.onosproject.ui.UiMessageHandler;
27import org.slf4j.Logger;
28import org.slf4j.LoggerFactory;
29
30import java.util.ArrayList;
31import java.util.Collection;
32import java.util.Comparator;
Thomas Vachuska72667382016-02-22 14:27:48 -080033import java.util.HashSet;
34import java.util.List;
Thomas Vachuska72667382016-02-22 14:27:48 -080035import java.util.Set;
36
37/**
Simon Hunta2b11a52016-02-22 22:05:47 -080038 * Message handler for driver matrix view related messages.
Thomas Vachuska72667382016-02-22 14:27:48 -080039 */
40public class DriverViewMessageHandler extends UiMessageHandler {
41
42 private final Logger log = LoggerFactory.getLogger(getClass());
43
Simon Hunta2b11a52016-02-22 22:05:47 -080044 private static final int ONE = 1;
Thomas Vachuska72667382016-02-22 14:27:48 -080045 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 Hunta2b11a52016-02-22 22:05:47 -080050 private static final String MATRIX = "matrix";
Thomas Vachuska72667382016-02-22 14:27:48 -080051
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 Hunta2b11a52016-02-22 22:05:47 -080062 // TODO: for row selection, produce data for detail panel
Thomas Vachuska72667382016-02-22 14:27:48 -080063// new DetailRequestHandler()
64 );
65 }
66
Simon Hunta2b11a52016-02-22 22:05:47 -080067 // handler for driver matrix requests
Thomas Vachuska72667382016-02-22 14:27:48 -080068 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 Vachuska72667382016-02-22 14:27:48 -080076 DriverService driverService = get(DriverService.class);
77
Simon Hunta2b11a52016-02-22 22:05:47 -080078 List<Driver> drivers = new ArrayList<>(driverService.getDrivers());
79 drivers = orderDrivers(drivers);
Thomas Vachuska72667382016-02-22 14:27:48 -080080
81 // Produce a union of all behaviours (and order them)
Simon Hunta2b11a52016-02-22 22:05:47 -080082 List<Class<? extends Behaviour>> behaviours = orderBehaviours(drivers);
Thomas Vachuska72667382016-02-22 14:27:48 -080083
84 // Produce a JSON structure and send it
Simon Hunta2b11a52016-02-22 22:05:47 -080085 sendMessage(DRIVER_DATA_RESPONSE, 0, driversJson(drivers, behaviours));
Thomas Vachuska72667382016-02-22 14:27:48 -080086 }
87
Simon Hunta2b11a52016-02-22 22:05:47 -080088 private List<Driver> orderDrivers(List<Driver> drivers) {
Thomas Vachuska72667382016-02-22 14:27:48 -080089 // For now order by alphanumeric name of the driver
Simon Hunta2b11a52016-02-22 22:05:47 -080090 drivers.sort(DRIVER_BY_NAME);
91 return drivers;
Thomas Vachuska72667382016-02-22 14:27:48 -080092 }
93
Simon Hunta2b11a52016-02-22 22:05:47 -080094 private List<Class<? extends Behaviour>> orderBehaviours(List<Driver> drivers) {
95 // first, produce a set-union of all behaviours from all drivers...
Thomas Vachuska72667382016-02-22 14:27:48 -080096 Set<Class<? extends Behaviour>> allBehaviours = new HashSet<>();
Simon Hunta2b11a52016-02-22 22:05:47 -080097 drivers.forEach(d -> allBehaviours.addAll(d.behaviours()));
98
99 // for now, order by alphanumeric name of the abstract behaviour simple name
Thomas Vachuska72667382016-02-22 14:27:48 -0800100 List<Class<? extends Behaviour>> ordered = new ArrayList<>(allBehaviours);
101 ordered.sort(BEHAVIOUR_BY_NAME);
102 return ordered;
103 }
104
Simon Hunta2b11a52016-02-22 22:05:47 -0800105 private ObjectNode driversJson(List<Driver> drivers,
Thomas Vachuska72667382016-02-22 14:27:48 -0800106 List<Class<? extends Behaviour>> behaviours) {
107 ObjectNode root = objectNode();
108 addBehaviours(root, behaviours);
109 addDrivers(root, drivers);
Simon Hunta2b11a52016-02-22 22:05:47 -0800110 addMatrixCells(root, drivers);
Thomas Vachuska72667382016-02-22 14:27:48 -0800111 return root;
112 }
113
Simon Hunta2b11a52016-02-22 22:05:47 -0800114 private void addBehaviours(ObjectNode root,
115 List<Class<? extends Behaviour>> behaviours) {
Thomas Vachuska72667382016-02-22 14:27:48 -0800116 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
Thomas Vachuskab486fe72016-03-09 09:35:18 -0800127 private Set<Driver> findLineage(Driver driver) {
128 ImmutableSet.Builder<Driver> lineage = ImmutableSet.builder();
129 lineage.add(driver);
130 List<Driver> parents = driver.parents();
131 if (parents != null) {
132 parents.forEach(p -> lineage.addAll(findLineage(p)));
133 }
134 return lineage.build();
135 }
136
Simon Hunta2b11a52016-02-22 22:05:47 -0800137 private void addMatrixCells(ObjectNode root, List<Driver> drivers) {
138 ObjectNode matrix = objectNode();
139 root.set(MATRIX, matrix);
140
Thomas Vachuskab486fe72016-03-09 09:35:18 -0800141 drivers.forEach(driver -> {
Simon Hunta2b11a52016-02-22 22:05:47 -0800142 ObjectNode dnode = objectNode();
Thomas Vachuskab486fe72016-03-09 09:35:18 -0800143 matrix.set(driver.name(), dnode);
144 Set<Driver> lineage = findLineage(driver);
145 lineage.forEach(d -> d.behaviours().forEach(b -> {
Simon Hunta2b11a52016-02-22 22:05:47 -0800146 // TODO: can put a payload here, rather than a '1' marker
147 dnode.put(b.getSimpleName(), ONE);
Thomas Vachuskab486fe72016-03-09 09:35:18 -0800148 }));
Simon Hunta2b11a52016-02-22 22:05:47 -0800149 });
Thomas Vachuska72667382016-02-22 14:27:48 -0800150 }
151 }
Thomas Vachuska72667382016-02-22 14:27:48 -0800152}