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