update link to upper-constraints.txt
[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 jakarta.ws.rs.core.Response.Status;
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.Map;
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_subs.values());
67         }
68         
69         public ArrayList<DR_Sub> getDr_SubsByFeedId( String pubId ) {
70                 ArrayList<DR_Sub> someSubs = new ArrayList<>();
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 ( "Yes".equals(unit_test) ) {
98                         resp = simulateResp( sub, "POST" );
99                         apiError.setCode(201);
100                 }
101                 logger.debug( "addDr_Sub resp=" + resp );
102
103                 DR_Sub snew = null;
104
105                 if ( resp != null ) {
106                         snew = new DR_Sub( resp );
107                         snew.setDcaeLocationName(sub.getDcaeLocationName());
108                         snew.setLastMod();
109                         addEgressRoute( snew, apiError );
110                         dr_subs.put( snew.getSubId(), snew );   
111                         apiError.setCode(201);
112                 } else {
113                         apiError.setCode(400);
114                 }
115                 
116                 return snew;
117         }
118
119         private void addEgressRoute( DR_Sub sub, ApiError err ) {
120                 
121                 String nodePattern = nodeService.getNodePatternAtLocation( sub.getDcaeLocationName(), false );
122                 if ( nodePattern != null && nodePattern.length() > 0 ) {
123                         logger.info( "creating egress rule: sub " + sub.getSubId() + " on feed " + sub.getFeedId() + " to " + nodePattern);
124                         prov.makeEgressConnection( sub.getSubId(),  nodePattern);
125                         int rc = prov.doXgressPost(err);
126                         logger.info( "rc=" + rc + " error code=" + err.getCode() );
127                         
128                         if ( rc != 200 ) {
129                                 switch( rc ) {
130                                 case 403:
131                                         logger.error( "Not authorized for DR egress API");
132                                         err.setCode(500);
133                                         err.setMessage("API deployment/configuration error - contact support");
134                                         err.setFields( "PROV_AUTH_ADDRESSES");
135                                         break;
136                                 
137                                 default: 
138                                         logger.info( DmaapbcLogMessageEnum.EGRESS_CREATE_ERROR, Integer.toString(rc),  sub.getSubId(), sub.getFeedId(), nodePattern);
139                                 }
140                         }
141
142                 }
143         }
144         
145         public DR_Sub updateDr_Sub( DR_Sub obj, ApiError apiError ) {
146                 logger.debug( "enter updateDR_Subs()");
147
148                 DrProvConnection prov = new DrProvConnection();
149                 prov.makeSubPutConnection( obj.getSubId() );
150                 String resp = prov.doPutDr_Sub( obj, apiError );
151                 if ( unit_test.equals( "Yes" ) ) {
152                         resp = simulateResp( obj, "PUT" );
153                         apiError.setCode(200);
154                 }
155                 logger.debug( "resp=" + resp );
156
157                 DR_Sub snew = null;
158
159                 if ( resp != null ) {
160                         snew = new DR_Sub( resp );
161                         snew.setDcaeLocationName(obj.getDcaeLocationName());
162                         snew.setLastMod();
163                         dr_subs.put( snew.getSubId(), snew );   
164                         apiError.setCode(200);
165                 } else if ( apiError.is2xx()) {
166                         apiError.setCode(400);
167                         apiError.setMessage("unexpected empty response from DR Prov");
168                 }
169                 
170                 return snew;
171         }
172                 
173         public void removeDr_Sub( String key, ApiError apiError ) {
174                 removeDr_Sub( key, apiError, true );
175                 return;
176         }
177         
178         public void removeDr_Sub( String key, ApiError apiError, boolean hitDR ) {
179                 logger.debug( "enter removeDR_Subs()");
180                 
181                 DR_Sub sub = dr_subs.get( key );
182                 if ( sub == null ) {
183                         apiError.setCode(Status.NOT_FOUND.getStatusCode());
184                         apiError.setFields( "subId");
185                         apiError.setMessage("subId " + key + " not found");
186                 } else {
187                         if ( hitDR ) {
188                                 DrProvConnection prov = new DrProvConnection();
189                                 prov.makeSubPutConnection( key );
190                                 String resp = prov.doDeleteDr_Sub( sub, apiError );
191                                 logger.debug( "resp=" + resp );
192                         } else {
193                                 apiError.setCode(200);
194                         }
195                         
196                         if ( apiError.is2xx() || unit_test.equals( "Yes" ) ) {
197                                 dr_subs.remove(key);
198                         }
199                 }
200
201                 return;
202         }       
203
204         private String simulateResp( DR_Sub sub, String action ){
205                 String server = "subscriber.onap.org";
206                 String subid;
207                 if ( action.equals( "POST" ) ) { 
208                         RandomInteger ran = new RandomInteger(10000);
209                         subid = Integer.toString( ran.next() );
210                 } else if ( action.equals( "PUT" ) ) {
211                         subid = sub.getSubId();
212                 } else {
213                         subid = "99";
214                 }
215                 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, ", 
216                         server, subid, sub.getUsername(), sub.getUserpwd());
217                 String links = String.format( "\"links\": {\"feed\": \"https://dr-prov/feedlog/%s\", \"self\": \"https://dr-prov/sub/%s\", \"log\": \"https://dr-prov/sublog/%s\" }", 
218                                 sub.getFeedId(),
219                                 subid,
220                                 subid );
221                 ret += links + "}";
222                 logger.info( "DR_SubService:simulateResp=" + ret);
223
224                 return ret;
225         }
226 }