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