2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.dmaap.dbcapi.aaf.client;
23 import java.io.BufferedReader;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.InputStreamReader;
27 import java.io.OutputStream;
28 import java.net.ConnectException;
29 import java.net.ProtocolException;
30 import java.net.SocketException;
32 import java.util.ArrayList;
34 import javax.net.ssl.HttpsURLConnection;
36 import org.apache.log4j.Logger;
37 import org.apache.log4j.PropertyConfigurator;
38 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
39 import org.onap.dmaap.dbcapi.logging.DmaapbcLogMessageEnum;
40 import org.onap.dmaap.dbcapi.model.ApiError;
41 import org.onap.dmaap.dbcapi.model.DR_Pub;
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.RandomInteger;
49 public class DrProvConnection extends BaseLoggingClass {
52 private String provURL;
54 private HttpsURLConnection uc;
57 public DrProvConnection() {
58 provURL = new DmaapService().getDmaap().getDrProvUrl();
59 if ( provURL.length() < 1 ) {
60 errorLogger.error( DmaapbcLogMessageEnum.PREREQ_DMAAP_OBJECT, "getDrProvUrl");
65 public boolean makeFeedConnection() {
66 return makeConnection( provURL );
68 public boolean makeFeedConnection(String feedId) {
69 return makeConnection( provURL + "/feed/" + feedId );
71 public boolean makeSubPostConnection( String subURL ) {
72 String[] parts = subURL.split("/");
73 String revisedURL = provURL + "/" + parts[3] + "/" + parts[4];
74 logger.info( "mapping " + subURL + " to " + revisedURL );
75 return makeConnection( revisedURL );
77 public boolean makeSubPutConnection( String subId ) {
78 String revisedURL = provURL + "/subs/" + subId;
79 logger.info( "mapping " + subId + " to " + revisedURL );
80 return makeConnection( revisedURL );
83 public boolean makeIngressConnection( String feed, String user, String subnet, String nodep ) {
84 String uri = String.format("/internal/route/ingress/?feed=%s&user=%s&subnet=%s&nodepatt=%s",
85 feed, user, subnet, nodep );
86 return makeConnection( provURL + uri );
88 public boolean makeEgressConnection( String sub, String nodep ) {
89 String uri = String.format("/internal/route/egress/?sub=%s&node=%s",
91 return makeConnection( provURL + uri );
93 public boolean makeNodesConnection( String varName ) {
95 String uri = String.format("/internal/api/%s", varName);
96 return makeConnection( provURL + uri );
99 public boolean makeNodesConnection( String varName, String val ) {
104 String cv = val.replaceAll("\\|", "%7C");
105 String uri = String.format( "/internal/api/%s?val=%s", varName, cv );
107 return makeConnection( provURL + uri );
110 private boolean makeConnection( String pURL ) {
113 URL u = new URL( pURL );
114 uc = (HttpsURLConnection) u.openConnection();
115 uc.setInstanceFollowRedirects(false);
116 logger.info( "successful connect to " + pURL );
118 } catch (Exception e) {
119 errorLogger.error( DmaapbcLogMessageEnum.HTTP_CONNECTION_ERROR, pURL, e.getMessage() );
126 private String bodyToString( InputStream is ) {
127 logger.info( "is=" + is );
128 StringBuilder sb = new StringBuilder();
129 BufferedReader br = new BufferedReader( new InputStreamReader(is));
132 while ((line = br.readLine()) != null ) {
135 } catch (IOException ex ) {
136 errorLogger.error( DmaapbcLogMessageEnum.IO_EXCEPTION, ex.getMessage());
139 return sb.toString();
143 public String doPostFeed( Feed postFeed, ApiError err ) {
145 byte[] postData = postFeed.getBytes();
146 logger.info( "post fields=" + postData.toString() );
147 String responsemessage = null;
148 String responseBody = null;
151 logger.info( "uc=" + uc );
152 uc.setRequestMethod("POST");
153 uc.setRequestProperty("Content-Type", "application/vnd.att-dr.feed");
154 uc.setRequestProperty( "charset", "utf-8");
155 uc.setRequestProperty( "X-ATT-DR-ON-BEHALF-OF", postFeed.getOwner() );
156 uc.setRequestProperty( "Content-Length", Integer.toString( postData.length ));
157 uc.setUseCaches(false);
158 uc.setDoOutput(true);
159 OutputStream os = null;
164 os = uc.getOutputStream();
165 os.write( postData );
167 } catch (ProtocolException pe) {
168 // Rcvd error instead of 100-Continue
170 // work around glitch in Java 1.7.0.21 and likely others
171 // without this, Java will connect multiple times to the server to run the same request
172 uc.setDoOutput(false);
173 } catch (Exception e) {
176 rc = uc.getResponseCode();
177 logger.info( "http response code:" + rc );
178 responsemessage = uc.getResponseMessage();
179 logger.info( "responsemessage=" + responsemessage );
182 if (responsemessage == null) {
183 // work around for glitch in Java 1.7.0.21 and likely others
184 // When Expect: 100 is set and a non-100 response is received, the response message is not set but the response code is
185 String h0 = uc.getHeaderField(0);
187 int i = h0.indexOf(' ');
188 int j = h0.indexOf(' ', i + 1);
189 if (i != -1 && j != -1) {
190 responsemessage = h0.substring(j + 1);
195 responseBody = bodyToString( uc.getInputStream() );
196 logger.info( "responseBody=" + responseBody );
200 err.setMessage(responsemessage);
203 } catch (ConnectException ce) {
204 errorLogger.error(DmaapbcLogMessageEnum.HTTP_CONNECTION_EXCEPTION, provURL, ce.getMessage() );
206 err.setMessage("Backend connection refused");
207 } catch (SocketException se) {
208 errorLogger.error( DmaapbcLogMessageEnum.SOCKET_EXCEPTION, se.getMessage(), "response from prov server" );
210 err.setMessage( "Unable to read response from DR");
211 } catch (Exception e) {
212 logger.warn("Unable to read response " );
215 err.setCode( uc.getResponseCode());
216 err.setMessage(uc.getResponseMessage());
217 } catch (Exception e2) {
219 err.setMessage("Unable to determine response message");
225 } catch ( Exception e ) {}
232 // the POST for /internal/route/ingress doesn't return any data, so needs a different function
233 // the POST for /internal/route/egress doesn't return any data, so needs a different function
234 public int doXgressPost( ApiError err ) {
236 String responsemessage = null;
240 uc.setRequestMethod("POST");
241 // uc.setRequestProperty("Content-Type", "application/vnd.att-dr.feed");
242 // uc.setRequestProperty( "charset", "utf-8");
243 // uc.setRequestProperty( "X-ATT-DR-ON-BEHALF-OF", postFeed.getOwner() );
244 // uc.setRequestProperty( "Content-Length", Integer.toString( postData.length ));
245 // uc.setUseCaches(false);
246 // uc.setDoOutput(true);
247 OutputStream os = null;
252 os = uc.getOutputStream();
255 } catch (ProtocolException pe) {
256 // Rcvd error instead of 100-Continue
258 // work around glitch in Java 1.7.0.21 and likely others
259 // without this, Java will connect multiple times to the server to run the same request
260 uc.setDoOutput(false);
261 } catch (Exception e) {
264 rc = uc.getResponseCode();
265 logger.info( "http response code:" + rc );
266 responsemessage = uc.getResponseMessage();
267 logger.info( "responsemessage=" + responsemessage );
271 if (rc < 200 || rc >= 300 ) {
273 err.setMessage(responsemessage);
275 } catch (Exception e) {
276 System.err.println("Unable to read response " );
281 } catch ( Exception e ) {}
288 public String doPostDr_Sub( DR_Sub postSub, ApiError err ) {
289 logger.info( "entry: doPostDr_Sub() " );
290 byte[] postData = postSub.getBytes();
291 logger.info( "post fields=" + postData );
292 String responsemessage = null;
293 String responseBody = null;
297 uc.setRequestMethod("POST");
299 uc.setRequestProperty("Content-Type", "application/vnd.att-dr.subscription");
300 uc.setRequestProperty( "charset", "utf-8");
301 uc.setRequestProperty( "X-ATT-DR-ON-BEHALF-OF", "DGL" );
302 uc.setRequestProperty( "Content-Length", Integer.toString( postData.length ));
303 uc.setUseCaches(false);
304 uc.setDoOutput(true);
305 OutputStream os = null;
310 os = uc.getOutputStream();
311 os.write( postData );
313 } catch (ProtocolException pe) {
314 // Rcvd error instead of 100-Continue
316 // work around glitch in Java 1.7.0.21 and likely others
317 // without this, Java will connect multiple times to the server to run the same request
318 uc.setDoOutput(false);
319 } catch (Exception e) {
322 rc = uc.getResponseCode();
323 logger.info( "http response code:" + rc );
324 responsemessage = uc.getResponseMessage();
325 logger.info( "responsemessage=" + responsemessage );
328 if (responsemessage == null) {
329 // work around for glitch in Java 1.7.0.21 and likely others
330 // When Expect: 100 is set and a non-100 response is received, the response message is not set but the response code is
331 String h0 = uc.getHeaderField(0);
333 int i = h0.indexOf(' ');
334 int j = h0.indexOf(' ', i + 1);
335 if (i != -1 && j != -1) {
336 responsemessage = h0.substring(j + 1);
341 responseBody = bodyToString( uc.getInputStream() );
342 logger.info( "responseBody=" + responseBody );
346 err.setMessage(responsemessage);
349 } catch (Exception e) {
350 System.err.println("Unable to read response " );
355 } catch ( Exception e ) {}
362 public String doPutFeed(Feed putFeed, ApiError err) {
363 byte[] postData = putFeed.getBytes();
364 logger.info( "post fields=" + postData.toString() );
365 String responsemessage = null;
366 String responseBody = null;
369 logger.info( "uc=" + uc );
370 uc.setRequestMethod("PUT");
371 uc.setRequestProperty("Content-Type", "application/vnd.att-dr.feed");
372 uc.setRequestProperty( "charset", "utf-8");
373 uc.setRequestProperty( "X-ATT-DR-ON-BEHALF-OF", putFeed.getOwner() );
374 uc.setRequestProperty( "Content-Length", Integer.toString( postData.length ));
375 uc.setUseCaches(false);
376 uc.setDoOutput(true);
377 OutputStream os = null;
382 os = uc.getOutputStream();
383 os.write( postData );
385 } catch (ProtocolException pe) {
386 // Rcvd error instead of 100-Continue
388 // work around glitch in Java 1.7.0.21 and likely others
389 // without this, Java will connect multiple times to the server to run the same request
390 uc.setDoOutput(false);
391 } catch (Exception e) {
394 rc = uc.getResponseCode();
395 logger.info( "http response code:" + rc );
396 responsemessage = uc.getResponseMessage();
397 logger.info( "responsemessage=" + responsemessage );
400 if (responsemessage == null) {
401 // work around for glitch in Java 1.7.0.21 and likely others
402 // When Expect: 100 is set and a non-100 response is received, the response message is not set but the response code is
403 String h0 = uc.getHeaderField(0);
405 int i = h0.indexOf(' ');
406 int j = h0.indexOf(' ', i + 1);
407 if (i != -1 && j != -1) {
408 responsemessage = h0.substring(j + 1);
412 if (rc >= 200 && rc < 300 ) {
413 responseBody = bodyToString( uc.getInputStream() );
414 logger.info( "responseBody=" + responseBody );
416 } else if ( rc == 404 ) {
418 err.setFields( "feedid");
419 String message = "FeedId " + putFeed.getFeedId() + " not found on DR to update. Out-of-sync condition?";
420 err.setMessage( message );
421 errorLogger.error( DmaapbcLogMessageEnum.PROV_OUT_OF_SYNC, "Feed", putFeed.getFeedId() );
425 err.setMessage(responsemessage);
428 } catch (ConnectException ce) {
429 errorLogger.error( DmaapbcLogMessageEnum.HTTP_CONNECTION_EXCEPTION, provURL, ce.getMessage() );
431 err.setMessage("Backend connection refused");
432 } catch (SocketException se) {
433 errorLogger.error( DmaapbcLogMessageEnum.SOCKET_EXCEPTION, se.getMessage(), "response from Prov server" );
435 err.setMessage( "Unable to read response from DR");
436 } catch (Exception e) {
437 logger.warn("Unable to read response " );
440 err.setCode( uc.getResponseCode());
441 err.setMessage(uc.getResponseMessage());
442 } catch (Exception e2) {
444 err.setMessage("Unable to determine response message");
449 } catch ( Exception e ) {}
453 public String doPutDr_Sub(DR_Sub postSub, ApiError err) {
454 logger.info( "entry: doPutDr_Sub() " );
455 byte[] postData = postSub.getBytes();
456 logger.info( "post fields=" + postData );
457 String responsemessage = null;
458 String responseBody = null;
462 uc.setRequestMethod("PUT");
464 uc.setRequestProperty("Content-Type", "application/vnd.att-dr.subscription");
465 uc.setRequestProperty( "charset", "utf-8");
466 uc.setRequestProperty( "X-ATT-DR-ON-BEHALF-OF", "DGL" );
467 uc.setRequestProperty( "Content-Length", Integer.toString( postData.length ));
468 uc.setUseCaches(false);
469 uc.setDoOutput(true);
470 OutputStream os = null;
475 os = uc.getOutputStream();
476 os.write( postData );
478 } catch (ProtocolException pe) {
479 // Rcvd error instead of 100-Continue
481 // work around glitch in Java 1.7.0.21 and likely others
482 // without this, Java will connect multiple times to the server to run the same request
483 uc.setDoOutput(false);
484 } catch (Exception e) {
487 rc = uc.getResponseCode();
488 logger.info( "http response code:" + rc );
489 responsemessage = uc.getResponseMessage();
490 logger.info( "responsemessage=" + responsemessage );
493 if (responsemessage == null) {
494 // work around for glitch in Java 1.7.0.21 and likely others
495 // When Expect: 100 is set and a non-100 response is received, the response message is not set but the response code is
496 String h0 = uc.getHeaderField(0);
498 int i = h0.indexOf(' ');
499 int j = h0.indexOf(' ', i + 1);
500 if (i != -1 && j != -1) {
501 responsemessage = h0.substring(j + 1);
506 responseBody = bodyToString( uc.getInputStream() );
507 logger.info( "responseBody=" + responseBody );
511 err.setMessage(responsemessage);
514 } catch (ConnectException ce) {
515 errorLogger.error( DmaapbcLogMessageEnum.HTTP_CONNECTION_EXCEPTION, provURL, ce.getMessage() );
517 err.setMessage("Backend connection refused");
518 } catch (Exception e) {
519 System.err.println("Unable to read response " );
528 public String doGetNodes( ApiError err ) {
529 logger.info( "entry: doGetNodes() " );
530 //byte[] postData = postSub.getBytes();
531 //logger.info( "get fields=" + postData );
532 String responsemessage = null;
533 String responseBody = null;
537 uc.setRequestMethod("GET");
539 //uc.setRequestProperty("Content-Type", "application/vnd.att-dr.subscription");
540 //uc.setRequestProperty( "charset", "utf-8");
541 //uc.setRequestProperty( "X-ATT-DR-ON-BEHALF-OF", "DGL" );
542 //uc.setRequestProperty( "Content-Length", Integer.toString( postData.length ));
543 //uc.setUseCaches(false);
544 //uc.setDoOutput(true);
545 OutputStream os = null;
550 //os = uc.getOutputStream();
551 //os.write( postData );
553 } catch (ProtocolException pe) {
554 // Rcvd error instead of 100-Continue
556 // work around glitch in Java 1.7.0.21 and likely others
557 // without this, Java will connect multiple times to the server to run the same request
558 uc.setDoOutput(false);
559 } catch (Exception e) {
562 rc = uc.getResponseCode();
563 logger.info( "http response code:" + rc );
564 responsemessage = uc.getResponseMessage();
565 logger.info( "responsemessage=" + responsemessage );
568 if (responsemessage == null) {
569 // work around for glitch in Java 1.7.0.21 and likely others
570 // When Expect: 100 is set and a non-100 response is received, the response message is not set but the response code is
571 String h0 = uc.getHeaderField(0);
573 int i = h0.indexOf(' ');
574 int j = h0.indexOf(' ', i + 1);
575 if (i != -1 && j != -1) {
576 responsemessage = h0.substring(j + 1);
580 err.setCode(rc); // may not really be an error, but we save rc
582 responseBody = bodyToString( uc.getInputStream() );
583 logger.info( "responseBody=" + responseBody );
585 err.setMessage(responsemessage);
588 } catch (ConnectException ce) {
589 errorLogger.error( DmaapbcLogMessageEnum.HTTP_CONNECTION_EXCEPTION, provURL, ce.getMessage() );
591 err.setMessage("Backend connection refused");
592 } catch (Exception e) {
593 System.err.println("Unable to read response " );
601 public String doPutNodes( ApiError err ) {
602 logger.info( "entry: doPutNodes() " );
603 //byte[] postData = nodeList.getBytes();
604 //logger.info( "get fields=" + postData );
605 String responsemessage = null;
606 String responseBody = null;
610 uc.setRequestMethod("PUT");
612 //uc.setRequestProperty("Content-Type", "application/vnd.att-dr.subscription");
613 //uc.setRequestProperty( "charset", "utf-8");
614 //uc.setRequestProperty( "X-ATT-DR-ON-BEHALF-OF", "DGL" );
615 //uc.setRequestProperty( "Content-Length", Integer.toString( postData.length ));
616 uc.setUseCaches(false);
617 //uc.setDoOutput(true);
618 OutputStream os = null;
623 //os = uc.getOutputStream();
624 //os.write( postData );
626 } catch (ProtocolException pe) {
627 // Rcvd error instead of 100-Continue
629 // work around glitch in Java 1.7.0.21 and likely others
630 // without this, Java will connect multiple times to the server to run the same request
631 uc.setDoOutput(false);
632 } catch (Exception e) {
635 rc = uc.getResponseCode();
636 logger.info( "http response code:" + rc );
637 responsemessage = uc.getResponseMessage();
638 logger.info( "responsemessage=" + responsemessage );
641 if (responsemessage == null) {
642 // work around for glitch in Java 1.7.0.21 and likely others
643 // When Expect: 100 is set and a non-100 response is received, the response message is not set but the response code is
644 String h0 = uc.getHeaderField(0);
646 int i = h0.indexOf(' ');
647 int j = h0.indexOf(' ', i + 1);
648 if (i != -1 && j != -1) {
649 responsemessage = h0.substring(j + 1);
655 responseBody = bodyToString( uc.getInputStream() );
656 logger.info( "responseBody=" + responseBody );
660 err.setMessage(responsemessage);
663 } catch (Exception e) {
664 System.err.println("Unable to read response " );
673 public String doDeleteFeed(Feed putFeed, ApiError err) {
674 //byte[] postData = putFeed.getBytes();
675 //logger.info( "post fields=" + postData.toString() );
676 String responsemessage = null;
677 String responseBody = null;
680 logger.info( "uc=" + uc );
681 uc.setRequestMethod("DELETE");
682 uc.setRequestProperty("Content-Type", "application/vnd.att-dr.feed");
683 uc.setRequestProperty( "charset", "utf-8");
684 uc.setRequestProperty( "X-ATT-DR-ON-BEHALF-OF", putFeed.getOwner() );
685 //uc.setRequestProperty( "Content-Length", Integer.toString( postData.length ));
686 uc.setUseCaches(false);
687 uc.setDoOutput(true);
688 OutputStream os = null;
693 os = uc.getOutputStream();
694 //os.write( postData );
696 } catch (ProtocolException pe) {
697 // Rcvd error instead of 100-Continue
699 // work around glitch in Java 1.7.0.21 and likely others
700 // without this, Java will connect multiple times to the server to run the same request
701 uc.setDoOutput(false);
702 } catch (Exception e) {
705 rc = uc.getResponseCode();
706 logger.info( "http response code:" + rc );
707 responsemessage = uc.getResponseMessage();
708 logger.info( "responsemessage=" + responsemessage );
711 if (responsemessage == null) {
712 // work around for glitch in Java 1.7.0.21 and likely others
713 // When Expect: 100 is set and a non-100 response is received, the response message is not set but the response code is
714 String h0 = uc.getHeaderField(0);
716 int i = h0.indexOf(' ');
717 int j = h0.indexOf(' ', i + 1);
718 if (i != -1 && j != -1) {
719 responsemessage = h0.substring(j + 1);
723 if (rc >= 200 && rc < 300 ) {
724 responseBody = bodyToString( uc.getInputStream() );
725 logger.info( "responseBody=" + responseBody );
727 } else if ( rc == 404 ) {
729 err.setFields( "feedid");
730 String message = "FeedId " + putFeed.getFeedId() + " not found on DR to update. Out-of-sync condition?";
731 err.setMessage( message );
732 errorLogger.error( DmaapbcLogMessageEnum.PROV_OUT_OF_SYNC, "Feed", putFeed.getFeedId() );
736 err.setMessage(responsemessage);
739 } catch (ConnectException ce) {
740 errorLogger.error( DmaapbcLogMessageEnum.HTTP_CONNECTION_EXCEPTION, provURL, ce.getMessage() );
742 err.setMessage("Backend connection refused");
743 } catch (SocketException se) {
744 errorLogger.error( DmaapbcLogMessageEnum.SOCKET_EXCEPTION, se.getMessage(), "response from Prov server" );
746 err.setMessage( "Unable to read response from DR");
747 } catch (Exception e) {
748 logger.warn("Unable to read response " );
751 err.setCode( uc.getResponseCode());
752 err.setMessage(uc.getResponseMessage());
753 } catch (Exception e2) {
755 err.setMessage("Unable to determine response message");
760 } catch ( Exception e ) {}
766 public static void main( String[] args ) throws Exception {
767 PropertyConfigurator.configure("log4j.properties");
768 logger.info("Started.");
770 RandomInteger ri = new RandomInteger(10000);
771 //String postJSON = String.format("{\"name\": \"dgl feed %d\", \"version\": \"v1.0\", \"description\": \"dgl feed N for testing\", \"authorization\": { \"classification\": \"unclassified\", \"endpoint_addrs\": [],\"endpoint_ids\": [{\"password\": \"test\",\"id\": \"test\"}]}}", ri.next()) ;
773 Feed tst = new Feed( "dgl feed " + i,
775 "dgl feed " + i + "for testing",
779 ArrayList<DR_Pub> pubs = new ArrayList<DR_Pub>();
780 pubs.add( new DR_Pub( "centralLocation" ) );
784 DrProvConnection context = new DrProvConnection();
785 rc = context.makeFeedConnection();
786 logger.info( "makeFeedConnection returns " + rc);
787 ApiError err = new ApiError();
789 String tmp = context.doPostFeed( tst, err );
790 logger.info( "doPostFeed returns " + tmp);