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 java.io.IOException;
25 import java.util.Collection;
28 import org.json.JSONArray;
29 import org.json.JSONException;
30 import org.json.JSONObject;
31 import org.springframework.stereotype.Component;
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 com.att.eelf.configuration.EELFLogger;
40 import com.att.eelf.configuration.EELFManager;
41 import com.att.nsa.configs.ConfigDbException;
42 import com.att.nsa.limits.Blacklist;
43 import com.att.nsa.security.NsaApiKey;
44 import com.att.nsa.security.ReadWriteSecuredResource.AccessDeniedException;
48 * @author muzainulhaque.qazi
52 public class AdminServiceImpl implements AdminService {
54 //private Logger log = Logger.getLogger(AdminServiceImpl.class.toString());
55 private static final EELFLogger log = EELFManager.getInstance().getLogger(AdminServiceImpl.class);
57 * getConsumerCache returns consumer cache
58 * @param dMaaPContext context
59 * @throws IOException ex
60 * @throws AccessDeniedException
63 public void showConsumerCache(DMaaPContext dMaaPContext) throws IOException, AccessDeniedException {
64 adminAuthenticate(dMaaPContext);
66 JSONObject consumers = new JSONObject();
67 JSONArray jsonConsumersList = new JSONArray();
69 for (Consumer consumer : getConsumerFactory(dMaaPContext).getConsumers()) {
70 JSONObject consumerObject = new JSONObject();
71 consumerObject.put("name", consumer.getName());
72 consumerObject.put("created", consumer.getCreateTimeMs());
73 consumerObject.put("accessed", consumer.getLastAccessMs());
74 jsonConsumersList.put(consumerObject);
77 consumers.put("consumers", jsonConsumersList);
78 log.info("========== AdminServiceImpl: getConsumerCache: " + jsonConsumersList.toString() + "===========");
79 DMaaPResponseBuilder.respondOk(dMaaPContext, consumers);
84 * dropConsumerCache() method clears consumer cache
85 * @param dMaaPContext context
86 * @throws JSONException ex
87 * @throws IOException ex
88 * @throws AccessDeniedException
92 public void dropConsumerCache(DMaaPContext dMaaPContext) throws JSONException, IOException, AccessDeniedException {
93 adminAuthenticate(dMaaPContext);
94 getConsumerFactory(dMaaPContext).dropCache();
95 DMaaPResponseBuilder.respondOkWithHtml(dMaaPContext, "Consumer cache cleared successfully");
96 // log.info("========== AdminServiceImpl: dropConsumerCache: Consumer
97 // Cache successfully dropped.===========");
101 * getfConsumerFactory returns CosnumerFactory details
102 * @param dMaaPContext contxt
103 * @return ConsumerFactory obj
106 private ConsumerFactory getConsumerFactory(DMaaPContext dMaaPContext) {
107 return dMaaPContext.getConfigReader().getfConsumerFactory();
112 * @param dMaaPContext context
113 * @return blacklist obj
115 private static Blacklist getIpBlacklist(DMaaPContext dMaaPContext) {
116 return dMaaPContext.getConfigReader().getfIpBlackList();
121 * Get list of blacklisted ips
124 public void getBlacklist ( DMaaPContext dMaaPContext ) throws IOException, AccessDeniedException
126 adminAuthenticate ( dMaaPContext );
128 DMaaPResponseBuilder.respondOk ( dMaaPContext,
129 new JSONObject().put ( "blacklist",
130 setToJsonArray ( getIpBlacklist (dMaaPContext).asSet() ) ) );
133 public static JSONArray setToJsonArray ( Set<?> fields )
135 return collectionToJsonArray ( fields );
138 public static JSONArray collectionToJsonArray ( Collection<?> fields )
140 final JSONArray a = new JSONArray ();
141 if ( fields != null )
143 for ( Object o : fields )
152 * Add ip to blacklist
155 public void addToBlacklist ( DMaaPContext dMaaPContext, String ip ) throws IOException, ConfigDbException, AccessDeniedException
157 adminAuthenticate ( dMaaPContext );
159 getIpBlacklist (dMaaPContext).add ( ip );
160 DMaaPResponseBuilder.respondOkNoContent ( dMaaPContext );
164 * Remove ip from blacklist
167 public void removeFromBlacklist ( DMaaPContext dMaaPContext, String ip ) throws IOException, ConfigDbException, AccessDeniedException
169 adminAuthenticate ( dMaaPContext );
171 getIpBlacklist (dMaaPContext).remove ( ip );
172 DMaaPResponseBuilder.respondOkNoContent ( dMaaPContext );
176 * Authenticate if user is admin
177 * @param dMaaPContext context
178 * @throws AccessDeniedException ex
180 private static void adminAuthenticate ( DMaaPContext dMaaPContext ) throws AccessDeniedException
183 final NsaApiKey user = DMaaPAuthenticatorImpl.getAuthenticatedUser(dMaaPContext);
184 if ( user == null || !user.getKey ().equals ( "admin" ) )
186 throw new AccessDeniedException ();