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.service;
25 import java.util.ArrayList;
26 import java.util.List;
29 import javax.ws.rs.core.Response.Status;
31 import org.onap.dmaap.dbcapi.client.DrProvConnection;
32 import org.onap.dmaap.dbcapi.database.DatabaseClass;
33 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
34 import org.onap.dmaap.dbcapi.logging.DmaapbcLogMessageEnum;
35 import org.onap.dmaap.dbcapi.model.ApiError;
36 import org.onap.dmaap.dbcapi.model.DR_Sub;
37 import org.onap.dmaap.dbcapi.util.DmaapConfig;
38 import org.onap.dmaap.dbcapi.util.RandomInteger;
40 public class DR_SubService extends BaseLoggingClass {
42 private Map<String, DR_Sub> dr_subs = DatabaseClass.getDr_subs();
43 private DR_NodeService nodeService = new DR_NodeService();
44 private String provURL;
45 private static DrProvConnection prov;
47 private String unit_test;
50 public DR_SubService( ) {
51 logger.debug( "Entry: DR_SubService (with no args)" );
52 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
53 unit_test = p.getProperty( "UnitTest", "No" );
55 public DR_SubService( String subURL ) {
56 logger.debug( "Entry: DR_SubService " + subURL );
58 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
59 unit_test = p.getProperty( "UnitTest", "No" );
61 public Map<String, DR_Sub> getDR_Subs() {
62 logger.debug( "enter getDR_Subs()");
66 public List<DR_Sub> getAllDr_Subs() {
67 logger.debug( "enter getAllDR_Subs()");
68 return new ArrayList<>(dr_subs.values());
71 public ArrayList<DR_Sub> getDr_SubsByFeedId( String pubId ) {
72 ArrayList<DR_Sub> someSubs = new ArrayList<>();
73 for( DR_Sub sub : dr_subs.values() ) {
74 if ( pubId.equals( sub.getFeedId() )) {
81 public DR_Sub getDr_Sub( String key, ApiError apiError ) {
82 logger.debug( "enter getDR_Sub()");
83 DR_Sub sub = dr_subs.get( key );
85 apiError.setCode(Status.NOT_FOUND.getStatusCode());
86 apiError.setFields( "subId");
87 apiError.setMessage("subId " + key + " not found");
89 apiError.setCode(200);
94 public DR_Sub addDr_Sub( DR_Sub sub, ApiError apiError ) {
95 logger.debug( "enter addDR_Subs()");
96 prov = new DrProvConnection();
97 prov.makeSubPostConnection( provURL );
98 String resp = prov.doPostDr_Sub( sub, apiError );
99 if ( "Yes".equals(unit_test) ) {
100 resp = simulateResp( sub, "POST" );
101 apiError.setCode(201);
103 logger.debug( "addDr_Sub resp=" + resp );
107 if ( resp != null ) {
108 snew = new DR_Sub( resp );
109 snew.setDcaeLocationName(sub.getDcaeLocationName());
111 addEgressRoute( snew, apiError );
112 dr_subs.put( snew.getSubId(), snew );
113 apiError.setCode(201);
115 apiError.setCode(400);
121 private void addEgressRoute( DR_Sub sub, ApiError err ) {
123 String nodePattern = nodeService.getNodePatternAtLocation( sub.getDcaeLocationName(), false );
124 if ( nodePattern != null && nodePattern.length() > 0 ) {
125 logger.info( "creating egress rule: sub " + sub.getSubId() + " on feed " + sub.getFeedId() + " to " + nodePattern);
126 prov.makeEgressConnection( sub.getSubId(), nodePattern);
127 int rc = prov.doXgressPost(err);
128 logger.info( "rc=" + rc + " error code=" + err.getCode() );
133 logger.error( "Not authorized for DR egress API");
135 err.setMessage("API deployment/configuration error - contact support");
136 err.setFields( "PROV_AUTH_ADDRESSES");
140 logger.info( DmaapbcLogMessageEnum.EGRESS_CREATE_ERROR, Integer.toString(rc), sub.getSubId(), sub.getFeedId(), nodePattern);
147 public DR_Sub updateDr_Sub( DR_Sub obj, ApiError apiError ) {
148 logger.debug( "enter updateDR_Subs()");
150 DrProvConnection prov = new DrProvConnection();
151 prov.makeSubPutConnection( obj.getSubId() );
152 String resp = prov.doPutDr_Sub( obj, apiError );
153 if ( unit_test.equals( "Yes" ) ) {
154 resp = simulateResp( obj, "PUT" );
155 apiError.setCode(200);
157 logger.debug( "resp=" + resp );
161 if ( resp != null ) {
162 snew = new DR_Sub( resp );
163 snew.setDcaeLocationName(obj.getDcaeLocationName());
165 dr_subs.put( snew.getSubId(), snew );
166 apiError.setCode(200);
167 } else if ( apiError.is2xx()) {
168 apiError.setCode(400);
169 apiError.setMessage("unexpected empty response from DR Prov");
175 public void removeDr_Sub( String key, ApiError apiError ) {
176 removeDr_Sub( key, apiError, true );
180 public void removeDr_Sub( String key, ApiError apiError, boolean hitDR ) {
181 logger.debug( "enter removeDR_Subs()");
183 DR_Sub sub = dr_subs.get( key );
185 apiError.setCode(Status.NOT_FOUND.getStatusCode());
186 apiError.setFields( "subId");
187 apiError.setMessage("subId " + key + " not found");
190 DrProvConnection prov = new DrProvConnection();
191 prov.makeSubPutConnection( key );
192 String resp = prov.doDeleteDr_Sub( sub, apiError );
193 logger.debug( "resp=" + resp );
195 apiError.setCode(200);
198 if ( apiError.is2xx() || unit_test.equals( "Yes" ) ) {
206 private String simulateResp( DR_Sub sub, String action ){
207 String server = "subscriber.onap.org";
209 if ( action.equals( "POST" ) ) {
210 RandomInteger ran = new RandomInteger(10000);
211 subid = Integer.toString( ran.next() );
212 } else if ( action.equals( "PUT" ) ) {
213 subid = sub.getSubId();
217 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, ",
218 server, subid, sub.getUsername(), sub.getUserpwd());
219 String links = String.format( "\"links\": {\"feed\": \"https://dr-prov/feedlog/%s\", \"self\": \"https://dr-prov/sub/%s\", \"log\": \"https://dr-prov/sublog/%s\" }",
224 logger.info( "DR_SubService:simulateResp=" + ret);