fixes for sonar vulnerabilities
[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.JSONException;
34 import org.json.JSONObject;
35 import org.springframework.stereotype.Service;
36
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;
48 /**
49  * @author muzainulhaque.qazi
50  *
51  */
52 @Service
53 public class UIServiceImpl implements UIService {
54
55         
56         private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(UIServiceImpl.class);
57         /**
58          * Returning template of hello page
59          * @param dmaapContext
60          * @throws IOException
61          */
62         @Override
63         public void hello(DMaaPContext dmaapContext) throws IOException {
64                 LOGGER.info("Returning template of hello page.");
65                 DMaaPResponseBuilder.respondOkWithHtml(dmaapContext, "templates/hello.html");
66         }
67
68         /**
69          * Fetching list of all api keys and returning in a templated form for display.
70          * @param dmaapContext
71          * @throws ConfigDbException
72          * @throws IOException
73          */
74         @Override
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
77                 // the template
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();
80
81                 LinkedList<JSONObject> keyList = new LinkedList<>();
82
83                 JSONObject jsonList = new JSONObject();
84
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);
92                 }
93
94                 jsonList.put("apiKeys", keyList);
95
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);
99
100         }
101
102         /**
103          * @param dmaapContext
104          * @param apiKey
105          * @throws ConfigDbException 
106          * @throws IOException 
107          * @throws JSONException 
108          * @throws Exception
109          */
110         @Override
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
113                 // the template
114                 LOGGER.info("Fetching detials of apikey: " + apiKey);
115                 final NsaSimpleApiKey key = getApiKeyDb(dmaapContext).loadApiKey(apiKey);
116
117                 if (null != key) {
118                         LOGGER.info("Details of apikey [" + apiKey + "] found. Returning response");
119                         DMaaPResponseBuilder.respondOk(dmaapContext, key.asJsonObject());
120                 } else {
121                         LOGGER.info("Details of apikey [" + apiKey + "] not found. Returning response");
122                         throw new CambriaApiException(400,"Key [" + apiKey + "] not found.");
123                 }
124
125         }
126
127         /**
128          * Fetching list of all the topics
129          * @param dmaapContext
130          * @throws ConfigDbException
131          * @throws IOException
132          */
133         @Override
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
136                 // the template
137                 LOGGER.info("Fetching list of all the topics and returning in a templated form for display");
138                 List<Topic> topicsList = getMetaBroker(dmaapContext).getAllTopics();
139
140                 JSONObject jsonObject = new JSONObject();
141
142                 JSONArray topicsArray = new JSONArray();
143
144                 List<Topic> topicList = getMetaBroker(dmaapContext).getAllTopics();
145
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);
152                 }
153
154                 jsonObject.put("topics", topicsList);
155
156                 LOGGER.info("Returning the list of topics in templated format for display.");
157                 DMaaPResponseBuilder.respondOk(dmaapContext, jsonObject);
158
159         }
160
161         /**
162          * @param dmaapContext
163          * @param topicName
164          * @throws ConfigDbException
165          * @throws IOException
166          * @throws TopicExistsException
167          */
168         @Override
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
172                 // the template
173                 LOGGER.info("Fetching detials of apikey: " + topicName);
174                 Topic topic = getMetaBroker(dmaapContext).getTopic(topicName);
175
176                 if (null == topic) {
177                         LOGGER.error("Topic [" + topicName + "] does not exist.");
178                         throw new TopicExistsException("Topic [" + topicName + "] does not exist.");
179                 }
180
181                 JSONObject json = new JSONObject();
182                 json.put("topicName", topic.getName());
183                 json.put("description", topic.getDescription());
184                 json.put("owner", topic.getOwner());
185
186                 LOGGER.info("Returning details of topic [" + topicName + "]. Sending response.");
187                 DMaaPResponseBuilder.respondOk(dmaapContext, json);
188
189         }
190
191         /**
192          * 
193          * @param dmaapContext
194          * @return
195          */
196         private NsaApiDb<NsaSimpleApiKey> getApiKeyDb(DMaaPContext dmaapContext) {
197                 return dmaapContext.getConfigReader().getfApiKeyDb();
198
199         }
200
201         /**
202          * 
203          * @param dmaapContext
204          * @return
205          */
206         private DMaaPKafkaMetaBroker getMetaBroker(DMaaPContext dmaapContext) {
207                 return (DMaaPKafkaMetaBroker) dmaapContext.getConfigReader().getfMetaBroker();
208         }
209
210 }