1 /*******************************************************************************
2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 * ============LICENSE_END=========================================================
19 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21 *******************************************************************************/
22 package org.onap.dmaap.dmf.mr.service.impl;
24 import com.att.eelf.configuration.EELFLogger;
25 import com.att.eelf.configuration.EELFManager;
26 import com.att.nsa.configs.ConfigDbException;
27 import com.att.nsa.security.db.NsaApiDb;
28 import com.att.nsa.security.db.simple.NsaSimpleApiKey;
29 import org.apache.kafka.common.errors.TopicExistsException;
30 import org.json.JSONArray;
31 import org.json.JSONException;
32 import org.json.JSONObject;
33 import org.onap.dmaap.dmf.mr.CambriaApiException;
34 import org.onap.dmaap.dmf.mr.beans.DMaaPContext;
35 import org.onap.dmaap.dmf.mr.beans.DMaaPKafkaMetaBroker;
36 import org.onap.dmaap.dmf.mr.metabroker.Topic;
37 import org.onap.dmaap.dmf.mr.service.UIService;
38 import org.onap.dmaap.dmf.mr.utils.DMaaPResponseBuilder;
39 import org.springframework.stereotype.Service;
41 import java.io.IOException;
42 import java.util.LinkedList;
43 import java.util.List;
45 import java.util.Map.Entry;
48 * @author muzainulhaque.qazi
52 public class UIServiceImpl implements UIService {
55 private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(UIServiceImpl.class);
57 * Returning template of hello page
62 public void hello(DMaaPContext dmaapContext) throws IOException {
63 LOGGER.info("Returning template of hello page.");
64 DMaaPResponseBuilder.respondOkWithHtml(dmaapContext, "templates/hello.html");
68 * Fetching list of all api keys and returning in a templated form for display.
70 * @throws ConfigDbException
74 public void getApiKeysTable(DMaaPContext dmaapContext) throws ConfigDbException, IOException {
75 // TODO - We need to work on the templates and how data will be set in
77 LOGGER.info("Fetching list of all api keys and returning in a templated form for display.");
78 Map<String, NsaSimpleApiKey> keyMap = getApiKeyDb(dmaapContext).loadAllKeyRecords();
80 LinkedList<JSONObject> keyList = new LinkedList<>();
82 JSONObject jsonList = new JSONObject();
84 for (Entry<String, NsaSimpleApiKey> e : keyMap.entrySet()) {
85 final NsaSimpleApiKey key = e.getValue();
86 final JSONObject jsonObject = new JSONObject();
87 jsonObject.put("key", key.getKey());
88 jsonObject.put("email", key.getContactEmail());
89 jsonObject.put("description", key.getDescription());
90 keyList.add(jsonObject);
93 jsonList.put("apiKeys", keyList);
95 LOGGER.info("Returning list of all the api keys in JSON format for the template.");
96 // "templates/apiKeyList.html"
97 DMaaPResponseBuilder.respondOk(dmaapContext, jsonList);
102 * @param dmaapContext
104 * @throws ConfigDbException
105 * @throws IOException
106 * @throws JSONException
110 public void getApiKey(DMaaPContext dmaapContext, String apiKey) throws CambriaApiException, ConfigDbException, JSONException, IOException {
111 // TODO - We need to work on the templates and how data will be set in
113 LOGGER.info("Fetching detials of apikey: " + apiKey);
114 final NsaSimpleApiKey key = getApiKeyDb(dmaapContext).loadApiKey(apiKey);
117 LOGGER.info("Details of apikey [" + apiKey + "] found. Returning response");
118 DMaaPResponseBuilder.respondOk(dmaapContext, key.asJsonObject());
120 LOGGER.info("Details of apikey [" + apiKey + "] not found. Returning response");
121 throw new CambriaApiException(400,"Key [" + apiKey + "] not found.");
127 * Fetching list of all the topics
128 * @param dmaapContext
129 * @throws ConfigDbException
130 * @throws IOException
133 public void getTopicsTable(DMaaPContext dmaapContext) throws ConfigDbException, IOException {
134 // TODO - We need to work on the templates and how data will be set in
136 LOGGER.info("Fetching list of all the topics and returning in a templated form for display");
137 List<Topic> topicsList = getMetaBroker(dmaapContext).getAllTopics();
139 JSONObject jsonObject = new JSONObject();
141 JSONArray topicsArray = new JSONArray();
143 List<Topic> topicList = getMetaBroker(dmaapContext).getAllTopics();
145 for (Topic topic : topicList) {
146 JSONObject obj = new JSONObject();
147 obj.put("topicName", topic.getName());
148 obj.put("description", topic.getDescription());
149 obj.put("owner", topic.getOwner());
150 topicsArray.put(obj);
153 jsonObject.put("topics", topicsList);
155 LOGGER.info("Returning the list of topics in templated format for display.");
156 DMaaPResponseBuilder.respondOk(dmaapContext, jsonObject);
161 * @param dmaapContext
163 * @throws ConfigDbException
164 * @throws IOException
165 * @throws TopicExistsException
168 public void getTopic(DMaaPContext dmaapContext, String topicName)
169 throws ConfigDbException, IOException, TopicExistsException {
170 // TODO - We need to work on the templates and how data will be set in
172 LOGGER.info("Fetching detials of apikey: " + topicName);
173 Topic topic = getMetaBroker(dmaapContext).getTopic(topicName);
176 LOGGER.error("Topic [" + topicName + "] does not exist.");
177 throw new TopicExistsException("Topic [" + topicName + "] does not exist.");
180 JSONObject json = new JSONObject();
181 json.put("topicName", topic.getName());
182 json.put("description", topic.getDescription());
183 json.put("owner", topic.getOwner());
185 LOGGER.info("Returning details of topic [" + topicName + "]. Sending response.");
186 DMaaPResponseBuilder.respondOk(dmaapContext, json);
192 * @param dmaapContext
195 private NsaApiDb<NsaSimpleApiKey> getApiKeyDb(DMaaPContext dmaapContext) {
196 return dmaapContext.getConfigReader().getfApiKeyDb();
202 * @param dmaapContext
205 private DMaaPKafkaMetaBroker getMetaBroker(DMaaPContext dmaapContext) {
206 return (DMaaPKafkaMetaBroker) dmaapContext.getConfigReader().getfMetaBroker();