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