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 org.onap.dmaap.dmf.mr.service.impl;
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.limits.Blacklist;
28 import com.att.nsa.security.NsaApiKey;
29 import com.att.nsa.security.ReadWriteSecuredResource.AccessDeniedException;
30 import org.json.JSONArray;
31 import org.json.JSONException;
32 import org.json.JSONObject;
33 import org.onap.dmaap.dmf.mr.backends.Consumer;
34 import org.onap.dmaap.dmf.mr.backends.ConsumerFactory;
35 import org.onap.dmaap.dmf.mr.beans.DMaaPContext;
36 import org.onap.dmaap.dmf.mr.security.DMaaPAuthenticatorImpl;
37 import org.onap.dmaap.dmf.mr.service.AdminService;
38 import org.onap.dmaap.dmf.mr.utils.DMaaPResponseBuilder;
39 import org.springframework.stereotype.Component;
41 import java.io.IOException;
42 import java.util.Collection;
47 * @author muzainulhaque.qazi
51 public class AdminServiceImpl implements AdminService {
53 //private Logger log = Logger.getLogger(AdminServiceImpl.class.toString());
54 private static final EELFLogger log = EELFManager.getInstance().getLogger(AdminServiceImpl.class);
56 * getConsumerCache returns consumer cache
57 * @param dMaaPContext context
58 * @throws IOException ex
59 * @throws AccessDeniedException
62 public void showConsumerCache(DMaaPContext dMaaPContext) throws IOException, AccessDeniedException {
63 adminAuthenticate(dMaaPContext);
65 JSONObject consumers = new JSONObject();
66 JSONArray jsonConsumersList = new JSONArray();
68 for (Consumer consumer : getConsumerFactory(dMaaPContext).getConsumers()) {
69 JSONObject consumerObject = new JSONObject();
70 consumerObject.put("name", consumer.getName());
71 consumerObject.put("created", consumer.getCreateTimeMs());
72 consumerObject.put("accessed", consumer.getLastAccessMs());
73 jsonConsumersList.put(consumerObject);
76 consumers.put("consumers", jsonConsumersList);
77 log.info("========== AdminServiceImpl: getConsumerCache: " + jsonConsumersList.toString() + "===========");
78 DMaaPResponseBuilder.respondOk(dMaaPContext, consumers);
83 * dropConsumerCache() method clears consumer cache
84 * @param dMaaPContext context
85 * @throws JSONException ex
86 * @throws IOException ex
87 * @throws AccessDeniedException
91 public void dropConsumerCache(DMaaPContext dMaaPContext) throws JSONException, IOException, AccessDeniedException {
92 adminAuthenticate(dMaaPContext);
93 getConsumerFactory(dMaaPContext).dropCache();
94 DMaaPResponseBuilder.respondOkWithHtml(dMaaPContext, "Consumer cache cleared successfully");
95 // log.info("========== AdminServiceImpl: dropConsumerCache: Consumer
96 // Cache successfully dropped.===========");
100 * getfConsumerFactory returns CosnumerFactory details
101 * @param dMaaPContext contxt
102 * @return ConsumerFactory obj
105 private ConsumerFactory getConsumerFactory(DMaaPContext dMaaPContext) {
106 return dMaaPContext.getConfigReader().getfConsumerFactory();
111 * @param dMaaPContext context
112 * @return blacklist obj
114 private static Blacklist getIpBlacklist(DMaaPContext dMaaPContext) {
115 return dMaaPContext.getConfigReader().getfIpBlackList();
120 * Get list of blacklisted ips
123 public void getBlacklist ( DMaaPContext dMaaPContext ) throws IOException, AccessDeniedException
125 adminAuthenticate ( dMaaPContext );
127 DMaaPResponseBuilder.respondOk ( dMaaPContext,
128 new JSONObject().put ( "blacklist",
129 setToJsonArray ( getIpBlacklist (dMaaPContext).asSet() ) ) );
132 public static JSONArray setToJsonArray ( Set<?> fields )
134 return collectionToJsonArray ( fields );
137 public static JSONArray collectionToJsonArray ( Collection<?> fields )
139 final JSONArray a = new JSONArray ();
140 if ( fields != null )
142 for ( Object o : fields )
151 * Add ip to blacklist
154 public void addToBlacklist ( DMaaPContext dMaaPContext, String ip ) throws IOException, ConfigDbException, AccessDeniedException
156 adminAuthenticate ( dMaaPContext );
158 getIpBlacklist (dMaaPContext).add ( ip );
159 DMaaPResponseBuilder.respondOkNoContent ( dMaaPContext );
163 * Remove ip from blacklist
166 public void removeFromBlacklist ( DMaaPContext dMaaPContext, String ip ) throws IOException, ConfigDbException, AccessDeniedException
168 adminAuthenticate ( dMaaPContext );
170 getIpBlacklist (dMaaPContext).remove ( ip );
171 DMaaPResponseBuilder.respondOkNoContent ( dMaaPContext );
175 * Authenticate if user is admin
176 * @param dMaaPContext context
177 * @throws AccessDeniedException ex
179 private static void adminAuthenticate ( DMaaPContext dMaaPContext ) throws AccessDeniedException
182 final NsaApiKey user = DMaaPAuthenticatorImpl.getAuthenticatedUser(dMaaPContext);
183 if ( user == null || !user.getKey ().equals ( "admin" ) )
185 throw new AccessDeniedException ();