Changes for ONAP AAF
[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         private static String noEnvironmentPrefix;
56         
57         
58         String topicFactory; // = "org.openecomp.dcae.dmaap.topicFactory";
59         String topicMgrRole; // = "org.openecomp.dmaapBC.TopicMgr";
60         String dcaeTopicNs; // = "org.openecomp.dcae.dmaap";
61         private boolean multiSite;
62         
63         
64         public DmaapService() {
65                 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
66                 topicFactory = p.getProperty("MR.TopicFactoryNS", "MR.topicFactoryNS.not.set");
67                 topicMgrRole = p.getProperty("MR.TopicMgrRole", "MR.TopicMgrRole.not.set" );
68                 dcaeTopicNs = dmaapholder.get().getTopicNsRoot();
69                 multiSite = "true".equalsIgnoreCase(p.getProperty("MR.multisite", "true"));
70                 noEnvironmentPrefix = p.getProperty( "AAF.NoEnvironmentPrefix", "org.onap");
71                 
72                 logger.info( "DmaapService settings: " + 
73                                 " topicFactory=" + topicFactory +
74                                 " topicMgrRole=" + topicMgrRole +
75                                 " dcaeTopicNs=" + dcaeTopicNs +
76                                 " multisite=" + multiSite +
77                                 " noEnvironmentPrefix=" + noEnvironmentPrefix
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 AafService( ServiceType.AAF_Admin);
97                         ApiPolicy apiPolicy = new ApiPolicy();
98                         if ( apiPolicy.getUseAuthClass() ) {
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                         ApiPolicy apiPolicy = new ApiPolicy();
140                         if ( apiPolicy.getUseAuthClass()) {
141                                 ApiPerms p = new ApiPerms();
142                                 p.setEnvMap();
143                         }
144                         AafService aaf = new AafService( ServiceType.AAF_Admin);
145                         if ( multiSite ) {
146                                 anythingWrong = setTopicMgtPerms(  nd,  aaf ) || createMmaTopic();
147                         }
148                 }
149                                         
150                 if ( anythingWrong ) {
151                         nd.setStatus(DmaapObject_Status.INVALID); 
152                 }
153                 else {
154                         nd.setStatus(DmaapObject_Status.VALID);  
155                 }
156                 nd.setLastMod();
157                 dmaapholder.update(nd);  // may need to update status...
158                 return(dmaapholder.get());
159                 
160         }
161         
162         public String getTopicPerm(){
163                 Dmaap dmaap = dmaapholder.get();
164                 return getTopicPerm( dmaap.getDmaapName() );
165         }
166         public String getTopicPerm( String val ) {
167                 Dmaap dmaap = dmaapholder.get();
168                 String nsRoot = dmaap.getTopicNsRoot();
169                 String t;
170                 // in ONAP Casablanca, we assume no distinction of environments reflected in topic namespace
171                 if ( nsRoot.startsWith(noEnvironmentPrefix) ) {
172                         t = nsRoot +  ".mr.topic";
173                 } else {
174                         t = nsRoot + "." + val + ".mr.topic";
175                 }
176                 return t;
177         }
178         
179         public String getBridgeAdminFqtn(){
180                 Dmaap dmaap = dmaapholder.get();
181                 String topic = dmaap.getBridgeAdminTopic();
182                 
183                 // check if this is already an fqtn (contains a dot)
184                 // otherwise build it
185                 if ( topic.indexOf('.') < 0 ) {
186                         topic = dmaap.getTopicNsRoot() + "." + dmaap.getDmaapName() + "." + dmaap.getBridgeAdminTopic();
187                 }
188                 return( topic );
189         }
190
191         private boolean setTopicMgtPerms( Dmaap nd, AafService aaf ){
192                 String[] actions = { "create", "destroy" };
193                 String instance = ":" + dcaeTopicNs + "." + nd.getDmaapName() + ".mr.topic:" + dcaeTopicNs + "." + nd.getDmaapName();
194                 
195                 for( String action : actions ) {
196
197                         DmaapPerm perm = new DmaapPerm( topicFactory, instance, action );
198                 
199                         int rc = aaf.addPerm( perm );
200                         if ( rc != 201 &&  rc != 409 ) {
201                                 logger.error( "unable to add perm for "+ topicFactory + "|" + instance + "|" + action );
202                                 return true;
203                         }
204
205                         DmaapGrant grant = new DmaapGrant( perm, topicMgrRole );
206                         rc = aaf.addGrant( grant );
207                         if ( rc != 201 && rc != 409 ) {
208                                 logger.error( "unable to grant to " + topicMgrRole + " perm for "+ topicFactory + "|" + instance + "|" + action );
209                                 return true;
210                         }
211                 }
212                 
213                 String t = dcaeTopicNs +"." + nd.getDmaapName() + ".mr.topic";
214                 String[] s = { "view", "pub", "sub" };
215                 actions = s;
216                 instance = "*";
217                 
218                 for( String action : actions ) {
219
220                         DmaapPerm perm = new DmaapPerm( t, instance, action );
221                 
222                         int rc = aaf.addPerm( perm );
223                         if ( rc != 201 &&  rc != 409 ) {
224                                 errorLogger.error( DmaapbcLogMessageEnum.AAF_UNEXPECTED_RESPONSE, Integer.toString(rc), "add perm", t + "|" + instance + "|" + action );
225                                 return true;
226                         }
227
228                         DmaapGrant grant = new DmaapGrant( perm, topicMgrRole );
229                         rc = aaf.addGrant( grant );
230                         if ( rc != 201 && rc != 409 ) {
231                                 errorLogger.error( DmaapbcLogMessageEnum.AAF_UNEXPECTED_RESPONSE, Integer.toString(rc), "grant to " + topicMgrRole + " perm ", topicFactory + "|" + instance + "|" + action );
232                                 return true;
233                         }
234                                 
235                 }
236                 return false;
237         }
238
239         public boolean testCreateMmaTopic() {
240
241                 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
242                 String unit_test = p.getProperty( "UnitTest", "No" );
243                 if ( unit_test.equals( "Yes" ) ) {
244                         return createMmaTopic();
245                 }
246                 return false;
247         }
248         
249         // create the special topic for MMA provisioning.
250         // return true indicating a problem in topic creation, 
251         // else false means it was ok  (created or previously existed)
252         private boolean createMmaTopic() {
253                 boolean rc = true;
254                 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
255                 Dmaap dmaap = dmaapholder.get();
256                 
257                 ArrayList<MR_Client> clients = new ArrayList<MR_Client>();
258                 String[] actions = { "pub", "sub", "view" };
259                 String centralMR = new DcaeLocationService().getCentralLocation();
260                 if ( centralMR == null ) {
261                         return rc;
262                 }
263                 logger.info( "Location for " + dmaap.getBridgeAdminTopic() + " is " + centralMR );
264         
265                 // first client is the Role used by Bus Controller to send messages to MMA
266                 String provRole = p.getProperty("MM.ProvRole");
267                 MR_Client nClient = new MR_Client();
268                 nClient.setAction(actions);
269                 nClient.setClientRole(provRole);
270                 nClient.setDcaeLocationName(centralMR);
271                 clients.add( nClient );
272         
273                 // second client is the Role used by MMA to listen to messages from Bus Controller
274                 String agentRole = p.getProperty("MM.AgentRole");
275                 nClient = new MR_Client();
276                 nClient.setAction(actions);
277                 nClient.setClientRole(agentRole);
278                 nClient.setDcaeLocationName(centralMR);
279                 clients.add( nClient );
280         
281                 // initialize Topic
282                 Topic mmaTopic = new Topic();
283                 mmaTopic.setTopicName(dmaap.getBridgeAdminTopic());
284                 mmaTopic.setClients(clients);
285                 mmaTopic.setOwner("BusController");
286                 mmaTopic.setTopicDescription("topic reserved for MirrorMaker Administration");
287                 mmaTopic.setTnxEnabled("false");
288                 
289                 ApiError err = new ApiError();
290                 TopicService svc = new TopicService();
291                 try {
292                         @SuppressWarnings("unused")
293                         Topic nTopic = svc.addTopic(mmaTopic, err);
294                         if ( err.is2xx() || err.getCode() == 409 ) {
295                                 return false;
296                         }
297                 } catch ( Exception e) {
298                         errorLogger.error( DmaapbcLogMessageEnum.UNEXPECTED_CONDITION, " while adding Topic: " + e.getMessage());
299                 }
300                 errorLogger.error( DmaapbcLogMessageEnum.TOPIC_CREATE_ERROR,  dmaap.getBridgeAdminTopic(), err.getFields(), err.getFields(), err.getMessage());
301                 
302                 return rc;
303                 
304         }
305 }