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