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 com.att.dmf.mr.service.impl;
24 import java.io.IOException;
25 import java.util.LinkedList;
26 import java.util.List;
28 import java.util.Map.Entry;
31 import org.apache.kafka.common.errors.TopicExistsException;
32 import org.json.JSONArray;
33 import org.json.JSONObject;
34 import org.springframework.stereotype.Service;
36 import com.att.dmf.mr.beans.DMaaPContext;
37 import com.att.dmf.mr.beans.DMaaPKafkaMetaBroker;
38 import com.att.dmf.mr.metabroker.Topic;
39 import com.att.dmf.mr.service.UIService;
40 import com.att.dmf.mr.utils.DMaaPResponseBuilder;
41 import com.att.eelf.configuration.EELFLogger;
42 import com.att.eelf.configuration.EELFManager;
43 import com.att.nsa.configs.ConfigDbException;
44 import com.att.nsa.security.db.NsaApiDb;
45 import com.att.nsa.security.db.simple.NsaSimpleApiKey;
47 * @author muzainulhaque.qazi
51 public class UIServiceImpl implements UIService {
54 private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(UIServiceImpl.class);
56 * Returning template of hello page
61 public void hello(DMaaPContext dmaapContext) throws IOException {
62 LOGGER.info("Returning template of hello page.");
63 DMaaPResponseBuilder.respondOkWithHtml(dmaapContext, "templates/hello.html");
67 * Fetching list of all api keys and returning in a templated form for display.
69 * @throws ConfigDbException
73 public void getApiKeysTable(DMaaPContext dmaapContext) throws ConfigDbException, IOException {
74 // TODO - We need to work on the templates and how data will be set in
76 LOGGER.info("Fetching list of all api keys and returning in a templated form for display.");
77 Map<String, NsaSimpleApiKey> keyMap = getApiKeyDb(dmaapContext).loadAllKeyRecords();
79 LinkedList<JSONObject> keyList = new LinkedList<>();
81 JSONObject jsonList = new JSONObject();
83 for (Entry<String, NsaSimpleApiKey> e : keyMap.entrySet()) {
84 final NsaSimpleApiKey key = e.getValue();
85 final JSONObject jsonObject = new JSONObject();
86 jsonObject.put("key", key.getKey());
87 jsonObject.put("email", key.getContactEmail());
88 jsonObject.put("description", key.getDescription());
89 keyList.add(jsonObject);
92 jsonList.put("apiKeys", keyList);
94 LOGGER.info("Returning list of all the api keys in JSON format for the template.");
95 // "templates/apiKeyList.html"
96 DMaaPResponseBuilder.respondOk(dmaapContext, jsonList);
101 * @param dmaapContext
106 public void getApiKey(DMaaPContext dmaapContext, String apiKey) throws Exception {
107 // TODO - We need to work on the templates and how data will be set in
109 LOGGER.info("Fetching detials of apikey: " + apiKey);
110 final NsaSimpleApiKey key = getApiKeyDb(dmaapContext).loadApiKey(apiKey);
113 LOGGER.info("Details of apikey [" + apiKey + "] found. Returning response");
114 DMaaPResponseBuilder.respondOk(dmaapContext, key.asJsonObject());
116 LOGGER.info("Details of apikey [" + apiKey + "] not found. Returning response");
117 throw new Exception("Key [" + apiKey + "] not found.");
123 * Fetching list of all the topics
124 * @param dmaapContext
125 * @throws ConfigDbException
126 * @throws IOException
129 public void getTopicsTable(DMaaPContext dmaapContext) throws ConfigDbException, IOException {
130 // TODO - We need to work on the templates and how data will be set in
132 LOGGER.info("Fetching list of all the topics and returning in a templated form for display");
133 List<Topic> topicsList = getMetaBroker(dmaapContext).getAllTopics();
135 JSONObject jsonObject = new JSONObject();
137 JSONArray topicsArray = new JSONArray();
139 List<Topic> topicList = getMetaBroker(dmaapContext).getAllTopics();
141 for (Topic topic : topicList) {
142 JSONObject obj = new JSONObject();
143 obj.put("topicName", topic.getName());
144 obj.put("description", topic.getDescription());
145 obj.put("owner", topic.getOwner());
146 topicsArray.put(obj);
149 jsonObject.put("topics", topicsList);
151 LOGGER.info("Returning the list of topics in templated format for display.");
152 DMaaPResponseBuilder.respondOk(dmaapContext, jsonObject);
157 * @param dmaapContext
159 * @throws ConfigDbException
160 * @throws IOException
161 * @throws TopicExistsException
164 public void getTopic(DMaaPContext dmaapContext, String topicName)
165 throws ConfigDbException, IOException, TopicExistsException {
166 // TODO - We need to work on the templates and how data will be set in
168 LOGGER.info("Fetching detials of apikey: " + topicName);
169 Topic topic = getMetaBroker(dmaapContext).getTopic(topicName);
172 LOGGER.error("Topic [" + topicName + "] does not exist.");
173 throw new TopicExistsException("Topic [" + topicName + "] does not exist.");
176 JSONObject json = new JSONObject();
177 json.put("topicName", topic.getName());
178 json.put("description", topic.getDescription());
179 json.put("owner", topic.getOwner());
181 LOGGER.info("Returning details of topic [" + topicName + "]. Sending response.");
182 DMaaPResponseBuilder.respondOk(dmaapContext, json);
188 * @param dmaapContext
191 private NsaApiDb<NsaSimpleApiKey> getApiKeyDb(DMaaPContext dmaapContext) {
192 return dmaapContext.getConfigReader().getfApiKeyDb();
198 * @param dmaapContext
201 private DMaaPKafkaMetaBroker getMetaBroker(DMaaPContext dmaapContext) {
202 return (DMaaPKafkaMetaBroker) dmaapContext.getConfigReader().getfMetaBroker();