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 com.att.nsa.cambria.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 com.att.eelf.configuration.EELFLogger;
34 import com.att.eelf.configuration.EELFManager;
35 import com.att.nsa.cambria.backends.Consumer;
36 import com.att.nsa.cambria.backends.ConsumerFactory;
37 import com.att.nsa.cambria.beans.DMaaPContext;
38 import com.att.nsa.cambria.security.DMaaPAuthenticatorImpl;
39 import com.att.nsa.cambria.service.AdminService;
40 import com.att.nsa.cambria.utils.DMaaPResponseBuilder;
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;
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", setToJsonArray ( getIpBlacklist (dMaaPContext).asSet() ) ) );
132 * Add ip to blacklist
135 public void addToBlacklist ( DMaaPContext dMaaPContext, String ip ) throws IOException, ConfigDbException, AccessDeniedException
137 adminAuthenticate ( dMaaPContext );
139 getIpBlacklist (dMaaPContext).add ( ip );
140 DMaaPResponseBuilder.respondOkNoContent ( dMaaPContext );
144 * Remove ip from blacklist
147 public void removeFromBlacklist ( DMaaPContext dMaaPContext, String ip ) throws IOException, ConfigDbException, AccessDeniedException
149 adminAuthenticate ( dMaaPContext );
151 getIpBlacklist (dMaaPContext).remove ( ip );
152 DMaaPResponseBuilder.respondOkNoContent ( dMaaPContext );
156 * Authenticate if user is admin
157 * @param dMaaPContext context
158 * @throws AccessDeniedException ex
160 private static void adminAuthenticate ( DMaaPContext dMaaPContext ) throws AccessDeniedException
163 final NsaApiKey user = DMaaPAuthenticatorImpl.getAuthenticatedUser(dMaaPContext);
164 if ( (user == null || !user.getKey ().equals ( "admin" )) )
166 throw new AccessDeniedException ();
170 public static JSONArray setToJsonArray ( Set<?> fields )
172 return collectionToJsonArray ( fields );
175 public static JSONArray collectionToJsonArray ( Collection<?> fields )
177 final JSONArray a = new JSONArray ();
178 if ( fields != null )
180 for ( Object o : fields )