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