f268eae43dacffd95d925dbdff8144cf4da7acb5
[dmaap/dbcapi.git] / src / main / java / org / onap / dmaap / dbcapi / client / DrProvConnection.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.client;
24
25 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
26 import org.onap.dmaap.dbcapi.logging.DmaapbcLogMessageEnum;
27 import org.onap.dmaap.dbcapi.model.ApiError;
28 import org.onap.dmaap.dbcapi.model.DR_Sub;
29 import org.onap.dmaap.dbcapi.model.Feed;
30 import org.onap.dmaap.dbcapi.service.DmaapService;
31 import org.onap.dmaap.dbcapi.util.DmaapConfig;
32
33 import javax.net.ssl.HttpsURLConnection;
34 import java.io.*;
35 import java.net.ConnectException;
36 import java.net.ProtocolException;
37 import java.net.SocketException;
38 import java.net.URL;
39 import java.util.Arrays;
40
41
42
43 public class DrProvConnection extends BaseLoggingClass {
44            
45    
46         private String provURL;
47         private String provApi;
48         private String  behalfHeader;
49         private String  feedContentType;
50         private String  subContentType;
51         private String unit_test;
52         private String  provURI;
53         
54         private HttpsURLConnection uc;
55
56
57         public DrProvConnection() {
58                 provURL = new DmaapService().getDmaap().getDrProvUrl();
59                 if ( provURL.length() < 1 ) {
60                         errorLogger.error( DmaapbcLogMessageEnum.PREREQ_DMAAP_OBJECT, "DmaapService().getDmaap().getDrProvUrl()");
61                 }
62                 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
63                 provApi = p.getProperty( "DR.provApi", "ONAP" );
64                 behalfHeader = p.getProperty( "DR.onBehalfHeader", "X-DMAAP-DR-ON-BEHALF-OF");
65                 feedContentType = p.getProperty( "DR.feedContentType", "application/vnd.dmaap-dr.feed");
66                 subContentType = p.getProperty( "DR.subContentType", "application/vnd.dmaap-dr.subscription");
67                 provURI = p.getProperty( "DR.ProvisioningURI", "/internal/prov");
68                 logger.info( "provURL=" + provURL + " provApi=" + provApi + " behalfHeader=" + behalfHeader
69                                 + " feedContentType=" + feedContentType + " subContentType=" + subContentType );
70                 unit_test = p.getProperty( "UnitTest", "No" );
71                         
72         }
73         
74         public boolean makeFeedConnection() {
75                 return makeConnection( provURL );
76         }
77         public boolean makeFeedConnection(String feedId) {
78                 return makeConnection( provURL + "/feed/" + feedId );   
79         }
80         public boolean makeSubPostConnection( String subURL ) {
81                 String[] parts = subURL.split("/");
82                 String revisedURL = provURL + "/" + parts[3] + "/" + parts[4];
83                 logger.info( "mapping " + subURL + " to " + revisedURL );
84                 return makeConnection( revisedURL );
85         }
86         public boolean makeSubPutConnection( String subId ) {
87                 String revisedURL = provURL + "/subs/" + subId;
88                 logger.info( "mapping " + subId + " to " + revisedURL );
89                 return makeConnection( revisedURL );
90         }
91
92         public boolean makeIngressConnection( String feed, String user, String subnet, String nodep ) {
93                 String uri = String.format("/internal/route/ingress/?feed=%s&user=%s&subnet=%s&nodepatt=%s", 
94                                         feed, user, subnet, nodep );
95                 return makeConnection( provURL + uri );
96         }
97         public boolean makeEgressConnection( String sub, String nodep ) {
98                 String uri = String.format("/internal/route/egress/?sub=%s&node=%s", 
99                                         sub,  nodep );
100                 return makeConnection( provURL + uri );
101         }
102         public boolean makeDumpConnection() {
103                 String url = provURL + provURI;
104                 return makeConnection( url );
105         }
106         public boolean makeNodesConnection( String varName ) {
107                 
108                 String uri = String.format("/internal/api/%s", varName);
109                 return makeConnection( provURL + uri );
110         }
111         
112         public boolean makeNodesConnection( String varName, String val ) {
113
114                 if ( val == null ) {
115                         return false;
116                 } 
117                 String cv = val.replaceAll("\\|", "%7C");
118                 String uri = String.format( "/internal/api/%s?val=%s", varName, cv );
119
120                 return makeConnection( provURL + uri );
121         }
122         
123         private boolean makeConnection( String pURL ) {
124         
125                 try {
126                         URL u = new URL( pURL );
127                         uc = (HttpsURLConnection) u.openConnection();
128                         uc.setInstanceFollowRedirects(false);
129                         logger.info( "successful connect to " + pURL );
130                         return(true);
131                 } catch (Exception e) {
132                         errorLogger.error( DmaapbcLogMessageEnum.HTTP_CONNECTION_ERROR,  pURL, e.getMessage() );
133             return(false);
134         }
135
136         }
137         
138         public String bodyToString( InputStream is ) {
139                 logger.info( "is=" + is );
140                 StringBuilder sb = new StringBuilder();
141                 BufferedReader br = new BufferedReader( new InputStreamReader(is));
142                 String line;
143                 try {
144                         while ((line = br.readLine()) != null ) {
145                                 sb.append( line );
146                         }
147                 } catch (IOException ex ) {
148                         errorLogger.error( DmaapbcLogMessageEnum.IO_EXCEPTION, ex.getMessage());
149                 }
150                         
151                 return sb.toString();
152         }
153         
154
155         public  String doPostFeed( Feed postFeed, ApiError err ) {
156
157                 byte[] postData = postFeed.getBytes();
158                 logger.info( "post fields=" + Arrays.toString(postData) );
159                 String responsemessage = null;
160                 String responseBody = null;
161
162                 try {
163                         logger.info( "uc=" + uc );
164                         uc.setRequestMethod("POST");
165                         uc.setRequestProperty("Content-Type", feedContentType);
166                         uc.setRequestProperty( "charset", "utf-8");
167                         uc.setRequestProperty( behalfHeader, postFeed.getOwner() );
168                         uc.setRequestProperty( "Content-Length", Integer.toString( postData.length ));
169                         uc.setUseCaches(false);
170                         uc.setDoOutput(true);
171                         OutputStream os = null;
172                         int rc = -1;
173                         
174                         try {
175                  uc.connect();
176                  os = uc.getOutputStream();
177                  os.write( postData );
178
179             } catch (ProtocolException pe) {
180                  // Rcvd error instead of 100-Continue
181                  try {
182                      // work around glitch in Java 1.7.0.21 and likely others
183                      // without this, Java will connect multiple times to the server to run the same request
184                      uc.setDoOutput(false);
185                  } catch (Exception e) {
186                  }
187             }
188                         rc = uc.getResponseCode();
189                         logger.info( "http response code:" + rc );
190             responsemessage = uc.getResponseMessage();
191             logger.info( "responsemessage=" + responsemessage );
192
193
194             if (responsemessage == null) {
195                  // work around for glitch in Java 1.7.0.21 and likely others
196                  // When Expect: 100 is set and a non-100 response is received, the response message is not set but the response code is
197                  String h0 = uc.getHeaderField(0);
198                  if (h0 != null) {
199                      int i = h0.indexOf(' ');
200                      int j = h0.indexOf(' ', i + 1);
201                      if (i != -1 && j != -1) {
202                          responsemessage = h0.substring(j + 1);
203                      }
204                  }
205             }
206             if (rc == 201 ) {
207                         responseBody = bodyToString( uc.getInputStream() );
208                         logger.info( "responseBody=" + responseBody );
209
210             } else {
211                 err.setCode( rc );
212                 err.setMessage(responsemessage);
213             }
214             
215                 } catch (ConnectException ce) {
216                         errorLogger.error(DmaapbcLogMessageEnum.HTTP_CONNECTION_EXCEPTION, provURL, ce.getMessage() );
217             err.setCode( 500 );
218                 err.setMessage("Backend connection refused");
219                 } catch (SocketException se) {
220                         errorLogger.error( DmaapbcLogMessageEnum.SOCKET_EXCEPTION, se.getMessage(), "response from prov server" );
221                         err.setCode( 500 );
222                         err.setMessage( "Unable to read response from DR");
223         } catch (Exception e) {
224                 if ( unit_test.equals( "Yes" ) ) {
225                                 err.setCode(200);
226                                 err.setMessage( "simulated response");
227                                 logger.info( "artificial 200 response from doPostFeed because unit_test =" + unit_test );
228                 } else {
229                     logger.warn("Unable to read response  " );
230                     errorLogger.error("Unable to read response  ", e.getMessage());
231                     try {
232                             err.setCode( uc.getResponseCode());
233                             err.setMessage(uc.getResponseMessage());
234                     } catch (Exception e2) {
235                         err.setCode( 500 );
236                         err.setMessage("Unable to determine response message");
237                     }
238                 }
239         } 
240                 finally {
241                         try {
242                                 uc.disconnect();
243                         } catch ( Exception e ) {
244                                 logger.error(e.getMessage(), e);
245                         }
246                 }
247                 return responseBody;
248
249         }
250
251         
252         // the POST for /internal/route/ingress doesn't return any data, so needs a different function
253         // the POST for /internal/route/egress doesn't return any data, so needs a different function   
254         public int doXgressPost( ApiError err ) {
255                 
256                 String responsemessage = null;
257                 int rc = -1;
258
259                 try {
260                         uc.setRequestMethod("POST");
261
262
263                         try {
264                  uc.connect();
265
266             } catch (ProtocolException pe) {
267                  // Rcvd error instead of 100-Continue
268                  try {
269                      // work around glitch in Java 1.7.0.21 and likely others
270                      // without this, Java will connect multiple times to the server to run the same request
271                      uc.setDoOutput(false);
272                  } catch (Exception e) {
273                         logger.error(e.getMessage(), e);
274                  }
275             }
276                         rc = uc.getResponseCode();
277                         logger.info( "http response code:" + rc );
278             responsemessage = uc.getResponseMessage();
279             logger.info( "responsemessage=" + responsemessage );
280
281
282
283             if (rc < 200 || rc >= 300 ) {
284                 err.setCode( rc );
285                 err.setMessage(responsemessage);
286             }
287                 } catch (Exception e) {
288                 if ( unit_test.equals( "Yes" ) ) {
289                                 err.setCode(200);
290                                 err.setMessage( "simulated response");
291                                 logger.info( "artificial 200 response from doXgressPost because unit_test =" + unit_test );
292                 } else {
293                     logger.error("Unable to read response  " );
294                     logger.error(e.getMessage(), e);
295                 }
296         }               
297         finally {
298                         try {
299                                 uc.disconnect();
300                         } catch ( Exception e ) {
301                                 logger.error(e.getMessage(), e);
302                         }
303                 }
304         
305                 return rc;
306
307         }
308         
309         public String doPostDr_Sub( DR_Sub postSub, ApiError err ) {
310                 logger.info( "entry: doPostDr_Sub() "  );
311                 byte[] postData = postSub.getBytes(provApi );
312                 logger.info( "post fields=" + postData );
313                 String responsemessage = null;
314                 String responseBody = null;
315
316                 try {
317         
318                         uc.setRequestMethod("POST");
319                 
320                         uc.setRequestProperty("Content-Type", subContentType );
321                         uc.setRequestProperty( "charset", "utf-8");
322                         uc.setRequestProperty( behalfHeader, "DGL" );
323                         uc.setRequestProperty( "Content-Length", Integer.toString( postData.length ));
324                         uc.setUseCaches(false);
325                         uc.setDoOutput(true);
326                         OutputStream os = null;
327                         int rc = -1;
328                         
329                         try {
330                  uc.connect();
331                  os = uc.getOutputStream();
332                  os.write( postData );
333
334             } catch (ProtocolException pe) {
335                  // Rcvd error instead of 100-Continue
336                  try {
337                      // work around glitch in Java 1.7.0.21 and likely others
338                      // without this, Java will connect multiple times to the server to run the same request
339                      uc.setDoOutput(false);
340                  } catch (Exception e) {
341                          logger.error(e.getMessage(), e);
342                  }
343             }
344                         rc = uc.getResponseCode();
345                         logger.info( "http response code:" + rc );
346             responsemessage = uc.getResponseMessage();
347             logger.info( "responsemessage=" + responsemessage );
348
349
350             if (responsemessage == null) {
351                  // work around for glitch in Java 1.7.0.21 and likely others
352                  // When Expect: 100 is set and a non-100 response is received, the response message is not set but the response code is
353                  String h0 = uc.getHeaderField(0);
354                  if (h0 != null) {
355                      int i = h0.indexOf(' ');
356                      int j = h0.indexOf(' ', i + 1);
357                      if (i != -1 && j != -1) {
358                          responsemessage = h0.substring(j + 1);
359                      }
360                  }
361             }
362             if (rc == 201 ) {
363                         responseBody = bodyToString( uc.getInputStream() );
364                         logger.info( "responseBody=" + responseBody );
365
366             } else {
367                 err.setCode(rc);
368                 err.setMessage(responsemessage);
369             }
370             
371                 } catch (Exception e) {
372                 if ( unit_test.equals( "Yes" ) ) {
373                                 err.setCode(200);
374                                 err.setMessage( "simulated response");
375                                 logger.info( "artificial 200 response from doPostDr_Sub because unit_test =" + unit_test );
376                 } else {
377                     logger.error("Unable to read response  ", e.getMessage());
378                 }
379         }               
380                 finally {
381                         try {
382                                 uc.disconnect();
383                         } catch ( Exception e ) {
384                                 logger.error(e.getMessage(), e);
385                         }
386                 }
387                 return responseBody;
388
389         }
390         
391
392         public String doPutFeed(Feed putFeed, ApiError err) {
393                 byte[] postData = putFeed.getBytes();
394                 logger.info( "post fields=" + Arrays.toString(postData) );
395                 String responsemessage = null;
396                 String responseBody = null;
397
398                 try {
399                         logger.info( "uc=" + uc );
400                         uc.setRequestMethod("PUT");
401                         uc.setRequestProperty("Content-Type", feedContentType );
402                         uc.setRequestProperty( "charset", "utf-8");
403                         uc.setRequestProperty( behalfHeader, putFeed.getOwner() );
404                         uc.setRequestProperty( "Content-Length", Integer.toString( postData.length ));
405                         uc.setUseCaches(false);
406                         uc.setDoOutput(true);
407                         OutputStream os = null;
408                         int rc = -1;
409                         
410                         try {
411                  uc.connect();
412                  os = uc.getOutputStream();
413                  os.write( postData );
414
415             } catch (ProtocolException pe) {
416                  // Rcvd error instead of 100-Continue
417                  try {
418                      // work around glitch in Java 1.7.0.21 and likely others
419                      // without this, Java will connect multiple times to the server to run the same request
420                      uc.setDoOutput(false);
421                  } catch (Exception e) {
422                          logger.error(e.getMessage(), e);
423                  }
424             }
425                         rc = uc.getResponseCode();
426                         logger.info( "http response code:" + rc );
427             responsemessage = uc.getResponseMessage();
428             logger.info( "responsemessage=" + responsemessage );
429
430
431             if (responsemessage == null) {
432                  // work around for glitch in Java 1.7.0.21 and likely others
433                  // When Expect: 100 is set and a non-100 response is received, the response message is not set but the response code is
434                  String h0 = uc.getHeaderField(0);
435                  if (h0 != null) {
436                      int i = h0.indexOf(' ');
437                      int j = h0.indexOf(' ', i + 1);
438                      if (i != -1 && j != -1) {
439                          responsemessage = h0.substring(j + 1);
440                      }
441                  }
442             }
443             if (rc >= 200 && rc < 300 ) {
444                         responseBody = bodyToString( uc.getInputStream() );
445                         logger.info( "responseBody=" + responseBody );
446                         err.setCode( rc );
447             } else if ( rc == 404 ) {
448                 err.setCode( rc );
449                 err.setFields( "feedid");
450                 String message =  "FeedId " + putFeed.getFeedId() + " not found on DR to update.  Out-of-sync condition?";
451                 err.setMessage( message );
452                 errorLogger.error( DmaapbcLogMessageEnum.PROV_OUT_OF_SYNC, "Feed", putFeed.getFeedId() );
453                 
454             } else {
455                 err.setCode( rc );
456                 err.setMessage(responsemessage);
457             }
458             
459                 } catch (ConnectException ce) {
460                         if ( unit_test.equals( "Yes" ) ) {
461                                 err.setCode(200);
462                                 err.setMessage( "simulated response");
463                                 logger.info( "artificial 200 response from doPutFeed because unit_test =" + unit_test );
464                         } else {
465                                 errorLogger.error(DmaapbcLogMessageEnum.HTTP_CONNECTION_EXCEPTION, provURL, ce.getMessage());
466                                 err.setCode(500);
467                                 err.setMessage("Backend connection refused");
468                         }
469                 } catch (SocketException se) {
470                         errorLogger.error( DmaapbcLogMessageEnum.SOCKET_EXCEPTION, se.getMessage(), "response from Prov server" );
471                         err.setCode( 500 );
472                         err.setMessage( "Unable to read response from DR");
473         } catch (Exception e) {
474                 if ( unit_test.equals( "Yes" ) ) {
475                                 err.setCode(200);
476                                 err.setMessage( "simulated response");
477                                 logger.info( "artificial 200 response from doPutFeed because unit_test =" + unit_test );
478                 } else {
479                     logger.warn("Unable to read response  " );
480                     logger.error(e.getMessage(), e);
481                 }
482             try {
483                     err.setCode( uc.getResponseCode());
484                     err.setMessage(uc.getResponseMessage());
485             } catch (Exception e2) {
486                 err.setCode( 500 );
487                 err.setMessage("Unable to determine response message");
488                 logger.error(e2.getMessage(), e2);
489             }
490         }               finally {
491                         try {
492                                 uc.disconnect();
493                         } catch ( Exception e ) {
494                                 logger.error(e.getMessage(), e);
495                         }
496                 }
497                 return responseBody;
498         }
499         public String doPutDr_Sub(DR_Sub postSub, ApiError err) {
500                 logger.info( "entry: doPutDr_Sub() "  );
501                 byte[] postData = postSub.getBytes(provApi);
502                 logger.info( "post fields=" + postData );
503                 String responsemessage = null;
504                 String responseBody = null;
505
506                 try {
507         
508                         uc.setRequestMethod("PUT");
509                 
510                         uc.setRequestProperty("Content-Type", subContentType );
511                         uc.setRequestProperty( "charset", "utf-8");
512                         uc.setRequestProperty( behalfHeader, "DGL" );
513                         uc.setRequestProperty( "Content-Length", Integer.toString( postData.length ));
514                         uc.setUseCaches(false);
515                         uc.setDoOutput(true);
516                         OutputStream os = null;
517                         int rc = -1;
518                         
519                         try {
520                  uc.connect();
521                  os = uc.getOutputStream();
522                  os.write( postData );
523
524             } catch (ProtocolException pe) {
525                  // Rcvd error instead of 100-Continue
526                  try {
527                      // work around glitch in Java 1.7.0.21 and likely others
528                      // without this, Java will connect multiple times to the server to run the same request
529                      uc.setDoOutput(false);
530                  } catch (Exception e) {
531                          logger.error(e.getMessage(), e);
532                  }
533             }
534                         rc = uc.getResponseCode();
535                         logger.info( "http response code:" + rc );
536             responsemessage = uc.getResponseMessage();
537             logger.info( "responsemessage=" + responsemessage );
538
539
540             if (responsemessage == null) {
541                  // work around for glitch in Java 1.7.0.21 and likely others
542                  // When Expect: 100 is set and a non-100 response is received, the response message is not set but the response code is
543                  String h0 = uc.getHeaderField(0);
544                  if (h0 != null) {
545                      int i = h0.indexOf(' ');
546                      int j = h0.indexOf(' ', i + 1);
547                      if (i != -1 && j != -1) {
548                          responsemessage = h0.substring(j + 1);
549                      }
550                  }
551             }
552             if (rc == 200 ) {
553                         responseBody = bodyToString( uc.getInputStream() );
554                         logger.info( "responseBody=" + responseBody );
555
556             } else {
557                 err.setCode(rc);
558                 err.setMessage(responsemessage);
559             }
560             
561                 } catch (ConnectException ce) {
562             errorLogger.error( DmaapbcLogMessageEnum.HTTP_CONNECTION_EXCEPTION, provURL, ce.getMessage() );
563             err.setCode( 500 );
564                 err.setMessage("Backend connection refused");
565                 logger.error(ce.getMessage(), ce);
566                 } catch (Exception e) {
567                 if ( unit_test.equals( "Yes" ) ) {
568                                 err.setCode(200);
569                                 err.setMessage( "simulated response");
570                                 logger.info( "artificial 200 response from doPutDr_Sub because unit_test =" + unit_test );
571                 } else {
572                     logger.error("Unable to read response  " );
573                     logger.error(e.getMessage(), e);
574                 }
575         } finally {
576                 if(null != uc){
577                     uc.disconnect();
578                 }
579         }
580                 return responseBody;
581
582         }
583         
584         public String doGetNodes( ApiError err ) {
585                 logger.info( "entry: doGetNodes() "  );
586                 //byte[] postData = postSub.getBytes();
587                 //logger.info( "get fields=" + postData );
588                 String responsemessage = null;
589                 String responseBody = null;
590
591                 try {
592         
593                         uc.setRequestMethod("GET");
594                         int rc = -1;
595                         
596
597                         try {
598                 uc.connect();
599         
600
601             } catch (ProtocolException pe) {
602
603                  // Rcvd error instead of 100-Continue
604                  try {
605                      // work around glitch in Java 1.7.0.21 and likely others
606                      // without this, Java will connect multiple times to the server to run the same request
607                      uc.setDoOutput(false);
608                  } catch (Exception e) {
609                          logger.error(e.getMessage(), e);
610                  }
611             } 
612         
613                         rc = uc.getResponseCode();
614                         logger.info( "http response code:" + rc );
615             responsemessage = uc.getResponseMessage();
616             logger.info( "responsemessage=" + responsemessage );
617         
618
619
620             if (responsemessage == null) {
621
622                  // work around for glitch in Java 1.7.0.21 and likely others
623                  // When Expect: 100 is set and a non-100 response is received, the response message is not set but the response code is
624                  String h0 = uc.getHeaderField(0);
625                  if (h0 != null) {
626                      int i = h0.indexOf(' ');
627                      int j = h0.indexOf(' ', i + 1);
628                      if (i != -1 && j != -1) {
629                          responsemessage = h0.substring(j + 1);
630                      }
631                  }
632             }
633         
634                 err.setCode(rc);  // may not really be an error, but we save rc
635             if (rc == 200 ) {
636                         responseBody = bodyToString( uc.getInputStream() );
637                         logger.info( "responseBody=" + responseBody );
638             } else {
639                 err.setMessage(responsemessage);
640             }
641             
642
643                 } catch (ConnectException ce) {
644                         if ( unit_test.equals( "Yes" ) ) {
645                                 err.setCode(200);
646                                 err.setMessage( "simulated response");
647                                 logger.info( "artificial 200 response from doGetNodes because unit_test =" + unit_test );
648                         } else {
649                                 errorLogger.error(DmaapbcLogMessageEnum.HTTP_CONNECTION_EXCEPTION, provURL, ce.getMessage());
650                                 err.setCode(500);
651                                 err.setMessage("Backend connection refused");
652                                 logger.error(ce.getMessage(), ce);
653                         }
654                 } catch (Exception e) {
655                 if ( unit_test.equals( "Yes" ) ) {
656                                 err.setCode(200);
657                                 err.setMessage( "simulated response");
658                                 logger.info( "artificial 200 response from doGetNodes because unit_test =" + unit_test );
659                 } else {
660                     logger.error("Unable to read response  ", e.getMessage());
661                 }
662         } finally {
663
664                         if ( uc != null ) uc.disconnect();
665         }
666
667                 return responseBody;
668
669         }
670         public String doPutNodes( ApiError err ) {
671                 logger.info( "entry: doPutNodes() "  );
672                 //byte[] postData = nodeList.getBytes();
673                 //logger.info( "get fields=" + postData );
674                 String responsemessage = null;
675                 String responseBody = null;
676
677                 try {
678         
679                         uc.setRequestMethod("PUT");
680                 
681                         //uc.setRequestProperty("Content-Type", subContentType );
682                         //uc.setRequestProperty( "charset", "utf-8");
683                         //uc.setRequestProperty( behalfHeader, "DGL" );
684                         //uc.setRequestProperty( "Content-Length", Integer.toString( postData.length ));
685                         uc.setUseCaches(false);
686                         //uc.setDoOutput(true);
687                         OutputStream os = null;
688                         int rc = -1;
689                         
690                         try {
691                  uc.connect();
692                  //os = uc.getOutputStream();
693                  //os.write( postData );
694
695             } catch (ProtocolException pe) {
696                  // Rcvd error instead of 100-Continue
697                  try {
698                      // work around glitch in Java 1.7.0.21 and likely others
699                      // without this, Java will connect multiple times to the server to run the same request
700                      uc.setDoOutput(false);
701                  } catch (Exception e) {
702                  }
703             }
704                         rc = uc.getResponseCode();
705                         logger.info( "http response code:" + rc );
706             responsemessage = uc.getResponseMessage();
707             logger.info( "responsemessage=" + responsemessage );
708
709
710             if (responsemessage == null) {
711                  // work around for glitch in Java 1.7.0.21 and likely others
712                  // When Expect: 100 is set and a non-100 response is received, the response message is not set but the response code is
713                  String h0 = uc.getHeaderField(0);
714                  if (h0 != null) {
715                      int i = h0.indexOf(' ');
716                      int j = h0.indexOf(' ', i + 1);
717                      if (i != -1 && j != -1) {
718                          responsemessage = h0.substring(j + 1);
719                      }
720                  }
721             }
722                 err.setCode(rc);
723             if (rc == 200 ) {
724                         responseBody = bodyToString( uc.getInputStream() );
725                         logger.info( "responseBody=" + responseBody );
726
727             } else {
728   
729                 err.setMessage(responsemessage);
730             }
731             
732                 } catch (Exception e) {
733                 if ( unit_test.equals( "Yes" ) ) {
734                                 err.setCode(200);
735                                 err.setMessage( "simulated response");
736                                 logger.info( "artificial 200 response from doPutNodes because unit_test =" + unit_test );
737                 } else {
738                     logger.error("Unable to read response  ", e.getMessage());
739                 }
740         } finally {
741                         if ( uc != null ) {
742                         uc.disconnect();
743                         }
744         }
745                 return responseBody;
746
747         }
748         
749         public String doDeleteFeed(Feed putFeed, ApiError err) {
750                 //byte[] postData = putFeed.getBytes();
751                 //logger.info( "post fields=" + postData.toString() );
752                 String responsemessage = null;
753                 String responseBody = null;
754
755                 try {
756                         logger.info( "uc=" + uc );
757                         uc.setRequestMethod("DELETE");
758                         uc.setRequestProperty("Content-Type", feedContentType );
759                         uc.setRequestProperty( "charset", "utf-8");
760                         uc.setRequestProperty( behalfHeader, putFeed.getOwner() );
761                         //uc.setRequestProperty( "Content-Length", Integer.toString( postData.length ));
762                         uc.setUseCaches(false);
763                         uc.setDoOutput(true);
764                         OutputStream os = null;
765                         int rc = -1;
766                         
767                         try {
768                  uc.connect();
769                  os = uc.getOutputStream();
770                  //os.write( postData );
771
772             } catch (ProtocolException pe) {
773                  // Rcvd error instead of 100-Continue
774                  try {
775                      // work around glitch in Java 1.7.0.21 and likely others
776                      // without this, Java will connect multiple times to the server to run the same request
777                      uc.setDoOutput(false);
778                  } catch (Exception e) {
779                          logger.error(e.getMessage(), e);
780                  }
781             }
782                         rc = uc.getResponseCode();
783                         logger.info( "http response code:" + rc );
784             responsemessage = uc.getResponseMessage();
785             logger.info( "responsemessage=" + responsemessage );
786
787
788             if (responsemessage == null) {
789                  // work around for glitch in Java 1.7.0.21 and likely others
790                  // When Expect: 100 is set and a non-100 response is received, the response message is not set but the response code is
791                  String h0 = uc.getHeaderField(0);
792                  if (h0 != null) {
793                      int i = h0.indexOf(' ');
794                      int j = h0.indexOf(' ', i + 1);
795                      if (i != -1 && j != -1) {
796                          responsemessage = h0.substring(j + 1);
797                      }
798                  }
799             }
800             if (rc >= 200 && rc < 300 ) {
801                         responseBody = bodyToString( uc.getInputStream() );
802                         logger.info( "responseBody=" + responseBody );
803
804             } else if ( rc == 404 ) {
805                 err.setCode( rc );
806                 err.setFields( "feedid");
807                 String message =  "FeedId " + putFeed.getFeedId() + " not found on DR to update.  Out-of-sync condition?";
808                 err.setMessage( message );
809                 errorLogger.error( DmaapbcLogMessageEnum.PROV_OUT_OF_SYNC, "Feed", putFeed.getFeedId() );
810                 
811             } else {
812                 err.setCode( rc );
813                 err.setMessage(responsemessage);
814             }
815             
816                 } catch (ConnectException ce) {
817                         errorLogger.error( DmaapbcLogMessageEnum.HTTP_CONNECTION_EXCEPTION, provURL, ce.getMessage() );
818             err.setCode( 500 );
819                 err.setMessage("Backend connection refused");
820                 logger.error(ce.getMessage(), ce);
821                 } catch (SocketException se) {
822                         errorLogger.error( DmaapbcLogMessageEnum.SOCKET_EXCEPTION, se.getMessage(), "response from Prov server" );
823                         err.setCode( 500 );
824                         err.setMessage( "Unable to read response from DR");
825                         logger.error(se.getMessage(), se);
826         } catch (Exception e) {
827                 if ( unit_test.equals( "Yes" ) ) {
828                                 err.setCode(200);
829                                 err.setMessage( "simulated response");
830                                 logger.info( "artificial 200 response from doDeleteFeed because unit_test =" + unit_test );
831                 } else {
832                     logger.warn("Unable to read response  " );
833                     logger.error(e.getMessage(), e);
834                     try {
835                             err.setCode( uc.getResponseCode());
836                             err.setMessage(uc.getResponseMessage());
837                     } catch (Exception e2) {
838                         err.setCode( 500 );
839                         err.setMessage("Unable to determine response message");
840                         logger.error(e2.getMessage(), e2);
841                     }
842                 }
843         }               finally {
844                         try {
845                                 if(uc != null) {
846                                     uc.disconnect();
847                                 }
848                         } catch ( Exception e ) {
849                                 logger.error(e.getMessage(), e);
850                         }
851                 }
852                 return responseBody;
853         }
854         
855         public String doDeleteDr_Sub(DR_Sub delSub, ApiError err) {
856                 logger.info( "entry: doDeleteDr_Sub() "  );
857                 byte[] postData = delSub.getBytes(provApi);
858                 logger.info( "post fields=" + postData );
859                 String responsemessage = null;
860                 String responseBody = null;
861
862                 try {
863         
864                         uc.setRequestMethod("DELETE");
865                 
866                         uc.setRequestProperty("Content-Type", subContentType);
867                         uc.setRequestProperty( "charset", "utf-8");
868                         uc.setRequestProperty( behalfHeader, "DGL" );
869                         //uc.setRequestProperty( "Content-Length", Integer.toString( postData.length ));
870                         uc.setUseCaches(false);
871                         uc.setDoOutput(true);
872                         OutputStream os = null;
873                         int rc = -1;
874                         
875                         try {
876                  uc.connect();
877                  os = uc.getOutputStream();
878                  //os.write( postData );
879
880             } catch (ProtocolException pe) {
881                  // Rcvd error instead of 100-Continue
882                  try {
883                      // work around glitch in Java 1.7.0.21 and likely others
884                      // without this, Java will connect multiple times to the server to run the same request
885                      uc.setDoOutput(false);
886                  } catch (Exception e) {
887                          logger.error(e.getMessage(), e);
888                  }
889             }
890                         rc = uc.getResponseCode();
891                         logger.info( "http response code:" + rc );
892             responsemessage = uc.getResponseMessage();
893             logger.info( "responsemessage=" + responsemessage );
894
895
896             if (responsemessage == null) {
897                  // work around for glitch in Java 1.7.0.21 and likely others
898                  // When Expect: 100 is set and a non-100 response is received, the response message is not set but the response code is
899                  String h0 = uc.getHeaderField(0);
900                  if (h0 != null) {
901                      int i = h0.indexOf(' ');
902                      int j = h0.indexOf(' ', i + 1);
903                      if (i != -1 && j != -1) {
904                          responsemessage = h0.substring(j + 1);
905                      }
906                  }
907             }
908                 err.setCode(rc);
909             if (rc == 204 ) {
910                         responseBody = bodyToString( uc.getInputStream() );
911                         logger.info( "responseBody=" + responseBody );
912             } else {
913                 err.setMessage(responsemessage);
914             }
915             
916                 } catch (ConnectException ce) {
917                 if ( unit_test.equals( "Yes" ) ) {
918                                 err.setCode(200);
919                                 err.setMessage( "simulated response");
920                                 logger.info( "artificial 200 response from doDeleteDr_Sub because unit_test =" + unit_test );
921                 } else {
922                     errorLogger.error( DmaapbcLogMessageEnum.HTTP_CONNECTION_EXCEPTION, provURL, ce.getMessage() );
923                     err.setCode( 500 );
924                         err.setMessage("Backend connection refused");
925                 }
926                 } catch (Exception e) {
927                 if ( unit_test.equals( "Yes" ) ) {
928                                 err.setCode(200);
929                                 err.setMessage( "simulated response");
930                                 logger.info( "artificial 200 response from doDeleteDr_Sub because unit_test =" + unit_test );
931                 } else {
932                     logger.error("Unable to read response  ", e.getMessage());
933                 }
934         } finally {
935                 if(uc != null){
936                     uc.disconnect();
937                 }
938         }
939                 return responseBody;
940
941         }
942         
943         // add double-quotes around a value
944         // hope his is easier to read than in-line escaping...
945         private String dq( String v ) {
946                 return ( "\"" + v + "\"");
947         }
948         private String dq( String k, String v) {
949                 return( dq(k) + ":" + dq(v));
950         }
951         private String dqc( String k, String v) {
952                 return( dq(k) + ":" + dq(v) + ",");
953         }
954         
955         private String dumpSimulation() {
956                 logger.info( "enter dumpSimulation()");
957                 String                                  responseBody = 
958                                 "{"
959                                 + dq("feeds") + ":["
960                                 + "{" + dq( "suspend") + ":false,"
961                                           + dq( "groupid") + ":0,"
962                                           + dqc( "description", "Some description" )
963                                           + dqc( "version", "m1.1") 
964                                           + dq( "authorization") + ":"
965                                           + "{" + dq( "endpoint_addrs" ) + ":[],"
966                                                         + dq( "classification", "unclassified")
967                                                         + dq( "endpoint_ids") + ":[{"
968                                                                 + dqc( "password", "dradmin" )
969                                                                 + dq( "id", "dradmin")
970                                                                 + "}]}"
971                                                 + dq( "last_mod") + ":1553738110000,"
972                                                 + dq( "deleted") + ":false,"
973                                                 + dq( "feedid") + ":1,"
974                                                 + dqc( "name", "Default PM Feed")
975                                                 + dq( "business_description") + ":\"\","
976                                                 + dqc( "publisher", "onap")
977                                                 + dq( "links") + ":{"
978                                                         + dqc( "subscribe", "https://dmaap-dr-prov/subscribe/1")
979                                                         + dqc( "log", "https://dmaap-dr-prov/feedlog/1")
980                                                         + dqc( "publish", "https://dmaap-dr-prov/publish/1")
981                                                         + dq( "self", "https:/dmaap-dr-prov/feed/1")
982                                                         + "}"
983                                                 + dq( "created_date") + ":1553738110000 }"
984                                 + "],"
985                                 + dq( "groups") + ":["
986                                 + "],"
987                                 + dq( "subscriptions") + ":["
988                                 + "],"
989                                 + dq( "ingress") + ":["
990                                 + "],"
991                                 + dq( "egress") + ":{"
992                                 + "},"
993                                 + dq( "routing") + ":["
994                                 + "],"
995                           + "}";
996                 return responseBody;
997         }
998         
999         public String doGetDump( ApiError err ) {
1000                 logger.info( "entry: doGetDump() "  );
1001
1002                 String responsemessage = null;
1003                 String responseBody = null;
1004
1005                 try {
1006         
1007                         uc.setRequestMethod("GET");
1008                         int rc = -1;
1009                         
1010
1011                         try {
1012                 uc.connect();
1013         
1014
1015             } catch (ProtocolException pe) {
1016
1017                  // Rcvd error instead of 100-Continue
1018                  try {
1019                      // work around glitch in Java 1.7.0.21 and likely others
1020                      // without this, Java will connect multiple times to the server to run the same request
1021                      uc.setDoOutput(false);
1022                  } catch (Exception e) {
1023                          logger.error(e.getMessage(), e);
1024                  }
1025             } 
1026         
1027                         rc = uc.getResponseCode();
1028                         logger.info( "http response code:" + rc );
1029             responsemessage = uc.getResponseMessage();
1030             logger.info( "responsemessage=" + responsemessage );
1031         
1032
1033
1034             if (responsemessage == null) {
1035
1036                  // work around for glitch in Java 1.7.0.21 and likely others
1037                  // When Expect: 100 is set and a non-100 response is received, the response message is not set but the response code is
1038                  String h0 = uc.getHeaderField(0);
1039                  if (h0 != null) {
1040                      int i = h0.indexOf(' ');
1041                      int j = h0.indexOf(' ', i + 1);
1042                      if (i != -1 && j != -1) {
1043                          responsemessage = h0.substring(j + 1);
1044                      }
1045                  }
1046             }
1047         
1048                 err.setCode(rc);  // may not really be an error, but we save rc
1049             if (rc == 200 ) {
1050                         responseBody = bodyToString( uc.getInputStream() );
1051                         logger.info( "responseBody=" + responseBody );
1052             } else {
1053                 err.setMessage(responsemessage);
1054             }
1055             
1056
1057                 } catch (ConnectException ce) {
1058                 if ( unit_test.equals( "Yes" ) ) {
1059                                 err.setCode(200);
1060                                 err.setMessage( "simulated response");
1061                                 logger.info( "artificial 200 response from doGetNodes because unit_test =" + unit_test );
1062                                 responseBody = dumpSimulation();
1063                                                           
1064                 } else {
1065                     errorLogger.error( DmaapbcLogMessageEnum.HTTP_CONNECTION_EXCEPTION, provURL, ce.getMessage() );
1066                     err.setCode( 500 );
1067                         err.setMessage("Backend connection refused");
1068                         logger.error(ce.getMessage(), ce);
1069                 }
1070                 } catch (Exception e) {
1071                 if ( unit_test.equals( "Yes" ) ) {
1072                                 err.setCode(200);
1073                                 err.setMessage( "simulated response");
1074                                 logger.info( "artificial 200 response from doGetNodes because unit_test =" + unit_test );
1075                                 responseBody = dumpSimulation();
1076                                                           
1077                 } else {
1078                     logger.error("Unable to read response  ", e.getMessage());
1079                 }
1080         } finally {
1081
1082                         if ( uc != null ) uc.disconnect();
1083         }
1084
1085                 return responseBody;
1086
1087         }
1088                 
1089 }