Merge "sonar critical for Conditional Statement"
[dmaap/messagerouter/msgrtr.git] / src / main / java / com / att / nsa / cambria / 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.nsa.cambria.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.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;
45
46 /**
47  * @author author
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", setToJsonArray ( getIpBlacklist (dMaaPContext).asSet() ) ) );
129         }
130         
131         /**
132          * Add ip to blacklist
133          */
134         @Override
135         public void addToBlacklist ( DMaaPContext dMaaPContext, String ip ) throws IOException, ConfigDbException, AccessDeniedException
136         {
137                 adminAuthenticate ( dMaaPContext );
138
139                 getIpBlacklist (dMaaPContext).add ( ip );
140                 DMaaPResponseBuilder.respondOkNoContent ( dMaaPContext );
141         }
142         
143         /**
144          * Remove ip from blacklist
145          */
146         @Override
147         public void removeFromBlacklist ( DMaaPContext dMaaPContext, String ip ) throws IOException, ConfigDbException, AccessDeniedException
148         {
149                 adminAuthenticate ( dMaaPContext );
150
151                 getIpBlacklist (dMaaPContext).remove ( ip );
152                 DMaaPResponseBuilder.respondOkNoContent ( dMaaPContext );
153         }
154         
155         /**
156          * Authenticate if user is admin
157          * @param dMaaPContext context
158          * @throws AccessDeniedException ex
159          */
160         private static void adminAuthenticate ( DMaaPContext dMaaPContext ) throws AccessDeniedException
161         {
162                 
163                 final NsaApiKey user = DMaaPAuthenticatorImpl.getAuthenticatedUser(dMaaPContext);
164                 if ( (user == null || !user.getKey ().equals ( "admin" )) )
165                 {
166                         throw new AccessDeniedException ();
167                 }
168         }
169         
170         public static JSONArray setToJsonArray ( Set<?> fields )
171         {
172                 return collectionToJsonArray ( fields );
173         }
174
175         public static JSONArray collectionToJsonArray ( Collection<?> fields )
176         {
177                 final JSONArray a = new JSONArray ();
178                 if ( fields != null )
179                 {
180                         for ( Object o : fields )
181                         {
182                                 a.put ( o );
183                         }
184                 }
185                 return a;
186         }
187
188 }