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 URL pUrl = new URL(provUrl);
132 if (pUrl.getProtocol().equals( "https" )) {
133 rc = makeSecureConnection(pUrl);
135 rc = makeDrConnection(pUrl);
137 } catch (MalformedURLException e) {
144 private boolean makeSecureConnection(URL u) {
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());
152 } catch (Exception e) {
153 logger.error("Unexpected error during openConnection of {}", u, e.getMessage());
158 private boolean makeDrConnection(URL u) {
160 uc = (HttpURLConnection) u.openConnection();
161 uc.setInstanceFollowRedirects(false);
162 logger.info( "successful connect to {}", u);
164 } catch(UnknownHostException uhe){
165 logger.error("Caught UnknownHostException for {}", u);
167 } catch (Exception e) {
168 logger.error("Unexpected error during openConnection of {}", u, e.getMessage());
174 public String bodyToString( InputStream is ) {
175 logger.info( "is=" + is );
176 StringBuilder sb = new StringBuilder();
177 BufferedReader br = new BufferedReader( new InputStreamReader(is));
180 while ((line = br.readLine()) != null ) {
183 } catch (IOException ex ) {
184 errorLogger.error( DmaapbcLogMessageEnum.IO_EXCEPTION, ex.getMessage());
187 return sb.toString();
191 public String doPostFeed( Feed postFeed, ApiError err ) {
193 byte[] postData = postFeed.getBytes();
194 logger.info( "post fields=" + Arrays.toString(postData) );
195 String responsemessage = null;
196 String responseBody = null;
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;
212 os = uc.getOutputStream();
213 os.write( postData );
215 } catch (ProtocolException pe) {
216 // Rcvd error instead of 100-Continue
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) {
223 } catch (Exception e) {
224 logger.info( "Exception: " + e.getMessage() );
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);
236 int i = h0.indexOf(' ');
237 int j = h0.indexOf(' ', i + 1);
238 if (i != -1 && j != -1) {
239 responsemessage = h0.substring(j + 1);
244 responseBody = bodyToString( uc.getInputStream() );
245 logger.info( "responseBody=" + responseBody );
249 err.setMessage(responsemessage);
252 } catch (ConnectException ce) {
253 errorLogger.error(DmaapbcLogMessageEnum.HTTP_CONNECTION_EXCEPTION, provURL, ce.getMessage() );
255 err.setMessage("Backend connection refused");
256 } catch (SocketException se) {
257 errorLogger.error( DmaapbcLogMessageEnum.SOCKET_EXCEPTION, se.getMessage(), "response from prov server" );
259 err.setMessage( "Unable to read response from DR");
260 } catch (Exception e) {
261 if ( unit_test.equals( "Yes" ) ) {
263 err.setMessage( "simulated response");
264 logger.info( "artificial 200 response from doPostFeed because unit_test =" + unit_test );
266 logger.warn("Unable to read response " );
267 errorLogger.error("Unable to read response ", e.getMessage());
269 err.setCode( uc.getResponseCode());
270 err.setMessage(uc.getResponseMessage());
271 } catch (Exception e2) {
273 err.setMessage("Unable to determine response message");
280 } catch ( Exception e ) {
281 logger.error(e.getMessage(), e);
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 ) {
293 String responsemessage = null;
297 uc.setRequestMethod("POST");
303 } catch (ProtocolException pe) {
304 // Rcvd error instead of 100-Continue
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);
312 } catch (Exception e) {
313 logger.info( "Exception: " + e.getMessage() );
316 rc = uc.getResponseCode();
317 logger.info( "http response code:" + rc );
318 responsemessage = uc.getResponseMessage();
319 logger.info( "responsemessage=" + responsemessage );
323 if (rc < 200 || rc >= 300 ) {
325 err.setMessage(responsemessage);
327 } catch (Exception e) {
328 if ( unit_test.equals( "Yes" ) ) {
330 err.setMessage( "simulated response");
331 logger.info( "artificial 200 response from doXgressPost because unit_test =" + unit_test );
333 logger.error("Unable to read response " );
334 logger.error(e.getMessage(), e);
340 } catch ( Exception e ) {
341 logger.error(e.getMessage(), e);
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;
358 uc.setRequestMethod("POST");
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;
371 os = uc.getOutputStream();
372 os.write( postData );
374 } catch (ProtocolException pe) {
375 // Rcvd error instead of 100-Continue
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);
383 } catch (Exception e) {
384 logger.info( "Exception: " + e.getMessage() );
387 rc = uc.getResponseCode();
388 logger.info( "http response code:" + rc );
389 responsemessage = uc.getResponseMessage();
390 logger.info( "responsemessage=" + responsemessage );
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);
398 int i = h0.indexOf(' ');
399 int j = h0.indexOf(' ', i + 1);
400 if (i != -1 && j != -1) {
401 responsemessage = h0.substring(j + 1);
406 responseBody = bodyToString( uc.getInputStream() );
407 logger.info( "responseBody=" + responseBody );
411 err.setMessage(responsemessage);
414 } catch (Exception e) {
415 if ( unit_test.equals( "Yes" ) ) {
417 err.setMessage( "simulated response");
418 logger.info( "artificial 200 response from doPostDr_Sub because unit_test =" + unit_test );
420 logger.error("Unable to read response ", e.getMessage());
426 } catch ( Exception e ) {
427 logger.error(e.getMessage(), e);
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;
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;
455 os = uc.getOutputStream();
456 os.write( postData );
458 } catch (ProtocolException pe) {
459 // Rcvd error instead of 100-Continue
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);
467 } catch (Exception e) {
468 logger.info( "Exception: " + e.getMessage() );
471 rc = uc.getResponseCode();
472 logger.info( "http response code:" + rc );
473 responsemessage = uc.getResponseMessage();
474 logger.info( "responsemessage=" + responsemessage );
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);
482 int i = h0.indexOf(' ');
483 int j = h0.indexOf(' ', i + 1);
484 if (i != -1 && j != -1) {
485 responsemessage = h0.substring(j + 1);
489 if (rc >= 200 && rc < 300 ) {
490 responseBody = bodyToString( uc.getInputStream() );
491 logger.info( "responseBody=" + responseBody );
493 } else if ( rc == 404 ) {
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() );
502 err.setMessage(responsemessage);
505 } catch (ConnectException ce) {
506 if ( unit_test.equals( "Yes" ) ) {
508 err.setMessage( "simulated response");
509 logger.info( "artificial 200 response from doPutFeed because unit_test =" + unit_test );
511 errorLogger.error(DmaapbcLogMessageEnum.HTTP_CONNECTION_EXCEPTION, provURL, ce.getMessage());
513 err.setMessage("Backend connection refused");
515 } catch (SocketException se) {
516 errorLogger.error( DmaapbcLogMessageEnum.SOCKET_EXCEPTION, se.getMessage(), "response from Prov server" );
518 err.setMessage( "Unable to read response from DR");
519 } catch (Exception e) {
520 if ( unit_test.equals( "Yes" ) ) {
522 err.setMessage( "simulated response");
523 logger.info( "artificial 200 response from doPutFeed because unit_test =" + unit_test );
525 logger.warn("Unable to read response " );
526 logger.error(e.getMessage(), e);
529 err.setCode( uc.getResponseCode());
530 err.setMessage(uc.getResponseMessage());
531 } catch (Exception e2) {
533 err.setMessage("Unable to determine response message");
534 logger.error(e2.getMessage(), e2);
539 } catch ( Exception e ) {
540 logger.error(e.getMessage(), e);
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;
554 uc.setRequestMethod("PUT");
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;
567 os = uc.getOutputStream();
568 os.write( postData );
570 } catch (ProtocolException pe) {
571 // Rcvd error instead of 100-Continue
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);
579 } catch (Exception e) {
580 logger.info( "Exception: " + e.getMessage() );
583 rc = uc.getResponseCode();
584 logger.info( "http response code:" + rc );
585 responsemessage = uc.getResponseMessage();
586 logger.info( "responsemessage=" + responsemessage );
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);
594 int i = h0.indexOf(' ');
595 int j = h0.indexOf(' ', i + 1);
596 if (i != -1 && j != -1) {
597 responsemessage = h0.substring(j + 1);
602 responseBody = bodyToString( uc.getInputStream() );
603 logger.info( "responseBody=" + responseBody );
607 err.setMessage(responsemessage);
610 } catch (ConnectException ce) {
611 errorLogger.error( DmaapbcLogMessageEnum.HTTP_CONNECTION_EXCEPTION, provURL, ce.getMessage() );
613 err.setMessage("Backend connection refused");
614 logger.error(ce.getMessage(), ce);
615 } catch (Exception e) {
616 if ( unit_test.equals( "Yes" ) ) {
618 err.setMessage( "simulated response");
619 logger.info( "artificial 200 response from doPutDr_Sub because unit_test =" + unit_test );
621 logger.error("Unable to read response " );
622 logger.error(e.getMessage(), e);
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;
642 uc.setRequestMethod("GET");
650 } catch (ProtocolException pe) {
652 // Rcvd error instead of 100-Continue
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);
660 } catch (Exception e) {
661 logger.info( "Exception: " + e.getMessage() );
665 rc = uc.getResponseCode();
666 logger.info( "http response code:" + rc );
667 responsemessage = uc.getResponseMessage();
668 logger.info( "responsemessage=" + responsemessage );
670 if (responsemessage == null) {
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);
676 int i = h0.indexOf(' ');
677 int j = h0.indexOf(' ', i + 1);
678 if (i != -1 && j != -1) {
679 responsemessage = h0.substring(j + 1);
684 err.setCode(rc); // may not really be an error, but we save rc
686 responseBody = bodyToString( uc.getInputStream() );
687 logger.info( "responseBody=" + responseBody );
689 err.setMessage(responsemessage);
693 } catch (ConnectException ce) {
694 if ( unit_test.equals( "Yes" ) ) {
696 err.setMessage( "simulated response");
697 logger.info( "artificial 200 response from doGetNodes because unit_test =" + unit_test );
699 errorLogger.error(DmaapbcLogMessageEnum.HTTP_CONNECTION_EXCEPTION, provURL, ce.getMessage());
701 err.setMessage("Backend connection refused");
702 logger.error(ce.getMessage(), ce);
704 } catch (Exception e) {
705 if ( unit_test.equals( "Yes" ) ) {
707 err.setMessage( "simulated response");
708 logger.info( "artificial 200 response from doGetNodes because unit_test =" + unit_test );
710 logger.error("Unable to read response ", e.getMessage());
714 if ( uc != null ) uc.disconnect();
720 public String doPutNodes( ApiError err ) {
721 logger.info( "entry: doPutNodes() " );
722 String responsemessage = null;
723 String responseBody = null;
726 uc.setRequestMethod("PUT");
727 uc.setUseCaches(false);
732 } catch (ProtocolException pe) {
733 // Rcvd error instead of 100-Continue
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) {
740 } catch (Exception e) {
741 logger.info( "Exception: " + e.getMessage() );
744 rc = uc.getResponseCode();
745 logger.info( "http response code:" + rc );
746 responsemessage = uc.getResponseMessage();
747 logger.info( "responsemessage=" + responsemessage );
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);
755 int i = h0.indexOf(' ');
756 int j = h0.indexOf(' ', i + 1);
757 if (i != -1 && j != -1) {
758 responsemessage = h0.substring(j + 1);
764 responseBody = bodyToString( uc.getInputStream() );
765 logger.info( "responseBody=" + responseBody );
769 err.setMessage(responsemessage);
772 } catch (Exception e) {
773 if ( unit_test.equals( "Yes" ) ) {
775 err.setMessage( "simulated response");
776 logger.info( "artificial 200 response from doPutNodes because unit_test =" + unit_test );
778 logger.error("Unable to read response ", e.getMessage());
789 public String doDeleteFeed(Feed putFeed, ApiError err) {
790 String responsemessage = null;
791 String responseBody = null;
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;
806 os = uc.getOutputStream();
807 //os.write( postData );
809 } catch (ProtocolException pe) {
810 // Rcvd error instead of 100-Continue
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);
818 } catch (Exception e) {
819 logger.info( "Exception: " + e.getMessage() );
822 rc = uc.getResponseCode();
823 logger.info( "http response code:" + rc );
824 responsemessage = uc.getResponseMessage();
825 logger.info( "responsemessage=" + responsemessage );
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);
833 int i = h0.indexOf(' ');
834 int j = h0.indexOf(' ', i + 1);
835 if (i != -1 && j != -1) {
836 responsemessage = h0.substring(j + 1);
840 if (rc >= 200 && rc < 300 ) {
841 responseBody = bodyToString( uc.getInputStream() );
842 logger.info( "responseBody=" + responseBody );
844 } else if ( rc == 404 ) {
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() );
853 err.setMessage(responsemessage);
856 } catch (ConnectException ce) {
857 errorLogger.error( DmaapbcLogMessageEnum.HTTP_CONNECTION_EXCEPTION, provURL, ce.getMessage() );
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" );
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" ) ) {
869 err.setMessage( "simulated response");
870 logger.info( "artificial 200 response from doDeleteFeed because unit_test =" + unit_test );
872 logger.warn("Unable to read response " );
873 logger.error(e.getMessage(), e);
875 err.setCode( uc.getResponseCode());
876 err.setMessage(uc.getResponseMessage());
877 } catch (Exception e2) {
879 err.setMessage("Unable to determine response message");
880 logger.error(e2.getMessage(), e2);
888 } catch ( Exception e ) {
889 logger.error(e.getMessage(), e);
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;
904 uc.setRequestMethod("DELETE");
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;
916 os = uc.getOutputStream();
917 //os.write( postData );
919 } catch (ProtocolException pe) {
920 // Rcvd error instead of 100-Continue
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);
928 } catch (Exception e) {
929 logger.info( "Exception: " + e.getMessage() );
932 rc = uc.getResponseCode();
933 logger.info( "http response code:" + rc );
934 responsemessage = uc.getResponseMessage();
935 logger.info( "responsemessage=" + responsemessage );
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);
943 int i = h0.indexOf(' ');
944 int j = h0.indexOf(' ', i + 1);
945 if (i != -1 && j != -1) {
946 responsemessage = h0.substring(j + 1);
952 responseBody = bodyToString( uc.getInputStream() );
953 logger.info( "responseBody=" + responseBody );
955 err.setMessage(responsemessage);
958 } catch (ConnectException ce) {
959 if ( unit_test.equals( "Yes" ) ) {
961 err.setMessage( "simulated response");
962 logger.info( "artificial 200 response from doDeleteDr_Sub because unit_test =" + unit_test );
964 errorLogger.error( DmaapbcLogMessageEnum.HTTP_CONNECTION_EXCEPTION, provURL, ce.getMessage() );
966 err.setMessage("Backend connection refused");
968 } catch (Exception e) {
969 if ( unit_test.equals( "Yes" ) ) {
971 err.setMessage( "simulated response");
972 logger.info( "artificial 200 response from doDeleteDr_Sub because unit_test =" + unit_test );
974 logger.error("Unable to read response ", e.getMessage());
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 + "\"");
990 private String dq( String k, String v) {
991 return( dq(k) + ":" + dq(v));
993 private String dqc( String k, String v) {
994 return( dq(k) + ":" + dq(v) + ",");
997 private String dumpSimulation() {
998 logger.info( "enter dumpSimulation()");
999 String responseBody =
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")
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")
1025 + dq( "created_date") + ":1553738110000 }"
1027 + dq( "groups") + ":["
1029 + dq( "subscriptions") + ":["
1031 + dq( "ingress") + ":["
1033 + dq( "egress") + ":{"
1035 + dq( "routing") + ":["
1038 return responseBody;
1041 public String doGetDump( ApiError err ) {
1042 logger.info( "entry: doGetDump() " );
1044 String responsemessage = null;
1045 String responseBody = null;
1049 uc.setRequestMethod("GET");
1057 } catch (ProtocolException pe) {
1059 // Rcvd error instead of 100-Continue
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);
1067 } catch (Exception e) {
1068 logger.info( "Exception: " + e.getMessage() );
1069 e.printStackTrace();
1072 rc = uc.getResponseCode();
1073 logger.info( "http response code:" + rc );
1074 responsemessage = uc.getResponseMessage();
1075 logger.info( "responsemessage=" + responsemessage );
1079 if (responsemessage == null) {
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);
1085 int i = h0.indexOf(' ');
1086 int j = h0.indexOf(' ', i + 1);
1087 if (i != -1 && j != -1) {
1088 responsemessage = h0.substring(j + 1);
1093 err.setCode(rc); // may not really be an error, but we save rc
1095 responseBody = bodyToString( uc.getInputStream() );
1096 logger.info( "responseBody=" + responseBody );
1098 err.setMessage(responsemessage);
1102 } catch (ConnectException ce) {
1103 if ( unit_test.equals( "Yes" ) ) {
1105 err.setMessage( "simulated response");
1106 logger.info( "artificial 200 response from doGetNodes because unit_test =" + unit_test );
1107 responseBody = dumpSimulation();
1110 errorLogger.error( DmaapbcLogMessageEnum.HTTP_CONNECTION_EXCEPTION, provURL, ce.getMessage() );
1112 err.setMessage("Backend connection refused");
1113 logger.error(ce.getMessage(), ce);
1115 } catch (Exception e) {
1116 if ( unit_test.equals( "Yes" ) ) {
1118 err.setMessage( "simulated response");
1119 logger.info( "artificial 200 response from doGetNodes because unit_test =" + unit_test );
1120 responseBody = dumpSimulation();
1123 logger.error("Unable to read response ", e.getMessage());
1127 if ( uc != null ) uc.disconnect();
1130 return responseBody;