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.service;
23 import java.util.ArrayList;
24 import java.util.List;
27 import javax.ws.rs.core.Response.Status;
29 import org.onap.dmaap.dbcapi.client.DrProvConnection;
30 import org.onap.dmaap.dbcapi.database.DatabaseClass;
31 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
32 import org.onap.dmaap.dbcapi.logging.DmaapbcLogMessageEnum;
33 import org.onap.dmaap.dbcapi.model.ApiError;
34 import org.onap.dmaap.dbcapi.model.DR_Sub;
35 import org.onap.dmaap.dbcapi.util.DmaapConfig;
36 import org.onap.dmaap.dbcapi.util.RandomInteger;
38 public class DR_SubService extends BaseLoggingClass {
40 private Map<String, DR_Sub> dr_subs = DatabaseClass.getDr_subs();
41 private DR_NodeService nodeService = new DR_NodeService();
42 private String provURL;
43 private static DrProvConnection prov;
45 private String unit_test;
48 public DR_SubService( ) {
49 logger.debug( "Entry: DR_SubService (with no args)" );
50 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
51 unit_test = p.getProperty( "UnitTest", "No" );
53 public DR_SubService( String subURL ) {
54 logger.debug( "Entry: DR_SubService " + subURL );
56 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
57 unit_test = p.getProperty( "UnitTest", "No" );
59 public Map<String, DR_Sub> getDR_Subs() {
60 logger.debug( "enter getDR_Subs()");
64 public List<DR_Sub> getAllDr_Subs() {
65 logger.debug( "enter getAllDR_Subs()");
66 return new ArrayList<DR_Sub>(dr_subs.values());
69 public ArrayList<DR_Sub> getDr_SubsByFeedId( String pubId ) {
70 ArrayList<DR_Sub> someSubs = new ArrayList<DR_Sub>();
71 for( DR_Sub sub : dr_subs.values() ) {
72 if ( pubId.equals( sub.getFeedId() )) {
79 public DR_Sub getDr_Sub( String key, ApiError apiError ) {
80 logger.debug( "enter getDR_Sub()");
81 DR_Sub sub = dr_subs.get( key );
83 apiError.setCode(Status.NOT_FOUND.getStatusCode());
84 apiError.setFields( "subId");
85 apiError.setMessage("subId " + key + " not found");
87 apiError.setCode(200);
92 public DR_Sub addDr_Sub( DR_Sub sub, ApiError apiError ) {
93 logger.debug( "enter addDR_Subs()");
94 prov = new DrProvConnection();
95 prov.makeSubPostConnection( provURL );
96 String resp = prov.doPostDr_Sub( sub, apiError );
97 if ( unit_test.equals( "Yes" ) ) {
98 resp = simulateResp( sub, "POST" );
100 logger.debug( "addDr_Sub resp=" + resp );
104 if ( resp != null ) {
105 snew = new DR_Sub( resp );
106 snew.setDcaeLocationName(sub.getDcaeLocationName());
108 addEgressRoute( snew, apiError );
109 dr_subs.put( snew.getSubId(), snew );
110 apiError.setCode(200);
112 apiError.setCode(400);
118 private void addEgressRoute( DR_Sub sub, ApiError err ) {
120 String nodePattern = nodeService.getNodePatternAtLocation( sub.getDcaeLocationName(), false );
121 if ( nodePattern != null && nodePattern.length() > 0 ) {
122 logger.info( "creating egress rule: sub " + sub.getSubId() + " on feed " + sub.getFeedId() + " to " + nodePattern);
123 prov.makeEgressConnection( sub.getSubId(), nodePattern);
124 int rc = prov.doXgressPost(err);
125 logger.info( "rc=" + rc + " error code=" + err.getCode() );
130 logger.error( "Not authorized for DR egress API");
132 err.setMessage("API deployment/configuration error - contact support");
133 err.setFields( "PROV_AUTH_ADDRESSES");
137 logger.info( DmaapbcLogMessageEnum.EGRESS_CREATE_ERROR, Integer.toString(rc), sub.getSubId(), sub.getFeedId(), nodePattern);
144 public DR_Sub updateDr_Sub( DR_Sub obj, ApiError apiError ) {
145 logger.debug( "enter updateDR_Subs()");
147 DrProvConnection prov = new DrProvConnection();
148 prov.makeSubPutConnection( obj.getSubId() );
149 String resp = prov.doPutDr_Sub( obj, apiError );
150 logger.debug( "resp=" + resp );
154 if ( resp != null ) {
155 snew = new DR_Sub( resp );
156 snew.setDcaeLocationName(obj.getDcaeLocationName());
158 dr_subs.put( snew.getSubId(), snew );
159 apiError.setCode(200);
160 } else if ( apiError.is2xx()) {
161 apiError.setCode(400);
162 apiError.setMessage("unexpected empty response from DR Prov");
168 public void removeDr_Sub( String key, ApiError apiError ) {
169 logger.debug( "enter removeDR_Subs()");
171 DR_Sub sub = dr_subs.get( key );
173 apiError.setCode(Status.NOT_FOUND.getStatusCode());
174 apiError.setFields( "subId");
175 apiError.setMessage("subId " + key + " not found");
177 DrProvConnection prov = new DrProvConnection();
178 prov.makeSubPutConnection( key );
179 String resp = prov.doDeleteDr_Sub( sub, apiError );
180 logger.debug( "resp=" + resp );
182 if ( apiError.is2xx() ) {
190 private String simulateResp( DR_Sub sub, String action ){
191 String server = "subscriber.onap.org";
193 if ( action.equals( "POST" ) ) {
194 RandomInteger ran = new RandomInteger(10000);
195 subid = Integer.toString( ran.next() );
196 } else if ( action.equals( "PUT" ) ) {
197 subid = sub.getSubId();
201 String ret = String.format("{\"delivery\": {\"url\": \"https://%s/delivery/%s\", \"user\": \"joe\", \"password\": \"secret\", \"use100\": true}, \"metadataOnly\": false, \"groupid\": \"0\" , \"follow_redirect\": true }",