Actually remove pubs and subs on Feed delete
[dmaap/dbcapi.git] / src / main / java / org / onap / dmaap / dbcapi / service / DR_SubService.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 import java.util.List;
25 import java.util.Map;
26
27 import javax.ws.rs.core.Response.Status;
28
29 import org.onap.dmaap.dbcapi.client.DrProvConnection;
30 import org.onap.dmaap.dbcapi.database.DatabaseClass;
31 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
32 import org.onap.dmaap.dbcapi.logging.DmaapbcLogMessageEnum;
33 import org.onap.dmaap.dbcapi.model.ApiError;
34 import org.onap.dmaap.dbcapi.model.DR_Sub;
35 import org.onap.dmaap.dbcapi.util.DmaapConfig;
36 import org.onap.dmaap.dbcapi.util.RandomInteger;
37
38 public class DR_SubService extends BaseLoggingClass {
39
40         private Map<String, DR_Sub> dr_subs = DatabaseClass.getDr_subs();
41         private DR_NodeService nodeService = new DR_NodeService();
42         private String provURL;
43         private static DrProvConnection prov;
44         
45         private String unit_test;
46         
47         
48         public DR_SubService(  ) {
49                 logger.debug( "Entry: DR_SubService (with no args)" );
50                 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
51                 unit_test = p.getProperty( "UnitTest", "No" );
52         }       
53         public DR_SubService( String subURL ) {
54                 logger.debug( "Entry: DR_SubService " + subURL );
55                 provURL = subURL;
56                 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
57                 unit_test = p.getProperty( "UnitTest", "No" );
58         }
59         public Map<String, DR_Sub> getDR_Subs() {
60                 logger.debug( "enter getDR_Subs()");
61                 return dr_subs;
62         }
63                 
64         public List<DR_Sub> getAllDr_Subs() {
65                 logger.debug( "enter getAllDR_Subs()");
66                 return new ArrayList<DR_Sub>(dr_subs.values());
67         }
68         
69         public ArrayList<DR_Sub> getDr_SubsByFeedId( String pubId ) {
70                 ArrayList<DR_Sub> someSubs = new ArrayList<DR_Sub>();
71                 for( DR_Sub sub : dr_subs.values() ) {
72                         if ( pubId.equals(  sub.getFeedId()  )) {
73                                 someSubs.add( sub );
74                         }
75                 }
76                         
77                 return someSubs;
78         }
79         public DR_Sub getDr_Sub( String key, ApiError apiError ) {      
80                 logger.debug( "enter getDR_Sub()");
81                 DR_Sub sub = dr_subs.get( key );
82                 if ( sub == null ) {
83                         apiError.setCode(Status.NOT_FOUND.getStatusCode());
84                         apiError.setFields( "subId");
85                         apiError.setMessage("subId " + key + " not found");
86                 } else {
87                         apiError.setCode(200);
88                 }
89                 return sub;
90         }
91
92         public DR_Sub addDr_Sub( DR_Sub sub, ApiError apiError ) {
93                 logger.debug( "enter addDR_Subs()");
94                 prov = new DrProvConnection();
95                 prov.makeSubPostConnection( provURL );
96                 String resp = prov.doPostDr_Sub( sub, apiError );
97                 if ( unit_test.equals( "Yes" ) ) {
98                         resp = simulateResp( sub, "POST" );
99                 }
100                 logger.debug( "addDr_Sub resp=" + resp );
101
102                 DR_Sub snew = null;
103
104                 if ( resp != null ) {
105                         snew = new DR_Sub( resp );
106                         snew.setDcaeLocationName(sub.getDcaeLocationName());
107                         snew.setLastMod();
108                         addEgressRoute( snew, apiError );
109                         dr_subs.put( snew.getSubId(), snew );   
110                         apiError.setCode(200);
111                 } else {
112                         apiError.setCode(400);
113                 }
114                 
115                 return snew;
116         }
117
118         private void addEgressRoute( DR_Sub sub, ApiError err ) {
119                 
120                 String nodePattern = nodeService.getNodePatternAtLocation( sub.getDcaeLocationName(), false );
121                 if ( nodePattern != null && nodePattern.length() > 0 ) {
122                         logger.info( "creating egress rule: sub " + sub.getSubId() + " on feed " + sub.getFeedId() + " to " + nodePattern);
123                         prov.makeEgressConnection( sub.getSubId(),  nodePattern);
124                         int rc = prov.doXgressPost(err);
125                         logger.info( "rc=" + rc + " error code=" + err.getCode() );
126                         
127                         if ( rc != 200 ) {
128                                 switch( rc ) {
129                                 case 403:
130                                         logger.error( "Not authorized for DR egress API");
131                                         err.setCode(500);
132                                         err.setMessage("API deployment/configuration error - contact support");
133                                         err.setFields( "PROV_AUTH_ADDRESSES");
134                                         break;
135                                 
136                                 default: 
137                                         logger.info( DmaapbcLogMessageEnum.EGRESS_CREATE_ERROR, Integer.toString(rc),  sub.getSubId(), sub.getFeedId(), nodePattern);
138                                 }
139                         }
140
141                 }
142         }
143         
144         public DR_Sub updateDr_Sub( DR_Sub obj, ApiError apiError ) {
145                 logger.debug( "enter updateDR_Subs()");
146
147                 DrProvConnection prov = new DrProvConnection();
148                 prov.makeSubPutConnection( obj.getSubId() );
149                 String resp = prov.doPutDr_Sub( obj, apiError );
150                 logger.debug( "resp=" + resp );
151
152                 DR_Sub snew = null;
153
154                 if ( resp != null ) {
155                         snew = new DR_Sub( resp );
156                         snew.setDcaeLocationName(obj.getDcaeLocationName());
157                         snew.setLastMod();
158                         dr_subs.put( snew.getSubId(), snew );   
159                         apiError.setCode(200);
160                 } else if ( apiError.is2xx()) {
161                         apiError.setCode(400);
162                         apiError.setMessage("unexpected empty response from DR Prov");
163                 }
164                 
165                 return snew;
166         }
167                 
168         public void removeDr_Sub( String key, ApiError apiError ) {
169                 logger.debug( "enter removeDR_Subs()");
170                 
171                 DR_Sub sub = dr_subs.get( key );
172                 if ( sub == null ) {
173                         apiError.setCode(Status.NOT_FOUND.getStatusCode());
174                         apiError.setFields( "subId");
175                         apiError.setMessage("subId " + key + " not found");
176                 } else {        
177                         DrProvConnection prov = new DrProvConnection();
178                         prov.makeSubPutConnection( key );
179                         String resp = prov.doDeleteDr_Sub( sub, apiError );
180                         logger.debug( "resp=" + resp );
181                         
182                         if ( apiError.is2xx() ) {
183                                 dr_subs.remove(key);
184                         }
185                 }
186
187                 return;
188         }       
189
190         private String simulateResp( DR_Sub sub, String action ){
191                 String server = "subscriber.onap.org";
192                 String subid;
193                 if ( action.equals( "POST" ) ) { 
194                         RandomInteger ran = new RandomInteger(10000);
195                         subid = Integer.toString( ran.next() );
196                 } else if ( action.equals( "PUT" ) ) {
197                         subid = sub.getSubId();
198                 } else {
199                         subid = "99";
200                 }
201                 String ret = String.format("{\"delivery\": {\"url\": \"https://%s/delivery/%s\", \"user\": \"joe\", \"password\": \"secret\", \"use100\":  true}, \"metadataOnly\": false, \"groupid\": \"0\" , \"follow_redirect\": true }", 
202                         server, subid );
203
204                 return ret;
205         }
206 }