blob: 3a374811b3fcedb10e60dae49de739612be3bfaf [file] [log] [blame]
Thomas Vachuskaee79ad32019-03-05 17:26:55 -08001/*
2 * Copyright 2019-present Open Networking Foundation
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
17package org.onosproject.onlpdemo;
18
19import com.fasterxml.jackson.databind.node.ObjectNode;
20import com.google.common.base.Strings;
21import com.google.common.collect.ImmutableSet;
22import org.onosproject.net.DeviceId;
23import org.onosproject.onlpdemo.OnlpDemoManager.OnlpData;
24import org.onosproject.onlpdemo.OnlpDemoManager.OnlpDataSource;
25import org.onosproject.ui.RequestHandler;
26import org.onosproject.ui.UiMessageHandler;
27import org.onosproject.ui.table.TableModel;
28import org.onosproject.ui.table.TableRequestHandler;
29import org.onosproject.ui.table.cell.AbstractCellComparator;
30
31import java.util.Collection;
32
33
34/**
35 * Message handler for ONLP view related messages.
36 */
37public class OnlpDemoViewMessageHandler extends UiMessageHandler {
38
39 private static final String ONLP_DATA_REQ = "onlpDataRequest";
40 private static final String ONLP_DATA_RESP = "onlpDataResponse";
41 private static final String ONLPS = "onlps";
42
43 private static final String ID = "id";
44 private static final String PRESENCE = "presence";
45 private static final String VENDOR = "vendor";
46 private static final String SERIAL_NO = "serial_number";
47 private static final String MODEL_NO = "model_number";
48 private static final String FORM_FACTOR = "form_factor";
49
50
51 private static final String[] COL_IDS = {
52 ID, PRESENCE, VENDOR, MODEL_NO, SERIAL_NO, FORM_FACTOR,
53 };
54
55 private final OnlpDataSource onlpDataSource;
56
57 public OnlpDemoViewMessageHandler(OnlpDataSource onlpDataSource) {
58 super();
59 this.onlpDataSource = onlpDataSource;
60 }
61
62 @Override
63 protected Collection<RequestHandler> createRequestHandlers() {
64 return ImmutableSet.of(
65 new OnlpDataRequest()
66 );
67 }
68
69 // handler for port table requests
70 private final class OnlpDataRequest extends TableRequestHandler {
71
72 private static final String NO_ROWS_MESSAGE = "No data available yet";
73
74 private OnlpDataRequest() {
75 super(ONLP_DATA_REQ, ONLP_DATA_RESP, ONLPS);
76 }
77
78 @Override
79 protected String[] getColumnIds() {
80 return COL_IDS;
81 }
82
83 @Override
84 protected String noRowsMessage(ObjectNode payload) {
85 return NO_ROWS_MESSAGE;
86 }
87
88 @Override
89 protected TableModel createTableModel() {
90 TableModel tm = super.createTableModel();
91 tm.setComparator(ID, new SfpIdCellComparator());
92 return tm;
93 }
94
95 @Override
96 protected void populateTable(TableModel tm, ObjectNode payload) {
97 String uri = string(payload, "devId");
98 if (!Strings.isNullOrEmpty(uri)) {
99 for (OnlpData data : onlpDataSource.getData(DeviceId.deviceId(uri))) {
100 populateRow(tm.addRow(), data);
101 }
102 }
103 }
104
105 private void populateRow(TableModel.Row row, OnlpData data) {
106 row.cell(ID, data.id)
107 .cell(PRESENCE, data.presence)
108 .cell(VENDOR, data.vendor)
109 .cell(MODEL_NO, data.modelNumber)
110 .cell(SERIAL_NO, data.serialNumber)
111 .cell(FORM_FACTOR, data.formFactor);
112 }
113 }
114
115 private static class SfpIdCellComparator extends AbstractCellComparator {
116 @Override
117 protected int nonNullCompare(Object o1, Object o2) {
118 return index((String) o2) - index((String) o1);
119 }
120
121 private int index(String s) {
122 int i = s.indexOf("-");
123 try {
124 return i > 0 ? Integer.parseInt(s.substring(i)) : 0;
125 } catch (NumberFormatException e) {
126 return Integer.MAX_VALUE;
127 }
128 }
129 }
130
131}