Only create AAF Perm when needed
[dmaap/dbcapi.git] / src / main / java / org / onap / dmaap / dbcapi / service / DmaapService.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.onap.dmaap
4  * ================================================================================
5  * Copyright (C) 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  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.dmaap.dbcapi.service;
22
23 import java.util.ArrayList;
24
25
26
27
28
29
30
31
32
33
34 import org.onap.dmaap.dbcapi.aaf.AafService;
35 import org.onap.dmaap.dbcapi.aaf.DmaapGrant;
36 import org.onap.dmaap.dbcapi.aaf.DmaapPerm;
37 import org.onap.dmaap.dbcapi.aaf.AafService.ServiceType;
38 import org.onap.dmaap.dbcapi.authentication.ApiPerms;
39 import org.onap.dmaap.dbcapi.authentication.ApiPolicy;
40 import org.onap.dmaap.dbcapi.database.DatabaseClass;
41 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
42 import org.onap.dmaap.dbcapi.logging.DmaapbcLogMessageEnum;
43 import org.onap.dmaap.dbcapi.model.ApiError;
44 import org.onap.dmaap.dbcapi.model.Dmaap;
45 import org.onap.dmaap.dbcapi.model.MR_Client;
46 import org.onap.dmaap.dbcapi.model.Topic;
47 import org.onap.dmaap.dbcapi.model.DmaapObject.DmaapObject_Status;
48 import org.onap.dmaap.dbcapi.util.DmaapConfig;
49 import org.onap.dmaap.dbcapi.util.Singleton;
50
51 public class DmaapService  extends BaseLoggingClass  {
52
53         
54         private Singleton<Dmaap> dmaapholder = DatabaseClass.getDmaap();
55         
56         
57         String topicFactory; // = "org.openecomp.dcae.dmaap.topicFactory";
58         String topicMgrRole; // = "org.openecomp.dmaapBC.TopicMgr";
59         String dcaeTopicNs; // = "org.openecomp.dcae.dmaap";
60         private boolean multiSite;
61         
62         
63         public DmaapService() {
64                 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
65                 topicFactory = p.getProperty("MR.TopicFactoryNS", "MR.topicFactoryNS.not.set");
66                 topicMgrRole = p.getProperty("MR.TopicMgrRole", "MR.TopicMgrRole.not.set" );
67                 dcaeTopicNs = dmaapholder.get().getTopicNsRoot();
68                 multiSite = "true".equalsIgnoreCase(p.getProperty("MR.multisite", "true"));
69                 
70         }
71         
72         public Dmaap getDmaap() {
73                 logger.info( "entering getDmaap()" );
74                 return(dmaapholder.get());
75         }
76         
77         public Dmaap addDmaap( Dmaap nd ) {
78                 
79                 logger.info( "entering addDmaap()" );
80                 Dmaap dmaap = dmaapholder.get();
81                 if ( dmaap.getVersion().equals( "0")) {
82
83                         nd.setLastMod();
84                         dmaapholder.update(nd);
85
86                         AafService aaf = new AafService( ServiceType.AAF_Admin);
87                         ApiPolicy apiPolicy = new ApiPolicy();
88                         if ( apiPolicy.getUseAuthClass() ) {
89                                 ApiPerms p = new ApiPerms();
90                                 p.setEnvMap();
91                         }
92                         boolean anythingWrong = false;
93                         
94                         if ( multiSite ) {
95                                 anythingWrong = setTopicMgtPerms(  nd,  aaf ) || createMmaTopic();
96                         }
97                                         
98                         if ( anythingWrong ) {
99                                 dmaap.setStatus(DmaapObject_Status.INVALID); 
100                         }
101                         else {
102                                 dmaap.setStatus(DmaapObject_Status.VALID);  
103                         }
104                         dmaap.setLastMod();
105                         dmaapholder.update(dmaap);
106
107                         return dmaap;
108                 
109                 }
110                 else { 
111                         return dmaap;
112                 }
113         }
114         
115         public Dmaap updateDmaap( Dmaap nd ) {
116                 logger.info( "entering updateDmaap()" );
117                 
118                 boolean anythingWrong = false;
119
120                 Dmaap dmaap = dmaapholder.get();
121                 
122                 // some triggers for when we attempt to reprovision perms and MMA topic:
123                 // - if the DMaaP Name changes
124                 // - if the version is 0  (this is a handy test to force this processing by updating the DB)
125                 // - if the object is invalid, reprocessing might fix it.
126                 if ( ! dmaap.isStatusValid()  || ! nd.getDmaapName().equals(dmaap.getDmaapName()) || dmaap.getVersion().equals( "0") ) {
127                         nd.setLastMod();
128                         dmaapholder.update(nd);  //need to set this so the following perms will pick up any new vals.
129                         ApiPolicy apiPolicy = new ApiPolicy();
130                         if ( apiPolicy.getUseAuthClass()) {
131                                 ApiPerms p = new ApiPerms();
132                                 p.setEnvMap();
133                         }
134                         AafService aaf = new AafService( ServiceType.AAF_Admin);
135                         if ( multiSite ) {
136                                 anythingWrong = setTopicMgtPerms(  nd,  aaf ) || createMmaTopic();
137                         }
138                 }
139                                         
140                 if ( anythingWrong ) {
141                         nd.setStatus(DmaapObject_Status.INVALID); 
142                 }
143                 else {
144                         nd.setStatus(DmaapObject_Status.VALID);  
145                 }
146                 nd.setLastMod();
147                 dmaapholder.update(nd);  // may need to update status...
148                 return(dmaapholder.get());
149                 
150         }
151         
152         public String getTopicPerm(){
153                 Dmaap dmaap = dmaapholder.get();
154                 return getTopicPerm( dmaap.getDmaapName() );
155         }
156         public String getTopicPerm( String val ) {
157                 Dmaap dmaap = dmaapholder.get();
158                 return dmaap.getTopicNsRoot() + "." + val + ".mr.topic";
159         }
160         
161         public String getBridgeAdminFqtn(){
162                 Dmaap dmaap = dmaapholder.get();
163                 String topic = dmaap.getBridgeAdminTopic();
164                 
165                 // check if this is already an fqtn (contains a dot)
166                 // otherwise build it
167                 if ( topic.indexOf('.') < 0 ) {
168                         topic = dmaap.getTopicNsRoot() + "." + dmaap.getDmaapName() + "." + dmaap.getBridgeAdminTopic();
169                 }
170                 return( topic );
171         }
172
173         private boolean setTopicMgtPerms( Dmaap nd, AafService aaf ){
174                 String[] actions = { "create", "destroy" };
175                 String instance = ":" + dcaeTopicNs + "." + nd.getDmaapName() + ".mr.topic:" + dcaeTopicNs + "." + nd.getDmaapName();
176                 
177                 for( String action : actions ) {
178
179                         DmaapPerm perm = new DmaapPerm( topicFactory, instance, action );
180                 
181                         int rc = aaf.addPerm( perm );
182                         if ( rc != 201 &&  rc != 409 ) {
183                                 logger.error( "unable to add perm for "+ topicFactory + "|" + instance + "|" + action );
184                                 return true;
185                         }
186
187                         DmaapGrant grant = new DmaapGrant( perm, topicMgrRole );
188                         rc = aaf.addGrant( grant );
189                         if ( rc != 201 && rc != 409 ) {
190                                 logger.error( "unable to grant to " + topicMgrRole + " perm for "+ topicFactory + "|" + instance + "|" + action );
191                                 return true;
192                         }
193                 }
194                 
195                 String t = dcaeTopicNs +"." + nd.getDmaapName() + ".mr.topic";
196                 String[] s = { "view", "pub", "sub" };
197                 actions = s;
198                 instance = "*";
199                 
200                 for( String action : actions ) {
201
202                         DmaapPerm perm = new DmaapPerm( t, instance, action );
203                 
204                         int rc = aaf.addPerm( perm );
205                         if ( rc != 201 &&  rc != 409 ) {
206                                 errorLogger.error( DmaapbcLogMessageEnum.AAF_UNEXPECTED_RESPONSE, Integer.toString(rc), "add perm", t + "|" + instance + "|" + action );
207                                 return true;
208                         }
209
210                         DmaapGrant grant = new DmaapGrant( perm, topicMgrRole );
211                         rc = aaf.addGrant( grant );
212                         if ( rc != 201 && rc != 409 ) {
213                                 errorLogger.error( DmaapbcLogMessageEnum.AAF_UNEXPECTED_RESPONSE, Integer.toString(rc), "grant to " + topicMgrRole + " perm ", topicFactory + "|" + instance + "|" + action );
214                                 return true;
215                         }
216                                 
217                 }
218                 return false;
219         }
220
221         public boolean testCreateMmaTopic() {
222
223                 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
224                 String unit_test = p.getProperty( "UnitTest", "No" );
225                 if ( unit_test.equals( "Yes" ) ) {
226                         return createMmaTopic();
227                 }
228                 return false;
229         }
230         
231         // create the special topic for MMA provisioning.
232         // return true indicating a problem in topic creation, 
233         // else false means it was ok  (created or previously existed)
234         private boolean createMmaTopic() {
235                 boolean rc = true;
236                 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
237                 Dmaap dmaap = dmaapholder.get();
238                 
239                 ArrayList<MR_Client> clients = new ArrayList<MR_Client>();
240                 String[] actions = { "pub", "sub", "view" };
241                 String centralMR = new DcaeLocationService().getCentralLocation();
242                 if ( centralMR == null ) {
243                         return rc;
244                 }
245                 logger.info( "Location for " + dmaap.getBridgeAdminTopic() + " is " + centralMR );
246         
247                 // first client is the Role used by Bus Controller to send messages to MMA
248                 String provRole = p.getProperty("MM.ProvRole");
249                 MR_Client nClient = new MR_Client();
250                 nClient.setAction(actions);
251                 nClient.setClientRole(provRole);
252                 nClient.setDcaeLocationName(centralMR);
253                 clients.add( nClient );
254         
255                 // second client is the Role used by MMA to listen to messages from Bus Controller
256                 String agentRole = p.getProperty("MM.AgentRole");
257                 nClient = new MR_Client();
258                 nClient.setAction(actions);
259                 nClient.setClientRole(agentRole);
260                 nClient.setDcaeLocationName(centralMR);
261                 clients.add( nClient );
262         
263                 // initialize Topic
264                 Topic mmaTopic = new Topic();
265                 mmaTopic.setTopicName(dmaap.getBridgeAdminTopic());
266                 mmaTopic.setClients(clients);
267                 mmaTopic.setOwner("BusController");
268                 mmaTopic.setTopicDescription("topic reserved for MirrorMaker Administration");
269                 mmaTopic.setTnxEnabled("false");
270                 
271                 ApiError err = new ApiError();
272                 TopicService svc = new TopicService();
273                 try {
274                         @SuppressWarnings("unused")
275                         Topic nTopic = svc.addTopic(mmaTopic, err);
276                         if ( err.is2xx() || err.getCode() == 409 ) {
277                                 return false;
278                         }
279                 } catch ( Exception e) {
280                         errorLogger.error( DmaapbcLogMessageEnum.UNEXPECTED_CONDITION, " while adding Topic: " + e.getMessage());
281                 }
282                 errorLogger.error( DmaapbcLogMessageEnum.TOPIC_CREATE_ERROR,  dmaap.getBridgeAdminTopic(), err.getFields(), err.getFields(), err.getMessage());
283                 
284                 return rc;
285                 
286         }
287 }