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