aboutsummaryrefslogtreecommitdiffstats
path: root/dmaap-bc/src/main/java/org/onap/dmaap/dbcapi/service/DR_SubService.java
blob: 0a583a0db0082e58f461eeb25bb9324c5575742e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*-
 * ============LICENSE_START=======================================================
 * org.onap.dmaap
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 *
 * Modifications Copyright (C) 2019 IBM.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.dmaap.dbcapi.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.ws.rs.core.Response.Status;

import org.onap.dmaap.dbcapi.client.DrProvConnection;
import org.onap.dmaap.dbcapi.database.DatabaseClass;
import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
import org.onap.dmaap.dbcapi.logging.DmaapbcLogMessageEnum;
import org.onap.dmaap.dbcapi.model.ApiError;
import org.onap.dmaap.dbcapi.model.DR_Sub;
import org.onap.dmaap.dbcapi.util.DmaapConfig;
import org.onap.dmaap.dbcapi.util.RandomInteger;

public class DR_SubService extends BaseLoggingClass {

	private Map<String, DR_Sub> dr_subs = DatabaseClass.getDr_subs();
	private DR_NodeService nodeService = new DR_NodeService();
	private String provURL;
	private static DrProvConnection prov;
	
	private String unit_test;
	
	
	public DR_SubService(  ) {
		logger.debug( "Entry: DR_SubService (with no args)" );
		DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
		unit_test = p.getProperty( "UnitTest", "No" );
	}	
	public DR_SubService( String subURL ) {
		logger.debug( "Entry: DR_SubService " + subURL );
		provURL = subURL;
		DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
		unit_test = p.getProperty( "UnitTest", "No" );
	}
	public Map<String, DR_Sub> getDR_Subs() {
		logger.debug( "enter getDR_Subs()");
		return dr_subs;
	}
		
	public List<DR_Sub> getAllDr_Subs() {
		logger.debug( "enter getAllDR_Subs()");
		return new ArrayList<>(dr_subs.values());
	}
	
	public ArrayList<DR_Sub> getDr_SubsByFeedId( String pubId ) {
		ArrayList<DR_Sub> someSubs = new ArrayList<>();
		for( DR_Sub sub : dr_subs.values() ) {
			if ( pubId.equals(  sub.getFeedId()  )) {
				someSubs.add( sub );
			}
		}
			
		return someSubs;
	}
	public DR_Sub getDr_Sub( String key, ApiError apiError ) {	
		logger.debug( "enter getDR_Sub()");
		DR_Sub sub = dr_subs.get( key );
		if ( sub == null ) {
			apiError.setCode(Status.NOT_FOUND.getStatusCode());
			apiError.setFields( "subId");
			apiError.setMessage("subId " + key + " not found");
		} else {
			apiError.setCode(200);
		}
		return sub;
	}

	public DR_Sub addDr_Sub( DR_Sub sub, ApiError apiError ) {
		logger.debug( "enter addDR_Subs()");
		prov = new DrProvConnection();
		prov.makeSubPostConnection( provURL );
		String resp = prov.doPostDr_Sub( sub, apiError );
		if ( "Yes".equals(unit_test) ) {
			resp = simulateResp( sub, "POST" );
			apiError.setCode(201);
		}
		logger.debug( "addDr_Sub resp=" + resp );

		DR_Sub snew = null;

		if ( resp != null ) {
			snew = new DR_Sub( resp );
			snew.setDcaeLocationName(sub.getDcaeLocationName());
			snew.setLastMod();
			addEgressRoute( snew, apiError );
			dr_subs.put( snew.getSubId(), snew );	
			apiError.setCode(201);
		} else {
			apiError.setCode(400);
		}
		
		return snew;
	}

	private void addEgressRoute( DR_Sub sub, ApiError err ) {
		
		String nodePattern = nodeService.getNodePatternAtLocation( sub.getDcaeLocationName(), false );
		if ( nodePattern != null && nodePattern.length() > 0 ) {
			logger.info( "creating egress rule: sub " + sub.getSubId() + " on feed " + sub.getFeedId() + " to " + nodePattern);
			prov.makeEgressConnection( sub.getSubId(),  nodePattern);
			int rc = prov.doXgressPost(err);
			logger.info( "rc=" + rc + " error code=" + err.getCode() );
			
			if ( rc != 200 ) {
				switch( rc ) {
				case 403:
					logger.error( "Not authorized for DR egress API");
					err.setCode(500);
					err.setMessage("API deployment/configuration error - contact support");
					err.setFields( "PROV_AUTH_ADDRESSES");
					break;
				
				default: 
					logger.info( DmaapbcLogMessageEnum.EGRESS_CREATE_ERROR, Integer.toString(rc),  sub.getSubId(), sub.getFeedId(), nodePattern);
				}
			}

		}
	}
	
	public DR_Sub updateDr_Sub( DR_Sub obj, ApiError apiError ) {
		logger.debug( "enter updateDR_Subs()");

		DrProvConnection prov = new DrProvConnection();
		prov.makeSubPutConnection( obj.getSubId() );
		String resp = prov.doPutDr_Sub( obj, apiError );
		if ( unit_test.equals( "Yes" ) ) {
			resp = simulateResp( obj, "PUT" );
			apiError.setCode(200);
		}
		logger.debug( "resp=" + resp );

		DR_Sub snew = null;

		if ( resp != null ) {
			snew = new DR_Sub( resp );
			snew.setDcaeLocationName(obj.getDcaeLocationName());
			snew.setLastMod();
			dr_subs.put( snew.getSubId(), snew );	
			apiError.setCode(200);
		} else if ( apiError.is2xx()) {
			apiError.setCode(400);
			apiError.setMessage("unexpected empty response from DR Prov");
		}
		
		return snew;
	}
		
	public void removeDr_Sub( String key, ApiError apiError ) {
		removeDr_Sub( key, apiError, true );
		return;
	}
	
	public void removeDr_Sub( String key, ApiError apiError, boolean hitDR ) {
		logger.debug( "enter removeDR_Subs()");
		
		DR_Sub sub = dr_subs.get( key );
		if ( sub == null ) {
			apiError.setCode(Status.NOT_FOUND.getStatusCode());
			apiError.setFields( "subId");
			apiError.setMessage("subId " + key + " not found");
		} else {
			if ( hitDR ) {
				DrProvConnection prov = new DrProvConnection();
				prov.makeSubPutConnection( key );
				String resp = prov.doDeleteDr_Sub( sub, apiError );
				logger.debug( "resp=" + resp );
			} else {
				apiError.setCode(200);
			}
			
			if ( apiError.is2xx() || unit_test.equals( "Yes" ) ) {
				dr_subs.remove(key);
			}
		}

		return;
	}	

	private String simulateResp( DR_Sub sub, String action ){
		String server = "subscriber.onap.org";
		String subid;
		if ( action.equals( "POST" ) ) { 
			RandomInteger ran = new RandomInteger(10000);
			subid = Integer.toString( ran.next() );
		} else if ( action.equals( "PUT" ) ) {
			subid = sub.getSubId();
		} else {
			subid = "99";
		}
		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, ", 
			server, subid, sub.getUsername(), sub.getUserpwd());
		String links = String.format( "\"links\": {\"feed\": \"https://dr-prov/feedlog/%s\", \"self\": \"https://dr-prov/sub/%s\", \"log\": \"https://dr-prov/sublog/%s\" }", 
				sub.getFeedId(),
				subid,
				subid );
		ret += links + "}";
		logger.info( "DR_SubService:simulateResp=" + ret);

		return ret;
	}
}