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" );
99 apiError.setCode(200);
101 logger.debug( "addDr_Sub resp=" + resp );
105 if ( resp != null ) {
106 snew = new DR_Sub( resp );
107 snew.setDcaeLocationName(sub.getDcaeLocationName());
109 addEgressRoute( snew, apiError );
110 dr_subs.put( snew.getSubId(), snew );
111 apiError.setCode(200);
113 apiError.setCode(400);
119 private void addEgressRoute( DR_Sub sub, ApiError err ) {
121 String nodePattern = nodeService.getNodePatternAtLocation( sub.getDcaeLocationName(), false );
122 if ( nodePattern != null && nodePattern.length() > 0 ) {
123 logger.info( "creating egress rule: sub " + sub.getSubId() + " on feed " + sub.getFeedId() + " to " + nodePattern);
124 prov.makeEgressConnection( sub.getSubId(), nodePattern);
125 int rc = prov.doXgressPost(err);
126 logger.info( "rc=" + rc + " error code=" + err.getCode() );
131 logger.error( "Not authorized for DR egress API");
133 err.setMessage("API deployment/configuration error - contact support");
134 err.setFields( "PROV_AUTH_ADDRESSES");
138 logger.info( DmaapbcLogMessageEnum.EGRESS_CREATE_ERROR, Integer.toString(rc), sub.getSubId(), sub.getFeedId(), nodePattern);
145 public DR_Sub updateDr_Sub( DR_Sub obj, ApiError apiError ) {
146 logger.debug( "enter updateDR_Subs()");
148 DrProvConnection prov = new DrProvConnection();
149 prov.makeSubPutConnection( obj.getSubId() );
150 String resp = prov.doPutDr_Sub( obj, apiError );
151 if ( unit_test.equals( "Yes" ) ) {
152 resp = simulateResp( obj, "PUT" );
153 apiError.setCode(200);
155 logger.debug( "resp=" + resp );
159 if ( resp != null ) {
160 snew = new DR_Sub( resp );
161 snew.setDcaeLocationName(obj.getDcaeLocationName());
163 dr_subs.put( snew.getSubId(), snew );
164 apiError.setCode(200);
165 } else if ( apiError.is2xx()) {
166 apiError.setCode(400);
167 apiError.setMessage("unexpected empty response from DR Prov");
173 public void removeDr_Sub( String key, ApiError apiError ) {
174 logger.debug( "enter removeDR_Subs()");
176 DR_Sub sub = dr_subs.get( key );
178 apiError.setCode(Status.NOT_FOUND.getStatusCode());
179 apiError.setFields( "subId");
180 apiError.setMessage("subId " + key + " not found");
182 DrProvConnection prov = new DrProvConnection();
183 prov.makeSubPutConnection( key );
184 String resp = prov.doDeleteDr_Sub( sub, apiError );
185 logger.debug( "resp=" + resp );
187 if ( apiError.is2xx() || unit_test.equals( "Yes" ) ) {
195 private String simulateResp( DR_Sub sub, String action ){
196 String server = "subscriber.onap.org";
198 if ( action.equals( "POST" ) ) {
199 RandomInteger ran = new RandomInteger(10000);
200 subid = Integer.toString( ran.next() );
201 } else if ( action.equals( "PUT" ) ) {
202 subid = sub.getSubId();
206 String ret = String.format("{\"suspend\": false, \"delivery\": {\"url\": \"https://%s/delivery/%s\", \"user\": \"%s\", \"password\": \"%s\", \"use100\": true}, \"metadataOnly\": false, \"groupid\": \"0\" , \"follow_redirect\": true, ",
207 server, subid, sub.getUsername(), sub.getUserpwd());
208 String links = String.format( "\"links\": {\"feed\": \"https://dr-prov/feedlog/%s\", \"self\": \"https://dr-prov/sub/%s\", \"log\": \"https://dr-prov/sublog/%s\" }",
213 logger.info( "DR_SubService:simulateResp=" + ret);