Introduce default pub/sub Roles for Topic
[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.ReplicationType;
47 import org.onap.dmaap.dbcapi.model.Topic;
48 import org.onap.dmaap.dbcapi.model.DmaapObject.DmaapObject_Status;
49 import org.onap.dmaap.dbcapi.util.DmaapConfig;
50 import org.onap.dmaap.dbcapi.util.Singleton;
51
52 public class DmaapService  extends BaseLoggingClass  {
53
54         
55         private Singleton<Dmaap> dmaapholder = DatabaseClass.getDmaap();
56         private static String noEnvironmentPrefix;
57         
58         
59         String topicFactory; // = "org.openecomp.dcae.dmaap.topicFactory";
60         String topicMgrRole; // = "org.openecomp.dmaapBC.TopicMgr";
61         String dcaeTopicNs; // = "org.openecomp.dcae.dmaap";
62         private boolean multiSite;
63         
64         
65         public DmaapService() {
66                 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
67                 topicFactory = p.getProperty("MR.TopicFactoryNS", "MR.topicFactoryNS.not.set");
68                 topicMgrRole = p.getProperty("MR.TopicMgrRole", "MR.TopicMgrRole.not.set" );
69                 dcaeTopicNs = dmaapholder.get().getTopicNsRoot();
70                 multiSite = "true".equalsIgnoreCase(p.getProperty("MR.multisite", "true"));
71                 noEnvironmentPrefix = p.getProperty( "AAF.NoEnvironmentPrefix", "org.onap");
72                 
73                 logger.info( "DmaapService settings: " + 
74                                 " topicFactory=" + topicFactory +
75                                 " topicMgrRole=" + topicMgrRole +
76                                 " dcaeTopicNs=" + dcaeTopicNs +
77                                 " multisite=" + multiSite +
78                                 " noEnvironmentPrefix=" + noEnvironmentPrefix
79                                 );
80                 
81         }
82         
83         public Dmaap getDmaap() {
84                 logger.info( "entering getDmaap()" );
85                 return(dmaapholder.get());
86         }
87         
88         public Dmaap addDmaap( Dmaap nd ) {
89                 
90                 logger.info( "entering addDmaap()" );
91                 Dmaap dmaap = dmaapholder.get();
92                 if ( dmaap.getVersion().equals( "0")) {
93
94                         nd.setLastMod();
95                         dmaapholder.update(nd);
96
97                         AafService aaf = new AafService( ServiceType.AAF_Admin);
98                         ApiPolicy apiPolicy = new ApiPolicy();
99                         if ( apiPolicy.getUseAuthClass() ) {
100                                 ApiPerms p = new ApiPerms();
101                                 p.setEnvMap();
102                         }
103                         boolean anythingWrong = false;
104                         
105                         if ( multiSite ) {
106                                 anythingWrong = setTopicMgtPerms(  nd,  aaf ) || createMmaTopic();
107                         }
108                                         
109                         if ( anythingWrong ) {
110                                 dmaap.setStatus(DmaapObject_Status.INVALID); 
111                         }
112                         else {
113                                 dmaap.setStatus(DmaapObject_Status.VALID);  
114                         }
115                         dmaap.setLastMod();
116                         dmaapholder.update(dmaap);
117
118                         return dmaap;
119                 
120                 }
121                 else { 
122                         return dmaap;
123                 }
124         }
125         
126         public Dmaap updateDmaap( Dmaap nd ) {
127                 logger.info( "entering updateDmaap()" );
128                 
129                 boolean anythingWrong = false;
130
131                 Dmaap dmaap = dmaapholder.get();
132                 
133                 // some triggers for when we attempt to reprovision perms and MMA topic:
134                 // - if the DMaaP Name changes
135                 // - if the version is 0  (this is a handy test to force this processing by updating the DB)
136                 // - if the object is invalid, reprocessing might fix it.
137                 if ( ! dmaap.isStatusValid()  || ! nd.getDmaapName().equals(dmaap.getDmaapName()) || dmaap.getVersion().equals( "0") ) {
138                         nd.setLastMod();
139                         dmaapholder.update(nd);  //need to set this so the following perms will pick up any new vals.
140                         dcaeTopicNs = dmaapholder.get().getTopicNsRoot();
141                         ApiPolicy apiPolicy = new ApiPolicy();
142                         if ( apiPolicy.getUseAuthClass()) {
143                                 ApiPerms p = new ApiPerms();
144                                 p.setEnvMap();
145                         }
146                         AafService aaf = new AafService( ServiceType.AAF_Admin);
147                         if ( multiSite ) {
148                                 anythingWrong = setTopicMgtPerms(  nd,  aaf ) || createMmaTopic();
149                         }
150                 }
151                                         
152                 if ( anythingWrong ) {
153                         nd.setStatus(DmaapObject_Status.INVALID); 
154                 }
155                 else {
156                         nd.setStatus(DmaapObject_Status.VALID);  
157                 }
158                 nd.setLastMod();
159                 dmaapholder.update(nd);  // may need to update status...
160                 return(dmaapholder.get());
161                 
162         }
163         
164         public String getTopicPerm(){
165                 Dmaap dmaap = dmaapholder.get();
166                 return getTopicPerm( dmaap.getDmaapName() );
167         }
168         public String getTopicPerm( String val ) {
169                 Dmaap dmaap = dmaapholder.get();
170                 String nsRoot = dmaap.getTopicNsRoot();
171                 if ( nsRoot == null ) { return null; }
172                 
173                 String t;
174                 // in ONAP Casablanca, we assume no distinction of environments reflected in topic namespace
175                 if ( nsRoot.startsWith(noEnvironmentPrefix) ) {
176                         t = nsRoot +  ".mr.topic";
177                 } else {
178                         t = nsRoot + "." + val + ".mr.topic";
179                 }
180                 return t;
181         }
182         
183         public String getBridgeAdminFqtn(){
184                 Dmaap dmaap = dmaapholder.get();
185                 String topic = dmaap.getBridgeAdminTopic();
186                 
187                 // check if this is already an fqtn (contains a dot)
188                 // otherwise build it
189                 if ( topic.indexOf('.') < 0 ) {
190                         topic = dmaap.getTopicNsRoot() + "." + dmaap.getDmaapName() + "." + dmaap.getBridgeAdminTopic();
191                 }
192                 return( topic );
193         }
194
195         private boolean setTopicMgtPerms( Dmaap nd, AafService aaf ){
196                 String[] actions = { "create", "destroy" };
197                 String instance = ":" + dcaeTopicNs + "." + nd.getDmaapName() + ".mr.topic:" + dcaeTopicNs + "." + nd.getDmaapName();
198                 
199                 for( String action : actions ) {
200
201                         DmaapPerm perm = new DmaapPerm( topicFactory, instance, action );
202                 
203                         int rc = aaf.addPerm( perm );
204                         if ( rc != 201 &&  rc != 409 ) {
205                                 logger.error( "unable to add perm for "+ topicFactory + "|" + instance + "|" + action );
206                                 return true;
207                         }
208
209                         DmaapGrant grant = new DmaapGrant( perm, topicMgrRole );
210                         rc = aaf.addGrant( grant );
211                         if ( rc != 201 && rc != 409 ) {
212                                 logger.error( "unable to grant to " + topicMgrRole + " perm for "+ topicFactory + "|" + instance + "|" + action );
213                                 return true;
214                         }
215                 }
216                 
217                 String t = dcaeTopicNs +"." + nd.getDmaapName() + ".mr.topic";
218                 String[] s = { "view", "pub", "sub" };
219                 actions = s;
220                 instance = "*";
221                 
222                 for( String action : actions ) {
223
224                         DmaapPerm perm = new DmaapPerm( t, instance, action );
225                 
226                         int rc = aaf.addPerm( perm );
227                         if ( rc != 201 &&  rc != 409 ) {
228                                 errorLogger.error( DmaapbcLogMessageEnum.AAF_UNEXPECTED_RESPONSE, Integer.toString(rc), "add perm", t + "|" + instance + "|" + action );
229                                 return true;
230                         }
231
232                         DmaapGrant grant = new DmaapGrant( perm, topicMgrRole );
233                         rc = aaf.addGrant( grant );
234                         if ( rc != 201 && rc != 409 ) {
235                                 errorLogger.error( DmaapbcLogMessageEnum.AAF_UNEXPECTED_RESPONSE, Integer.toString(rc), "grant to " + topicMgrRole + " perm ", topicFactory + "|" + instance + "|" + action );
236                                 return true;
237                         }
238                                 
239                 }
240                 return false;
241         }
242
243         public boolean testCreateMmaTopic() {
244
245                 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
246                 String unit_test = p.getProperty( "UnitTest", "No" );
247                 if ( unit_test.equals( "Yes" ) ) {
248                         return createMmaTopic();
249                 }
250                 return false;
251         }
252         
253         // create the special topic for MMA provisioning.
254         // return true indicating a problem in topic creation, 
255         // else false means it was ok  (created or previously existed)
256         private boolean createMmaTopic() {
257                 boolean rc = true;
258                 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
259                 Dmaap dmaap = dmaapholder.get();
260                 
261                 ArrayList<MR_Client> clients = new ArrayList<MR_Client>();
262                 String[] actions = { "pub", "sub", "view" };
263                 String centralMR = new DcaeLocationService().getCentralLocation();
264                 if ( centralMR == null ) {
265                         return rc;
266                 }
267                 logger.info( "Location for " + dmaap.getBridgeAdminTopic() + " is " + centralMR );
268         
269                 // first client is the Role used by Bus Controller to send messages to MMA
270                 String provRole = p.getProperty("MM.ProvRole");
271                 MR_Client nClient = new MR_Client();
272                 nClient.setAction(actions);
273                 nClient.setClientRole(provRole);
274                 nClient.setDcaeLocationName(centralMR);
275                 clients.add( nClient );
276         
277                 // second client is the Role used by MMA to listen to messages from Bus Controller
278                 String agentRole = p.getProperty("MM.AgentRole");
279                 nClient = new MR_Client();
280                 nClient.setAction(actions);
281                 nClient.setClientRole(agentRole);
282                 nClient.setDcaeLocationName(centralMR);
283                 clients.add( nClient );
284         
285                 // initialize Topic
286                 Topic mmaTopic = new Topic().init();
287                 mmaTopic.setTopicName(dmaap.getBridgeAdminTopic());
288                 mmaTopic.setClients(clients);
289                 mmaTopic.setOwner("BusController");
290                 mmaTopic.setTopicDescription("topic reserved for MirrorMaker Administration");
291                 mmaTopic.setTnxEnabled("false");
292                 mmaTopic.setPartitionCount("1");  // a single partition should guarantee message order
293                 
294                 
295                 ApiError err = new ApiError();
296                 TopicService svc = new TopicService();
297                 try {
298                         @SuppressWarnings("unused")
299                         Topic nTopic = svc.addTopic(mmaTopic, err, true);
300                         if ( err.is2xx() || err.getCode() == 409 ) {
301                                 return false;
302                         }
303                 } catch ( Exception e) {
304                         errorLogger.error( DmaapbcLogMessageEnum.UNEXPECTED_CONDITION, " while adding Topic: " + e.getMessage());
305                 }
306                 errorLogger.error( DmaapbcLogMessageEnum.TOPIC_CREATE_ERROR,  dmaap.getBridgeAdminTopic(), err.getFields(), err.getFields(), err.getMessage());
307                 
308                 return rc;
309                 
310         }
311 }