blob: 1e4f86ea65f1efa064fc8ae38184fafd253dc972 [file] [log] [blame]
Simon Huntcc035c52017-02-22 21:12:51 -08001/*
2 * Copyright 2017-present 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
17package org.onosproject.yms.gui;
18
19import com.fasterxml.jackson.databind.node.ObjectNode;
20import com.google.common.collect.ImmutableSet;
21import org.onlab.osgi.ServiceDirectory;
22import org.onosproject.ui.RequestHandler;
23import org.onosproject.ui.UiConnection;
24import org.onosproject.ui.UiMessageHandler;
25import org.onosproject.ui.table.TableModel;
26import org.onosproject.ui.table.TableRequestHandler;
Simon Huntcc035c52017-02-22 21:12:51 -080027import org.slf4j.Logger;
28import org.slf4j.LoggerFactory;
29
30import java.util.Collection;
31
32/**
33 * ONOS UI Yang Models message handler.
34 */
35public class YangModelMessageHandler extends UiMessageHandler {
36
37 private static final String TABLE_REQ = "yangModelDataRequest";
38 private static final String TABLE_RESP = "yangModelDataResponse";
39 private static final String MODELS = "models";
40
41 private static final String DETAILS_REQ = "yangModelDetailsRequest";
42 private static final String DETAILS_RESP = "yangModelDetailsResponse";
43 private static final String DETAILS = "details";
44
45 // Table Column IDs
46 private static final String ID = "id";
47 private static final String TYPE = "type";
48 // TODO: fill out table columns as needed
49
50 private static final String[] COL_IDS = {
51 ID, TYPE
52 };
53
54 private final Logger log = LoggerFactory.getLogger(getClass());
55
Thomas Vachuska9ac4c7e2017-02-23 09:01:45 -080056// private YmsService ymsService;
Simon Huntcc035c52017-02-22 21:12:51 -080057 // TODO: fill out other fields as necessary
58
59
60 // ===============-=-=-=-=-=-==================-=-=-=-=-=-=-===========
61
62 @Override
63 public void init(UiConnection connection, ServiceDirectory directory) {
64 super.init(connection, directory);
Thomas Vachuska9ac4c7e2017-02-23 09:01:45 -080065// ymsService = directory.get(YmsService.class);
Simon Huntcc035c52017-02-22 21:12:51 -080066 // TODO: addListeners(); ???
67 }
68
69 @Override
70 public void destroy() {
71 // TODO: removeListeners(); ???
72 super.destroy();
73 // NOTE: if no listeners are required, this method can be removed
74 }
75
76 @Override
77 protected Collection<RequestHandler> createRequestHandlers() {
78 return ImmutableSet.of(
79 new TableDataHandler()
80 );
81 }
82
83
84 // Handler for table requests
85 private final class TableDataHandler extends TableRequestHandler {
86 private static final String NO_ROWS_MESSAGE = "No YANG Models found";
87
88 private TableDataHandler() {
89 super(TABLE_REQ, TABLE_RESP, MODELS);
90 }
91
92 @Override
93 protected String[] getColumnIds() {
94 return COL_IDS;
95 }
96
97 @Override
98 protected String noRowsMessage(ObjectNode payload) {
99 return NO_ROWS_MESSAGE;
100 }
101
102 @Override
103 protected void populateTable(TableModel tm, ObjectNode payload) {
104 // TODO: use ymsService(?) to iterate over list of models...
105 for (int k = 0; k < 5; k++) {
106 populateRow(tm.addRow(), k);
107 }
108 }
109
110 // TODO: obviously, this should be adapted to arrange YANG model data
111 // into the appropriate table columns
112 private void populateRow(TableModel.Row row, int k) {
113 row.cell(ID, k)
114 .cell(TYPE, "ymtype-" + k);
115 }
116 }
117
118
119 // handler for selected model detail requests (selected table row)
120 private final class DetailRequestHandler extends RequestHandler {
121 private DetailRequestHandler() {
122 super(DETAILS_REQ);
123 }
124
125 @Override
126 public void process(ObjectNode payload) {
127 String id = string(payload, ID);
128
129 // TODO: retrieve the appropriate model from ymsService and create
130 // a detail record to send back to the client.
131
132 ObjectNode data = objectNode();
133
134 data.put(ID, id);
135 data.put(TYPE, "some-type");
136 data.put("todo", "fill out with appropriate date attributes");
137
138 ObjectNode rootNode = objectNode();
139 rootNode.set(DETAILS, data);
140
141 sendMessage(DETAILS_RESP, rootNode);
142 }
143 }
144}