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