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