Sonar major issues
[dmaap/messagerouter/msgrtr.git] / src / main / java / com / att / dmf / mr / service / impl / UIServiceImpl.java
1 /*******************************************************************************
2  *  ============LICENSE_START=======================================================
3  *  org.onap.dmaap
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
11 *  
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=========================================================
18  *  
19  *  ECOMP is a trademark and service mark of AT&T Intellectual Property.
20  *  
21  *******************************************************************************/
22 package com.att.dmf.mr.service.impl;
23
24 import java.io.IOException;
25 import java.util.LinkedList;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Map.Entry;
29
30
31 import org.apache.kafka.common.errors.TopicExistsException;
32 import org.json.JSONArray;
33 import org.json.JSONObject;
34 import org.springframework.stereotype.Service;
35
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;
46 /**
47  * @author muzainulhaque.qazi
48  *
49  */
50 @Service
51 public class UIServiceImpl implements UIService {
52
53         
54         private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(UIServiceImpl.class);
55         /**
56          * Returning template of hello page
57          * @param dmaapContext
58          * @throws IOException
59          */
60         @Override
61         public void hello(DMaaPContext dmaapContext) throws IOException {
62                 LOGGER.info("Returning template of hello page.");
63                 DMaaPResponseBuilder.respondOkWithHtml(dmaapContext, "templates/hello.html");
64         }
65
66         /**
67          * Fetching list of all api keys and returning in a templated form for display.
68          * @param dmaapContext
69          * @throws ConfigDbException
70          * @throws IOException
71          */
72         @Override
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
75                 // the template
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();
78
79                 LinkedList<JSONObject> keyList = new LinkedList<>();
80
81                 JSONObject jsonList = new JSONObject();
82
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);
90                 }
91
92                 jsonList.put("apiKeys", keyList);
93
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);
97
98         }
99
100         /**
101          * @param dmaapContext
102          * @param apiKey
103          * @throws Exception
104          */
105         @Override
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
108                 // the template
109                 LOGGER.info("Fetching detials of apikey: " + apiKey);
110                 final NsaSimpleApiKey key = getApiKeyDb(dmaapContext).loadApiKey(apiKey);
111
112                 if (null != key) {
113                         LOGGER.info("Details of apikey [" + apiKey + "] found. Returning response");
114                         DMaaPResponseBuilder.respondOk(dmaapContext, key.asJsonObject());
115                 } else {
116                         LOGGER.info("Details of apikey [" + apiKey + "] not found. Returning response");
117                         throw new Exception("Key [" + apiKey + "] not found.");
118                 }
119
120         }
121
122         /**
123          * Fetching list of all the topics
124          * @param dmaapContext
125          * @throws ConfigDbException
126          * @throws IOException
127          */
128         @Override
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
131                 // the template
132                 LOGGER.info("Fetching list of all the topics and returning in a templated form for display");
133                 List<Topic> topicsList = getMetaBroker(dmaapContext).getAllTopics();
134
135                 JSONObject jsonObject = new JSONObject();
136
137                 JSONArray topicsArray = new JSONArray();
138
139                 List<Topic> topicList = getMetaBroker(dmaapContext).getAllTopics();
140
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);
147                 }
148
149                 jsonObject.put("topics", topicsList);
150
151                 LOGGER.info("Returning the list of topics in templated format for display.");
152                 DMaaPResponseBuilder.respondOk(dmaapContext, jsonObject);
153
154         }
155
156         /**
157          * @param dmaapContext
158          * @param topicName
159          * @throws ConfigDbException
160          * @throws IOException
161          * @throws TopicExistsException
162          */
163         @Override
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
167                 // the template
168                 LOGGER.info("Fetching detials of apikey: " + topicName);
169                 Topic topic = getMetaBroker(dmaapContext).getTopic(topicName);
170
171                 if (null == topic) {
172                         LOGGER.error("Topic [" + topicName + "] does not exist.");
173                         throw new TopicExistsException("Topic [" + topicName + "] does not exist.");
174                 }
175
176                 JSONObject json = new JSONObject();
177                 json.put("topicName", topic.getName());
178                 json.put("description", topic.getDescription());
179                 json.put("owner", topic.getOwner());
180
181                 LOGGER.info("Returning details of topic [" + topicName + "]. Sending response.");
182                 DMaaPResponseBuilder.respondOk(dmaapContext, json);
183
184         }
185
186         /**
187          * 
188          * @param dmaapContext
189          * @return
190          */
191         private NsaApiDb<NsaSimpleApiKey> getApiKeyDb(DMaaPContext dmaapContext) {
192                 return dmaapContext.getConfigReader().getfApiKeyDb();
193
194         }
195
196         /**
197          * 
198          * @param dmaapContext
199          * @return
200          */
201         private DMaaPKafkaMetaBroker getMetaBroker(DMaaPContext dmaapContext) {
202                 return (DMaaPKafkaMetaBroker) dmaapContext.getConfigReader().getfMetaBroker();
203         }
204
205 }