f7c48de6ca2ee45d6353cd0980327eedbc131ab5
[dmaap/messagerouter/msgrtr.git] / src / main / java / com / att / dmf / mr / service / impl / AdminServiceImpl.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.Collection;
26 import java.util.Set;
27
28 import org.json.JSONArray;
29 import org.json.JSONException;
30 import org.json.JSONObject;
31 import org.springframework.stereotype.Component;
32
33 import com.att.dmf.mr.backends.Consumer;
34 import com.att.dmf.mr.backends.ConsumerFactory;
35 import com.att.dmf.mr.beans.DMaaPContext;
36 import com.att.dmf.mr.security.DMaaPAuthenticatorImpl;
37 import com.att.dmf.mr.service.AdminService;
38 import com.att.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;
45
46
47 /**
48  * @author muzainulhaque.qazi
49  *
50  */
51 @Component
52 public class AdminServiceImpl implements AdminService {
53
54         //private Logger log = Logger.getLogger(AdminServiceImpl.class.toString());
55         private static final EELFLogger log = EELFManager.getInstance().getLogger(AdminServiceImpl.class);
56         /**
57          * getConsumerCache returns consumer cache
58          * @param dMaaPContext context
59          * @throws IOException ex
60          * @throws AccessDeniedException 
61          */
62         @Override       
63         public void showConsumerCache(DMaaPContext dMaaPContext) throws IOException, AccessDeniedException {
64                 adminAuthenticate(dMaaPContext);
65                 
66                 JSONObject consumers = new JSONObject();
67                 JSONArray jsonConsumersList = new JSONArray();
68
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);
75                 }
76
77                 consumers.put("consumers", jsonConsumersList);
78                 log.info("========== AdminServiceImpl: getConsumerCache: " + jsonConsumersList.toString() + "===========");
79                 DMaaPResponseBuilder.respondOk(dMaaPContext, consumers);
80         }
81
82         /**
83          * 
84          * dropConsumerCache() method clears consumer cache
85          * @param dMaaPContext context
86          * @throws JSONException ex
87          * @throws IOException ex
88          * @throws AccessDeniedException 
89          * 
90          */
91         @Override
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.===========");
98         }
99
100         /** 
101          * getfConsumerFactory returns CosnumerFactory details
102          * @param dMaaPContext contxt
103          * @return ConsumerFactory obj
104          * 
105          */
106         private ConsumerFactory getConsumerFactory(DMaaPContext dMaaPContext) {
107                 return dMaaPContext.getConfigReader().getfConsumerFactory();
108         }
109         
110         /**
111          * return ipblacklist
112          * @param dMaaPContext context
113          * @return blacklist obj
114          */
115         private static Blacklist getIpBlacklist(DMaaPContext dMaaPContext) {
116                 return dMaaPContext.getConfigReader().getfIpBlackList();
117         }
118         
119         
120         /**
121          * Get list of blacklisted ips
122          */
123         @Override
124         public void getBlacklist ( DMaaPContext dMaaPContext ) throws IOException, AccessDeniedException
125         {
126                 adminAuthenticate ( dMaaPContext );
127
128                 DMaaPResponseBuilder.respondOk ( dMaaPContext,
129                         new JSONObject().put ( "blacklist",
130                                         setToJsonArray ( getIpBlacklist (dMaaPContext).asSet() ) ) );
131         }
132         
133         public static JSONArray setToJsonArray ( Set<?> fields )
134         {
135                 return collectionToJsonArray ( fields );
136         }
137         
138         public static JSONArray collectionToJsonArray ( Collection<?> fields )
139         {
140                 final JSONArray a = new JSONArray ();
141                 if ( fields != null )
142                 {
143                         for ( Object o : fields )
144                         {
145                                 a.put ( o );
146                         }
147                 }
148                 return a;
149         }
150         
151         /**
152          * Add ip to blacklist
153          */
154         @Override
155         public void addToBlacklist ( DMaaPContext dMaaPContext, String ip ) throws IOException, ConfigDbException, AccessDeniedException
156         {
157                 adminAuthenticate ( dMaaPContext );
158
159                 getIpBlacklist (dMaaPContext).add ( ip );
160                 DMaaPResponseBuilder.respondOkNoContent ( dMaaPContext );
161         }
162         
163         /**
164          * Remove ip from blacklist
165          */
166         @Override
167         public void removeFromBlacklist ( DMaaPContext dMaaPContext, String ip ) throws IOException, ConfigDbException, AccessDeniedException
168         {
169                 adminAuthenticate ( dMaaPContext );
170
171                 getIpBlacklist (dMaaPContext).remove ( ip );
172                 DMaaPResponseBuilder.respondOkNoContent ( dMaaPContext );
173         }
174         
175         /**
176          * Authenticate if user is admin
177          * @param dMaaPContext context
178          * @throws AccessDeniedException ex
179          */
180         private static void adminAuthenticate ( DMaaPContext dMaaPContext ) throws AccessDeniedException
181         {
182                 
183                 final NsaApiKey user = DMaaPAuthenticatorImpl.getAuthenticatedUser(dMaaPContext);
184                 if ( user == null || !user.getKey ().equals ( "admin" ) )
185                 {
186                         throw new AccessDeniedException ();
187                 }
188         }
189
190 }