2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
23 package org.onap.dmaap.dbcapi.client;
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;
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;
49 public class DrProvConnection extends BaseLoggingClass {
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;
60 private HttpURLConnection uc;
62 public DrProvConnection() {
63 provURL = new DmaapService().getDmaap().getDrProvUrl();
64 if ( provURL.length() < 1 ) {
65 errorLogger.error( DmaapbcLogMessageEnum.PREREQ_DMAAP_OBJECT, "DmaapService().getDmaap().getDrProvUrl()");
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" );
78 public boolean makeFeedConnection() {
79 return makeDrConnection( provURL );
81 public boolean makeFeedConnection(String feedId) {
82 return makeDrConnection( provURL + "/feed/" + feedId );
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 );
90 public boolean makeSubPutConnection( String subId ) {
91 String revisedURL = provURL + "/subs/" + subId;
92 logger.info( "mapping " + subId + " to " + revisedURL );
93 return makeDrConnection( revisedURL );
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 );
101 public boolean makeEgressConnection( String sub, String nodep ) {
102 String uri = String.format("/internal/route/egress/?sub=%s&node=%s",
104 return makeDrConnection( provURL + uri );
106 public boolean makeDumpConnection() {
107 String url = provURL + provURI;
108 return makeDrConnection( url );
110 public boolean makeNodesConnection( String varName ) {
112 String uri = String.format("/internal/api/%s", varName);
113 return makeDrConnection( provURL + uri );
116 public boolean makeNodesConnection( String varName, String val ) {
121 String cv = val.replaceAll("\\|", "%7C");
122 String uri = String.format( "/internal/api/%s?val=%s", varName, cv );
124 return makeDrConnection( provURL + uri );
127 public boolean makeDrConnection(String provUrl) {
129 logger.info( "connect to data router at: {}", provUrl);
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);
136 rc = makeDrConnection(pUrl);
138 } catch (MalformedURLException e) {
145 private boolean makeSecureConnection(URL u) {
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());
153 } catch (Exception e) {
154 logger.error("Unexpected error during openConnection of {}", u, e.getMessage());
159 private boolean makeDrConnection(URL u) {
161 uc = (HttpURLConnection) u.openConnection();
162 uc.setInstanceFollowRedirects(false);
163 logger.info( "successful connect to {}", u);
165 } catch(UnknownHostException uhe){
166 logger.error("Caught UnknownHostException for {}", u);
168 } catch (Exception e) {
169 logger.error("Unexpected error during openConnection of {}", u, e.getMessage());
175 public String bodyToString( InputStream is ) {
176 logger.info( "is=" + is );
177 StringBuilder sb = new StringBuilder();
178 BufferedReader br = new BufferedReader( new InputStreamReader(is));
181 while ((line = br.readLine()) != null ) {
184 } catch (IOException ex ) {
185 errorLogger.error( DmaapbcLogMessageEnum.IO_EXCEPTION, ex.getMessage());
188 return sb.toString();
192 public String doPostFeed( Feed postFeed, ApiError err ) {
194 byte[] postData = postFeed.getBytes();
195 logger.info( "post fields=" + Arrays.toString(postData) );
196 String responsemessage = null;
197 String responseBody = null;
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;
213 os = uc.getOutputStream();
214 os.write( postData );
216 } catch (ProtocolException pe) {
217 // Rcvd error instead of 100-Continue
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) {
224 } catch (Exception e) {
225 logger.info( "Exception: " + e.getMessage() );
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);
237 int i = h0.indexOf(' ');
238 int j = h0.indexOf(' ', i + 1);
239 if (i != -1 && j != -1) {
240 responsemessage = h0.substring(j + 1);
245 responseBody = bodyToString( uc.getInputStream() );
246 logger.info( "responseBody=" + responseBody );
250 err.setMessage(responsemessage);
253 } catch (ConnectException ce) {
254 errorLogger.error(DmaapbcLogMessageEnum.HTTP_CONNECTION_EXCEPTION, provURL, ce.getMessage() );
256 err.setMessage("Backend connection refused");
257 } catch (SocketException se) {
258 errorLogger.error( DmaapbcLogMessageEnum.SOCKET_EXCEPTION, se.getMessage(), "response from prov server" );
260 err.setMessage( "Unable to read response from DR");
261 } catch (Exception e) {
262 if ( unit_test.equals( "Yes" ) ) {
264 err.setMessage( "simulated response");
265 logger.info( "artificial 200 response from doPostFeed because unit_test =" + unit_test );
267 logger.warn("Unable to read response " );
268 errorLogger.error("Unable to read response ", e.getMessage());
270 err.setCode( uc.getResponseCode());
271 err.setMessage(uc.getResponseMessage());
272 } catch (Exception e2) {
274 err.setMessage("Unable to determine response message");
281 } catch ( Exception e ) {
282 logger.error(e.getMessage(), e);
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 ) {
294 String responsemessage = null;
298 uc.setRequestMethod("POST");
304 } catch (ProtocolException pe) {
305 // Rcvd error instead of 100-Continue
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);
313 } catch (Exception e) {
314 logger.info( "Exception: " + e.getMessage() );
317 rc = uc.getResponseCode();
318 logger.info( "http response code:" + rc );
319 responsemessage = uc.getResponseMessage();
320 logger.info( "responsemessage=" + responsemessage );
324 if (rc < 200 || rc >= 300 ) {
326 err.setMessage(responsemessage);
328 } catch (Exception e) {
329 if ( unit_test.equals( "Yes" ) ) {
331 err.setMessage( "simulated response");
332 logger.info( "artificial 200 response from doXgressPost because unit_test =" + unit_test );
334 logger.error("Unable to read response " );
335 logger.error(e.getMessage(), e);
341 } catch ( Exception e ) {
342 logger.error(e.getMessage(), e);
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;
359 uc.setRequestMethod("POST");
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;
372 os = uc.getOutputStream();
373 os.write( postData );
375 } catch (ProtocolException pe) {
376 // Rcvd error instead of 100-Continue
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);
384 } catch (Exception e) {
385 logger.info( "Exception: " + e.getMessage() );
388 rc = uc.getResponseCode();
389 logger.info( "http response code:" + rc );
390 responsemessage = uc.getResponseMessage();
391 logger.info( "responsemessage=" + responsemessage );
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);
399 int i = h0.indexOf(' ');
400 int j = h0.indexOf(' ', i + 1);
401 if (i != -1 && j != -1) {
402 responsemessage = h0.substring(j + 1);
407 responseBody = bodyToString( uc.getInputStream() );
408 logger.info( "responseBody=" + responseBody );
412 err.setMessage(responsemessage);
415 } catch (Exception e) {
416 if ( unit_test.equals( "Yes" ) ) {
418 err.setMessage( "simulated response");
419 logger.info( "artificial 200 response from doPostDr_Sub because unit_test =" + unit_test );
421 logger.error("Unable to read response ", e.getMessage());
427 } catch ( Exception e ) {
428 logger.error(e.getMessage(), e);
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;
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;
456 os = uc.getOutputStream();
457 os.write( postData );
459 } catch (ProtocolException pe) {
460 // Rcvd error instead of 100-Continue
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);
468 } catch (Exception e) {
469 logger.info( "Exception: " + e.getMessage() );
472 rc = uc.getResponseCode();
473 logger.info( "http response code:" + rc );
474 responsemessage = uc.getResponseMessage();
475 logger.info( "responsemessage=" + responsemessage );
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);
483 int i = h0.indexOf(' ');
484 int j = h0.indexOf(' ', i + 1);
485 if (i != -1 && j != -1) {
486 responsemessage = h0.substring(j + 1);
490 if (rc >= 200 && rc < 300 ) {
491 responseBody = bodyToString( uc.getInputStream() );
492 logger.info( "responseBody=" + responseBody );
494 } else if ( rc == 404 ) {
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() );
503 err.setMessage(responsemessage);
506 } catch (ConnectException ce) {
507 if ( unit_test.equals( "Yes" ) ) {
509 err.setMessage( "simulated response");
510 logger.info( "artificial 200 response from doPutFeed because unit_test =" + unit_test );
512 errorLogger.error(DmaapbcLogMessageEnum.HTTP_CONNECTION_EXCEPTION, provURL, ce.getMessage());
514 err.setMessage("Backend connection refused");
516 } catch (SocketException se) {
517 errorLogger.error( DmaapbcLogMessageEnum.SOCKET_EXCEPTION, se.getMessage(), "response from Prov server" );
519 err.setMessage( "Unable to read response from DR");
520 } catch (Exception e) {
521 if ( unit_test.equals( "Yes" ) ) {
523 err.setMessage( "simulated response");
524 logger.info( "artificial 200 response from doPutFeed because unit_test =" + unit_test );
526 logger.warn("Unable to read response " );
527 logger.error(e.getMessage(), e);
530 err.setCode( uc.getResponseCode());
531 err.setMessage(uc.getResponseMessage());
532 } catch (Exception e2) {
534 err.setMessage("Unable to determine response message");
535 logger.error(e2.getMessage(), e2);
540 } catch ( Exception e ) {
541 logger.error(e.getMessage(), e);
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;
555 uc.setRequestMethod("PUT");
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;
568 os = uc.getOutputStream();
569 os.write( postData );
571 } catch (ProtocolException pe) {
572 // Rcvd error instead of 100-Continue
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);
580 } catch (Exception e) {
581 logger.info( "Exception: " + e.getMessage() );
584 rc = uc.getResponseCode();
585 logger.info( "http response code:" + rc );
586 responsemessage = uc.getResponseMessage();
587 logger.info( "responsemessage=" + responsemessage );
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);
595 int i = h0.indexOf(' ');
596 int j = h0.indexOf(' ', i + 1);
597 if (i != -1 && j != -1) {
598 responsemessage = h0.substring(j + 1);
603 responseBody = bodyToString( uc.getInputStream() );
604 logger.info( "responseBody=" + responseBody );
608 err.setMessage(responsemessage);
611 } catch (ConnectException ce) {
612 errorLogger.error( DmaapbcLogMessageEnum.HTTP_CONNECTION_EXCEPTION, provURL, ce.getMessage() );
614 err.setMessage("Backend connection refused");
615 logger.error(ce.getMessage(), ce);
616 } catch (Exception e) {
617 if ( unit_test.equals( "Yes" ) ) {
619 err.setMessage( "simulated response");
620 logger.info( "artificial 200 response from doPutDr_Sub because unit_test =" + unit_test );
622 logger.error("Unable to read response " );
623 logger.error(e.getMessage(), e);
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;
643 uc.setRequestMethod("GET");
651 } catch (ProtocolException pe) {
653 // Rcvd error instead of 100-Continue
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);
661 } catch (Exception e) {
662 logger.info( "Exception: " + e.getMessage() );
666 rc = uc.getResponseCode();
667 logger.info( "http response code:" + rc );
668 responsemessage = uc.getResponseMessage();
669 logger.info( "responsemessage=" + responsemessage );
671 if (responsemessage == null) {
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);
677 int i = h0.indexOf(' ');
678 int j = h0.indexOf(' ', i + 1);
679 if (i != -1 && j != -1) {
680 responsemessage = h0.substring(j + 1);
685 err.setCode(rc); // may not really be an error, but we save rc
687 responseBody = bodyToString( uc.getInputStream() );
688 logger.info( "responseBody=" + responseBody );
690 err.setMessage(responsemessage);
694 } catch (ConnectException ce) {
695 if ( unit_test.equals( "Yes" ) ) {
697 err.setMessage( "simulated response");
698 logger.info( "artificial 200 response from doGetNodes because unit_test =" + unit_test );
700 errorLogger.error(DmaapbcLogMessageEnum.HTTP_CONNECTION_EXCEPTION, provURL, ce.getMessage());
702 err.setMessage("Backend connection refused");
703 logger.error(ce.getMessage(), ce);
705 } catch (Exception e) {
706 if ( unit_test.equals( "Yes" ) ) {
708 err.setMessage( "simulated response");
709 logger.info( "artificial 200 response from doGetNodes because unit_test =" + unit_test );
711 logger.error("Unable to read response ", e.getMessage());
715 if ( uc != null ) uc.disconnect();
721 public String doPutNodes( ApiError err ) {
722 logger.info( "entry: doPutNodes() " );
723 String responsemessage = null;
724 String responseBody = null;
727 uc.setRequestMethod("PUT");
728 uc.setUseCaches(false);
733 } catch (ProtocolException pe) {
734 // Rcvd error instead of 100-Continue
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) {
741 } catch (Exception e) {
742 logger.info( "Exception: " + e.getMessage() );
745 rc = uc.getResponseCode();
746 logger.info( "http response code:" + rc );
747 responsemessage = uc.getResponseMessage();
748 logger.info( "responsemessage=" + responsemessage );
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);
756 int i = h0.indexOf(' ');
757 int j = h0.indexOf(' ', i + 1);
758 if (i != -1 && j != -1) {
759 responsemessage = h0.substring(j + 1);
765 responseBody = bodyToString( uc.getInputStream() );
766 logger.info( "responseBody=" + responseBody );
770 err.setMessage(responsemessage);
773 } catch (Exception e) {
774 if ( unit_test.equals( "Yes" ) ) {
776 err.setMessage( "simulated response");
777 logger.info( "artificial 200 response from doPutNodes because unit_test =" + unit_test );
779 logger.error("Unable to read response ", e.getMessage());
790 public String doDeleteFeed(Feed putFeed, ApiError err) {
791 String responsemessage = null;
792 String responseBody = null;
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;
807 os = uc.getOutputStream();
808 //os.write( postData );
810 } catch (ProtocolException pe) {
811 // Rcvd error instead of 100-Continue
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);
819 } catch (Exception e) {
820 logger.info( "Exception: " + e.getMessage() );
823 rc = uc.getResponseCode();
824 logger.info( "http response code:" + rc );
825 responsemessage = uc.getResponseMessage();
826 logger.info( "responsemessage=" + responsemessage );
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);
834 int i = h0.indexOf(' ');
835 int j = h0.indexOf(' ', i + 1);
836 if (i != -1 && j != -1) {
837 responsemessage = h0.substring(j + 1);
841 if (rc >= 200 && rc < 300 ) {
842 responseBody = bodyToString( uc.getInputStream() );
843 logger.info( "responseBody=" + responseBody );
845 } else if ( rc == 404 ) {
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() );
854 err.setMessage(responsemessage);
857 } catch (ConnectException ce) {
858 errorLogger.error( DmaapbcLogMessageEnum.HTTP_CONNECTION_EXCEPTION, provURL, ce.getMessage() );
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" );
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" ) ) {
870 err.setMessage( "simulated response");
871 logger.info( "artificial 200 response from doDeleteFeed because unit_test =" + unit_test );
873 logger.warn("Unable to read response " );
874 logger.error(e.getMessage(), e);
876 err.setCode( uc.getResponseCode());
877 err.setMessage(uc.getResponseMessage());
878 } catch (Exception e2) {
880 err.setMessage("Unable to determine response message");
881 logger.error(e2.getMessage(), e2);
889 } catch ( Exception e ) {
890 logger.error(e.getMessage(), e);
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;
905 uc.setRequestMethod("DELETE");
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;
917 os = uc.getOutputStream();
918 //os.write( postData );
920 } catch (ProtocolException pe) {
921 // Rcvd error instead of 100-Continue
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);
929 } catch (Exception e) {
930 logger.info( "Exception: " + e.getMessage() );
933 rc = uc.getResponseCode();
934 logger.info( "http response code:" + rc );
935 responsemessage = uc.getResponseMessage();
936 logger.info( "responsemessage=" + responsemessage );
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);
944 int i = h0.indexOf(' ');
945 int j = h0.indexOf(' ', i + 1);
946 if (i != -1 && j != -1) {
947 responsemessage = h0.substring(j + 1);
953 responseBody = bodyToString( uc.getInputStream() );
954 logger.info( "responseBody=" + responseBody );
956 err.setMessage(responsemessage);
959 } catch (ConnectException ce) {
960 if ( unit_test.equals( "Yes" ) ) {
962 err.setMessage( "simulated response");
963 logger.info( "artificial 200 response from doDeleteDr_Sub because unit_test =" + unit_test );
965 errorLogger.error( DmaapbcLogMessageEnum.HTTP_CONNECTION_EXCEPTION, provURL, ce.getMessage() );
967 err.setMessage("Backend connection refused");
969 } catch (Exception e) {
970 if ( unit_test.equals( "Yes" ) ) {
972 err.setMessage( "simulated response");
973 logger.info( "artificial 200 response from doDeleteDr_Sub because unit_test =" + unit_test );
975 logger.error("Unable to read response ", e.getMessage());
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 + "\"");
991 private String dq( String k, String v) {
992 return( dq(k) + ":" + dq(v));
994 private String dqc( String k, String v) {
995 return( dq(k) + ":" + dq(v) + ",");
998 private String dumpSimulation() {
999 logger.info( "enter dumpSimulation()");
1000 String responseBody =
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")
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")
1026 + dq( "created_date") + ":1553738110000 }"
1028 + dq( "groups") + ":["
1030 + dq( "subscriptions") + ":["
1032 + dq( "ingress") + ":["
1034 + dq( "egress") + ":{"
1036 + dq( "routing") + ":["
1039 return responseBody;
1042 public String doGetDump( ApiError err ) {
1043 logger.info( "entry: doGetDump() " );
1045 String responsemessage = null;
1046 String responseBody = null;
1050 uc.setRequestMethod("GET");
1058 } catch (ProtocolException pe) {
1060 // Rcvd error instead of 100-Continue
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);
1068 } catch (Exception e) {
1069 logger.info( "Exception: " + e.getMessage() );
1070 e.printStackTrace();
1073 rc = uc.getResponseCode();
1074 logger.info( "http response code:" + rc );
1075 responsemessage = uc.getResponseMessage();
1076 logger.info( "responsemessage=" + responsemessage );
1080 if (responsemessage == null) {
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);
1086 int i = h0.indexOf(' ');
1087 int j = h0.indexOf(' ', i + 1);
1088 if (i != -1 && j != -1) {
1089 responsemessage = h0.substring(j + 1);
1094 err.setCode(rc); // may not really be an error, but we save rc
1096 responseBody = bodyToString( uc.getInputStream() );
1097 logger.info( "responseBody=" + responseBody );
1099 err.setMessage(responsemessage);
1103 } catch (ConnectException ce) {
1104 if ( unit_test.equals( "Yes" ) ) {
1106 err.setMessage( "simulated response");
1107 logger.info( "artificial 200 response from doGetNodes because unit_test =" + unit_test );
1108 responseBody = dumpSimulation();
1111 errorLogger.error( DmaapbcLogMessageEnum.HTTP_CONNECTION_EXCEPTION, provURL, ce.getMessage() );
1113 err.setMessage("Backend connection refused");
1114 logger.error(ce.getMessage(), ce);
1116 } catch (Exception e) {
1117 if ( unit_test.equals( "Yes" ) ) {
1119 err.setMessage( "simulated response");
1120 logger.info( "artificial 200 response from doGetNodes because unit_test =" + unit_test );
1121 responseBody = dumpSimulation();
1124 logger.error("Unable to read response ", e.getMessage());
1128 if ( uc != null ) uc.disconnect();
1131 return responseBody;