blob: 563990389bbd95d9a7fe1b9a0fae1fccd2e8dc3e [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;
Thomas Vachuska0cb73f42017-03-22 10:57:34 -070028import org.onosproject.yang.model.YangModuleId;
Thomas Vachuska04059f92017-03-07 15:16:23 -080029import org.onosproject.yang.runtime.YangModelRegistry;
Simon Huntcc035c52017-02-22 21:12:51 -080030import org.slf4j.Logger;
31import org.slf4j.LoggerFactory;
32
33import java.util.Collection;
Thomas Vachuska0cb73f42017-03-22 10:57:34 -070034import java.util.Set;
Simon Huntcc035c52017-02-22 21:12:51 -080035
36/**
Thomas Vachuska04059f92017-03-07 15:16:23 -080037 * ONOS UI YANG Models message handler.
Simon Huntcc035c52017-02-22 21:12:51 -080038 */
39public class YangModelMessageHandler extends UiMessageHandler {
40
41 private static final String TABLE_REQ = "yangModelDataRequest";
42 private static final String TABLE_RESP = "yangModelDataResponse";
Simon Hunt7d5e9842017-02-23 11:37:02 -080043 private static final String MODELS = "yangModels";
Simon Huntcc035c52017-02-22 21:12:51 -080044
45 private static final String DETAILS_REQ = "yangModelDetailsRequest";
46 private static final String DETAILS_RESP = "yangModelDetailsResponse";
47 private static final String DETAILS = "details";
48
49 // Table Column IDs
50 private static final String ID = "id";
Thomas Vachuska0cb73f42017-03-22 10:57:34 -070051 private static final String MODULES = "modules";
Simon Huntcc035c52017-02-22 21:12:51 -080052 // TODO: fill out table columns as needed
53
54 private static final String[] COL_IDS = {
Thomas Vachuska0cb73f42017-03-22 10:57:34 -070055 ID, MODULES
Simon Huntcc035c52017-02-22 21:12:51 -080056 };
57
58 private final Logger log = LoggerFactory.getLogger(getClass());
59
Thomas Vachuska04059f92017-03-07 15:16:23 -080060 private YangModelRegistry modelRegistry;
Simon Huntcc035c52017-02-22 21:12:51 -080061 // TODO: fill out other fields as necessary
62
63
64 // ===============-=-=-=-=-=-==================-=-=-=-=-=-=-===========
65
66 @Override
67 public void init(UiConnection connection, ServiceDirectory directory) {
68 super.init(connection, directory);
Thomas Vachuska04059f92017-03-07 15:16:23 -080069 modelRegistry = directory.get(YangModelRegistry.class);
Simon Huntcc035c52017-02-22 21:12:51 -080070 }
71
72 @Override
73 public void destroy() {
Simon Huntcc035c52017-02-22 21:12:51 -080074 super.destroy();
Simon Huntcc035c52017-02-22 21:12:51 -080075 }
76
77 @Override
78 protected Collection<RequestHandler> createRequestHandlers() {
79 return ImmutableSet.of(
Simon Hunt7d5e9842017-02-23 11:37:02 -080080 new TableDataHandler(),
81 new DetailRequestHandler()
Simon Huntcc035c52017-02-22 21:12:51 -080082 );
83 }
84
85
86 // Handler for table requests
87 private final class TableDataHandler extends TableRequestHandler {
88 private static final String NO_ROWS_MESSAGE = "No YANG Models found";
89
90 private TableDataHandler() {
91 super(TABLE_REQ, TABLE_RESP, MODELS);
92 }
93
94 @Override
95 protected String[] getColumnIds() {
96 return COL_IDS;
97 }
98
99 @Override
100 protected String noRowsMessage(ObjectNode payload) {
101 return NO_ROWS_MESSAGE;
102 }
103
104 @Override
105 protected void populateTable(TableModel tm, ObjectNode payload) {
Thomas Vachuska04059f92017-03-07 15:16:23 -0800106 for (YangModel model : modelRegistry.getModels()) {
Thomas Vachuska0cb73f42017-03-22 10:57:34 -0700107 populateRow(tm.addRow(), model.getYangModulesId());
Simon Huntcc035c52017-02-22 21:12:51 -0800108 }
109 }
110
Thomas Vachuska0cb73f42017-03-22 10:57:34 -0700111 private void populateRow(TableModel.Row row, Set<YangModuleId> moduleIds) {
112 StringBuilder sb = new StringBuilder();
113 moduleIds.forEach(i -> sb.append(", ").append(i.moduleName())
114 .append("(").append(i.revision()).append(")"));
115 row.cell(ID, moduleIds.hashCode()).cell(MODULES, sb.toString().substring(2));
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);
Thomas Vachuska0cb73f42017-03-22 10:57:34 -0700136 data.put(MODULES, "some-type");
Simon Huntcc035c52017-02-22 21:12:51 -0800137 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}