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