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