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.JSONException;
34 import org.json.JSONObject;
35 import org.springframework.stereotype.Service;
37 import com.att.dmf.mr.CambriaApiException;
38 import com.att.dmf.mr.beans.DMaaPContext;
39 import com.att.dmf.mr.beans.DMaaPKafkaMetaBroker;
40 import com.att.dmf.mr.metabroker.Topic;
41 import com.att.dmf.mr.service.UIService;
42 import com.att.dmf.mr.utils.DMaaPResponseBuilder;
43 import com.att.eelf.configuration.EELFLogger;
44 import com.att.eelf.configuration.EELFManager;
45 import com.att.nsa.configs.ConfigDbException;
46 import com.att.nsa.security.db.NsaApiDb;
47 import com.att.nsa.security.db.simple.NsaSimpleApiKey;
49 * @author muzainulhaque.qazi
53 public class UIServiceImpl implements UIService {
56 private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(UIServiceImpl.class);
58 * Returning template of hello page
63 public void hello(DMaaPContext dmaapContext) throws IOException {
64 LOGGER.info("Returning template of hello page.");
65 DMaaPResponseBuilder.respondOkWithHtml(dmaapContext, "templates/hello.html");
69 * Fetching list of all api keys and returning in a templated form for display.
71 * @throws ConfigDbException
75 public void getApiKeysTable(DMaaPContext dmaapContext) throws ConfigDbException, IOException {
76 // TODO - We need to work on the templates and how data will be set in
78 LOGGER.info("Fetching list of all api keys and returning in a templated form for display.");
79 Map<String, NsaSimpleApiKey> keyMap = getApiKeyDb(dmaapContext).loadAllKeyRecords();
81 LinkedList<JSONObject> keyList = new LinkedList<>();
83 JSONObject jsonList = new JSONObject();
85 for (Entry<String, NsaSimpleApiKey> e : keyMap.entrySet()) {
86 final NsaSimpleApiKey key = e.getValue();
87 final JSONObject jsonObject = new JSONObject();
88 jsonObject.put("key", key.getKey());
89 jsonObject.put("email", key.getContactEmail());
90 jsonObject.put("description", key.getDescription());
91 keyList.add(jsonObject);
94 jsonList.put("apiKeys", keyList);
96 LOGGER.info("Returning list of all the api keys in JSON format for the template.");
97 // "templates/apiKeyList.html"
98 DMaaPResponseBuilder.respondOk(dmaapContext, jsonList);
103 * @param dmaapContext
105 * @throws ConfigDbException
106 * @throws IOException
107 * @throws JSONException
111 public void getApiKey(DMaaPContext dmaapContext, String apiKey) throws CambriaApiException, ConfigDbException, JSONException, IOException {
112 // TODO - We need to work on the templates and how data will be set in
114 LOGGER.info("Fetching detials of apikey: " + apiKey);
115 final NsaSimpleApiKey key = getApiKeyDb(dmaapContext).loadApiKey(apiKey);
118 LOGGER.info("Details of apikey [" + apiKey + "] found. Returning response");
119 DMaaPResponseBuilder.respondOk(dmaapContext, key.asJsonObject());
121 LOGGER.info("Details of apikey [" + apiKey + "] not found. Returning response");
122 throw new CambriaApiException(400,"Key [" + apiKey + "] not found.");
128 * Fetching list of all the topics
129 * @param dmaapContext
130 * @throws ConfigDbException
131 * @throws IOException
134 public void getTopicsTable(DMaaPContext dmaapContext) throws ConfigDbException, IOException {
135 // TODO - We need to work on the templates and how data will be set in
137 LOGGER.info("Fetching list of all the topics and returning in a templated form for display");
138 List<Topic> topicsList = getMetaBroker(dmaapContext).getAllTopics();
140 JSONObject jsonObject = new JSONObject();
142 JSONArray topicsArray = new JSONArray();
144 List<Topic> topicList = getMetaBroker(dmaapContext).getAllTopics();
146 for (Topic topic : topicList) {
147 JSONObject obj = new JSONObject();
148 obj.put("topicName", topic.getName());
149 obj.put("description", topic.getDescription());
150 obj.put("owner", topic.getOwner());
151 topicsArray.put(obj);
154 jsonObject.put("topics", topicsList);
156 LOGGER.info("Returning the list of topics in templated format for display.");
157 DMaaPResponseBuilder.respondOk(dmaapContext, jsonObject);
162 * @param dmaapContext
164 * @throws ConfigDbException
165 * @throws IOException
166 * @throws TopicExistsException
169 public void getTopic(DMaaPContext dmaapContext, String topicName)
170 throws ConfigDbException, IOException, TopicExistsException {
171 // TODO - We need to work on the templates and how data will be set in
173 LOGGER.info("Fetching detials of apikey: " + topicName);
174 Topic topic = getMetaBroker(dmaapContext).getTopic(topicName);
177 LOGGER.error("Topic [" + topicName + "] does not exist.");
178 throw new TopicExistsException("Topic [" + topicName + "] does not exist.");
181 JSONObject json = new JSONObject();
182 json.put("topicName", topic.getName());
183 json.put("description", topic.getDescription());
184 json.put("owner", topic.getOwner());
186 LOGGER.info("Returning details of topic [" + topicName + "]. Sending response.");
187 DMaaPResponseBuilder.respondOk(dmaapContext, json);
193 * @param dmaapContext
196 private NsaApiDb<NsaSimpleApiKey> getApiKeyDb(DMaaPContext dmaapContext) {
197 return dmaapContext.getConfigReader().getfApiKeyDb();
203 * @param dmaapContext
206 private DMaaPKafkaMetaBroker getMetaBroker(DMaaPContext dmaapContext) {
207 return (DMaaPKafkaMetaBroker) dmaapContext.getConfigReader().getfMetaBroker();