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