blob: 475b1055460254fa5250dcd725ce4650e64d77f3 [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
Thomas Vachuska04059f92017-03-07 15:16:23 -080017package org.onosproject.yang.gui;
Simon Huntcc035c52017-02-22 21:12:51 -080018
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;
Thomas Vachuska04059f92017-03-07 15:16:23 -080027import org.onosproject.yang.model.YangModel;
28import org.onosproject.yang.runtime.YangModelRegistry;
Simon Huntcc035c52017-02-22 21:12:51 -080029import org.slf4j.Logger;
30import org.slf4j.LoggerFactory;
31
32import java.util.Collection;
33
34/**
Thomas Vachuska04059f92017-03-07 15:16:23 -080035 * ONOS UI YANG Models message handler.
Simon Huntcc035c52017-02-22 21:12:51 -080036 */
37public class YangModelMessageHandler extends UiMessageHandler {
38
39 private static final String TABLE_REQ = "yangModelDataRequest";
40 private static final String TABLE_RESP = "yangModelDataResponse";
Simon Hunt7d5e9842017-02-23 11:37:02 -080041 private static final String MODELS = "yangModels";
Simon Huntcc035c52017-02-22 21:12:51 -080042
43 private static final String DETAILS_REQ = "yangModelDetailsRequest";
44 private static final String DETAILS_RESP = "yangModelDetailsResponse";
45 private static final String DETAILS = "details";
46
47 // Table Column IDs
48 private static final String ID = "id";
49 private static final String TYPE = "type";
50 // TODO: fill out table columns as needed
51
52 private static final String[] COL_IDS = {
53 ID, TYPE
54 };
55
56 private final Logger log = LoggerFactory.getLogger(getClass());
57
Thomas Vachuska04059f92017-03-07 15:16:23 -080058 private YangModelRegistry modelRegistry;
Simon Huntcc035c52017-02-22 21:12:51 -080059 // TODO: fill out other fields as necessary
60
61
62 // ===============-=-=-=-=-=-==================-=-=-=-=-=-=-===========
63
64 @Override
65 public void init(UiConnection connection, ServiceDirectory directory) {
66 super.init(connection, directory);
Thomas Vachuska04059f92017-03-07 15:16:23 -080067 modelRegistry = directory.get(YangModelRegistry.class);
Simon Huntcc035c52017-02-22 21:12:51 -080068 // TODO: addListeners(); ???
69 }
70
71 @Override
72 public void destroy() {
73 // TODO: removeListeners(); ???
74 super.destroy();
75 // NOTE: if no listeners are required, this method can be removed
76 }
77
78 @Override
79 protected Collection<RequestHandler> createRequestHandlers() {
80 return ImmutableSet.of(
Simon Hunt7d5e9842017-02-23 11:37:02 -080081 new TableDataHandler(),
82 new DetailRequestHandler()
Simon Huntcc035c52017-02-22 21:12:51 -080083 );
84 }
85
86
87 // Handler for table requests
88 private final class TableDataHandler extends TableRequestHandler {
89 private static final String NO_ROWS_MESSAGE = "No YANG Models found";
90
91 private TableDataHandler() {
92 super(TABLE_REQ, TABLE_RESP, MODELS);
93 }
94
95 @Override
96 protected String[] getColumnIds() {
97 return COL_IDS;
98 }
99
100 @Override
101 protected String noRowsMessage(ObjectNode payload) {
102 return NO_ROWS_MESSAGE;
103 }
104
105 @Override
106 protected void populateTable(TableModel tm, ObjectNode payload) {
Thomas Vachuska04059f92017-03-07 15:16:23 -0800107 for (YangModel model : modelRegistry.getModels()) {
108 populateRow(tm.addRow(), model.getYangModulesId().toString());
Simon Huntcc035c52017-02-22 21:12:51 -0800109 }
110 }
111
112 // TODO: obviously, this should be adapted to arrange YANG model data
113 // into the appropriate table columns
Thomas Vachuska04059f92017-03-07 15:16:23 -0800114 private void populateRow(TableModel.Row row, String k) {
115 row.cell(ID, k).cell(TYPE, k);
Simon Huntcc035c52017-02-22 21:12:51 -0800116 }
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}