aboutsummaryrefslogtreecommitdiffstats
path: root/dmaap-bc/src/main/java/org/onap/dmaap/dbcapi/service/DR_NodeService.java
blob: b29beb9f260fb714960ea3a965f2ffb8b046d2bc (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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*-
 * ============LICENSE_START=======================================================
 * org.onap.dcae
 * ================================================================================
 * 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.model.ApiError;
import org.onap.dmaap.dbcapi.model.DR_Node;
import org.onap.dmaap.dbcapi.model.DmaapObject.DmaapObject_Status;
import org.onap.dmaap.dbcapi.util.DmaapConfig;


public class DR_NodeService extends BaseLoggingClass {
	private  class DrProv {
		String currentNodes;
		String currentStaticNodes;
		
		private String getX( String X, ApiError apiError ) {
			
		logger.info( "templog:getX at" + " 12.10.10" );
			DrProvConnection prov = new DrProvConnection();
		logger.info( "templog:getX at" + " 12.10.12" );
			prov.makeNodesConnection( X );	
		logger.info( "templog:getX at" + " 12.10.14" );
			String resp  = prov.doGetNodes(  apiError );
		logger.info( "templog:getX at" + " 12.10.16" );
			logger.info( "rc=" + apiError.getCode() );
		logger.info( "templog:getX at" + " 12.10.18" );
			return resp;
		}
		
		private void setX( String X, String list, ApiError apiError ) {
			DrProvConnection prov = new DrProvConnection();
			prov.makeNodesConnection( X, list );	
			prov.doPutNodes( apiError );
		}
		
		private String removeFromList( String aNode, String aList ) {
			String[] nodeList = aList.split("\\|");
			StringBuilder res = new StringBuilder();
			for ( String n: nodeList ) {
				logger.info( "compare existing node " + n + " vs " + aNode );
				if ( ! n.equals(aNode)) {
					if (res.length() > 0 ) {
						res.append( "|" );
					}
					res.append(n);
				}
			}
			logger.info( "result=" + res.toString() );
			return res.toString();
		}
		
		 boolean containsNode( String aNode , ApiError apiError ){
	
		logger.info( "templog:containsNode at" + " 12.10" );
			//DrProvConnection prov = new DrProvConnection();
			//prov.makeNodesConnection();	
			currentNodes = getX( "NODES", apiError );
		logger.info( "templog:containsNode at" + " 12.12" );
			if ( ! apiError.is2xx() || currentNodes == null ) {
		logger.info( "templog:containsNode at" + " 12.14" );
				return false;
			}
		logger.info( "templog:containsNode at" + " 12.16" );
			logger.info( "NODES now=" + currentNodes );
			String[] nodeList = currentNodes.split("\\|");
		logger.info( "templog:containsNode at" + " 12.17" );
			for( String n: nodeList ) {
		logger.info( "templog:containsNode at" + " 12.18" );
				logger.info( "compare existing node " + n + " vs " + aNode );
				if ( n.equals(aNode) ) {
					return true;
				}
			}
			return false;
		}
		
		 void addNode( String aNode, ApiError apiError ) {
			
			currentNodes = currentNodes + "|" + aNode;
			setX( "NODES", currentNodes, apiError );	

			
		}
		void removeNode( String aNode, ApiError apiError ) {
			currentNodes = removeFromList( aNode, currentNodes );
			setX( "NODES", currentNodes, apiError );			
		}

		public boolean containsStaticNode(String aNode, ApiError apiError) {
	
			//DrProvConnection prov = new DrProvConnection();
			//prov.makeNodesConnection();	
			currentStaticNodes = getX( "STATIC_ROUTING_NODES", apiError );
			if (! apiError.is2xx() || currentStaticNodes == null ) {
				return false;
			}
			logger.info( "STATIC_ROUTING_NODES now=" + currentNodes );
			String[] nodeList = currentStaticNodes.split("\\|");
			for( String n: nodeList ) {
				logger.info( "compare existing node " + n + " vs " + aNode );
				if ( n.equals(aNode) ) {
					return true;
				}
			}
			return false;
		}
		

		public void addStaticNode(String aNode, ApiError apiError) {
			currentStaticNodes = currentStaticNodes + "|" + aNode;
			setX( "STATIC_ROUTING_NODES", currentStaticNodes, apiError );
		}
		void removeStaticNode( String aNode, ApiError apiError ) {
			currentStaticNodes = removeFromList( aNode, currentStaticNodes );
			setX( "STATIC_ROUTING_NODES", currentStaticNodes, apiError );			
		}
	}	

	DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
	String unit_test = p.getProperty( "UnitTest", "No" );

	private Map<String, DR_Node> dr_nodes = DatabaseClass.getDr_nodes();
	
	public Map<String, DR_Node> getDr_Nodes() {
		return dr_nodes;
	}
	
	public List<DR_Node> getAllDr_Nodes() {
		return new ArrayList<DR_Node>(dr_nodes.values());
	}
	
	public DR_Node getDr_Node( String fqdn, ApiError apiError ) {
		DR_Node old = dr_nodes.get( fqdn );
		if ( old == null ) {
			apiError.setCode(Status.NOT_FOUND.getStatusCode());
			apiError.setFields( "fqdn");
			apiError.setMessage( "Node " + fqdn + " does not exist");
			return null;
		}
		apiError.setCode(200);
		return old;
	}

	public DR_Node addDr_Node( DR_Node node, ApiError apiError ) {
		String fqdn = node.getFqdn();
		DR_Node old = dr_nodes.get( fqdn );
		if ( old != null ) {
			apiError.setCode(Status.CONFLICT.getStatusCode());
			apiError.setFields( "fqdn");
			apiError.setMessage( "Node " + fqdn + " already exists");
			return null;
		}
		logger.info( "templog:addDr_Node at" + " 10" );
		
		DrProv drProv = new DrProv();
		logger.info( "templog:addDr_Node at" + " 12" );

		if ( ! drProv.containsNode( node.getFqdn(), apiError ) && apiError.is2xx() ) {
			logger.info( "templog:addDr_Node at" + " 15" );
			drProv.addNode( node.getFqdn(), apiError );
		}
		logger.info( "templog:addDr_Node at" + " 20" );
		if ( ! apiError.is2xx() && ! unit_test.equals( "Yes" ) ) {
			return null;
		}
		logger.info( "templog:addDr_Node at" + " 30" );
		DcaeLocationService locService = new DcaeLocationService();
		if ( locService.isEdgeLocation( node.getDcaeLocationName()) && ! drProv.containsStaticNode( node.getFqdn(), apiError ) ) {
			if ( apiError.is2xx() ) {
				drProv.addStaticNode( node.getFqdn(), apiError );
			}
		}
		logger.info( "templog:addDr_Node at" + " 40" );
		if ( ! apiError.is2xx() && ! unit_test.equals("Yes") ) {
			return null;
		}
		
		logger.info( "templog:addDr_Node at" + " 50" );
		node.setLastMod();
		node.setStatus(DmaapObject_Status.VALID);
		dr_nodes.put( node.getFqdn(), node );
		logger.info( "templog:addDr_Node at" + " 60" );
		apiError.setCode(200);
		return node;
	}
	
	public DR_Node updateDr_Node( DR_Node node, ApiError apiError ) {
		DR_Node old = dr_nodes.get( node.getFqdn() );
		if ( old == null ) {
			apiError.setCode(Status.NOT_FOUND.getStatusCode());
			apiError.setFields( "fqdn");
			apiError.setMessage( "Node " + node.getFqdn() + " does not exist");
			return null;
		}
		node.setLastMod();
		dr_nodes.put( node.getFqdn(), node );
		apiError.setCode(200);
		return node;
	}
	
	public DR_Node removeDr_Node( String nodeName, ApiError apiError ) {
		DR_Node old = dr_nodes.get( nodeName );
		if ( old == null ) {
			apiError.setCode(Status.NOT_FOUND.getStatusCode());
			apiError.setFields( "fqdn");
			apiError.setMessage( "Node " + nodeName + " does not exist");
			return null;
		}
		
		DrProv drProv = new DrProv();
		if ( drProv.containsNode( old.getFqdn(), apiError ) && apiError.is2xx() ) {
			drProv.removeNode( old.getFqdn(), apiError );
		}
		DcaeLocationService locService = new DcaeLocationService();
		if ( locService.isEdgeLocation( old.getDcaeLocationName()) && drProv.containsStaticNode( old.getFqdn(), apiError ) ) {
			if ( apiError.is2xx()) {
				drProv.removeStaticNode( old.getFqdn(), apiError );
			}
			
		}
		
		apiError.setCode(200);
		return dr_nodes.remove(nodeName);
	}		

	public String getNodePatternAtLocation( String loc, boolean allowMult ) {
		logger.info( "loc=" + loc );
		if ( loc == null ) {
			return null;
		}
		StringBuilder str = new StringBuilder();
		for( DR_Node node : dr_nodes.values() ) {
			if ( loc.equals( node.getDcaeLocationName()) ) {
				if ( str.length() > 0 ) {
					str.append( ",");
				}
				str.append( node.getFqdn());
				if ( ! allowMult ) {
					break;
				}
			}
		}
		logger.info( "returning " + str.toString() );
		return str.toString();
	}
}