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.apache.log4j.Logger;
30 import org.onap.dmaap.dbcapi.aaf.client.DrProvConnection;
31 import org.onap.dmaap.dbcapi.aaf.database.DatabaseClass;
32 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
33 import org.onap.dmaap.dbcapi.model.ApiError;
34 import org.onap.dmaap.dbcapi.model.DR_Node;
35 import org.onap.dmaap.dbcapi.model.DmaapObject.DmaapObject_Status;
37 public class DR_NodeService extends BaseLoggingClass {
38 private class DrProv {
40 String currentStaticNodes;
42 private String getX( String X, ApiError apiError ) {
44 logger.info( "templog:getX at" + " 12.10.10" );
45 DrProvConnection prov = new DrProvConnection();
46 logger.info( "templog:getX at" + " 12.10.12" );
47 prov.makeNodesConnection( X );
48 logger.info( "templog:getX at" + " 12.10.14" );
49 String resp = prov.doGetNodes( apiError );
50 logger.info( "templog:getX at" + " 12.10.16" );
51 logger.info( "rc=" + apiError.getCode() );
52 logger.info( "templog:getX at" + " 12.10.18" );
56 private void setX( String X, String list, ApiError apiError ) {
57 DrProvConnection prov = new DrProvConnection();
58 prov.makeNodesConnection( X, list );
59 String resp = prov.doPutNodes( apiError );
62 private String removeFromList( String aNode, String aList ) {
63 String[] nodeList = aList.split("\\|");
64 StringBuilder res = new StringBuilder();
65 for ( String n: nodeList ) {
66 logger.info( "compare existing node " + n + " vs " + aNode );
67 if ( ! n.equals(aNode)) {
68 if (res.length() > 0 ) {
74 logger.info( "result=" + res.toString() );
75 return res.toString();
78 boolean containsNode( String aNode , ApiError apiError ){
80 logger.info( "templog:containsNode at" + " 12.10" );
81 //DrProvConnection prov = new DrProvConnection();
82 //prov.makeNodesConnection();
83 currentNodes = getX( "NODES", apiError );
84 logger.info( "templog:containsNode at" + " 12.12" );
85 if ( ! apiError.is2xx() || currentNodes == null ) {
86 logger.info( "templog:containsNode at" + " 12.14" );
89 logger.info( "templog:containsNode at" + " 12.16" );
90 logger.info( "NODES now=" + currentNodes );
91 String[] nodeList = currentNodes.split("\\|");
92 logger.info( "templog:containsNode at" + " 12.17" );
93 for( String n: nodeList ) {
94 logger.info( "templog:containsNode at" + " 12.18" );
95 logger.info( "compare existing node " + n + " vs " + aNode );
96 if ( n.equals(aNode) ) {
103 void addNode( String aNode, ApiError apiError ) {
105 currentNodes = currentNodes + "|" + aNode;
106 setX( "NODES", currentNodes, apiError );
110 void removeNode( String aNode, ApiError apiError ) {
111 currentNodes = removeFromList( aNode, currentNodes );
112 setX( "NODES", currentNodes, apiError );
115 public boolean containsStaticNode(String aNode, ApiError apiError) {
117 //DrProvConnection prov = new DrProvConnection();
118 //prov.makeNodesConnection();
119 currentStaticNodes = getX( "STATIC_ROUTING_NODES", apiError );
120 if (! apiError.is2xx() || currentStaticNodes == null ) {
123 logger.info( "STATIC_ROUTING_NODES now=" + currentNodes );
124 String[] nodeList = currentStaticNodes.split("\\|");
125 for( String n: nodeList ) {
126 logger.info( "compare existing node " + n + " vs " + aNode );
127 if ( n.equals(aNode) ) {
135 public void addStaticNode(String aNode, ApiError apiError) {
136 currentStaticNodes = currentStaticNodes + "|" + aNode;
137 setX( "STATIC_ROUTING_NODES", currentStaticNodes, apiError );
139 void removeStaticNode( String aNode, ApiError apiError ) {
140 currentStaticNodes = removeFromList( aNode, currentStaticNodes );
141 setX( "STATIC_ROUTING_NODES", currentStaticNodes, apiError );
144 private Map<String, DR_Node> dr_nodes = DatabaseClass.getDr_nodes();
146 public Map<String, DR_Node> getDr_Nodes() {
150 public List<DR_Node> getAllDr_Nodes() {
151 return new ArrayList<DR_Node>(dr_nodes.values());
154 public DR_Node getDr_Node( String fqdn, ApiError apiError ) {
155 DR_Node old = dr_nodes.get( fqdn );
157 apiError.setCode(Status.NOT_FOUND.getStatusCode());
158 apiError.setFields( "fqdn");
159 apiError.setMessage( "Node " + fqdn + " does not exist");
162 apiError.setCode(200);
166 public DR_Node addDr_Node( DR_Node node, ApiError apiError ) {
167 String fqdn = node.getFqdn();
168 DR_Node old = dr_nodes.get( fqdn );
170 apiError.setCode(Status.CONFLICT.getStatusCode());
171 apiError.setFields( "fqdn");
172 apiError.setMessage( "Node " + fqdn + " already exists");
175 logger.info( "templog:addDr_Node at" + " 10" );
177 DrProv drProv = new DrProv();
178 logger.info( "templog:addDr_Node at" + " 12" );
180 if ( ! drProv.containsNode( node.getFqdn(), apiError ) && apiError.is2xx() ) {
181 logger.info( "templog:addDr_Node at" + " 15" );
182 drProv.addNode( node.getFqdn(), apiError );
184 logger.info( "templog:addDr_Node at" + " 20" );
185 if ( ! apiError.is2xx()) {
188 logger.info( "templog:addDr_Node at" + " 30" );
189 DcaeLocationService locService = new DcaeLocationService();
190 if ( locService.isEdgeLocation( node.getDcaeLocationName()) && ! drProv.containsStaticNode( node.getFqdn(), apiError ) ) {
191 if ( apiError.is2xx() ) {
192 drProv.addStaticNode( node.getFqdn(), apiError );
195 logger.info( "templog:addDr_Node at" + " 40" );
196 if ( ! apiError.is2xx()) {
200 logger.info( "templog:addDr_Node at" + " 50" );
202 node.setStatus(DmaapObject_Status.VALID);
203 dr_nodes.put( node.getFqdn(), node );
204 logger.info( "templog:addDr_Node at" + " 60" );
205 apiError.setCode(200);
209 public DR_Node updateDr_Node( DR_Node node, ApiError apiError ) {
210 DR_Node old = dr_nodes.get( node );
212 apiError.setCode(Status.NOT_FOUND.getStatusCode());
213 apiError.setFields( "fqdn");
214 apiError.setMessage( "Node " + node + " does not exist");
218 dr_nodes.put( node.getFqdn(), node );
219 apiError.setCode(200);
223 public DR_Node removeDr_Node( String nodeName, ApiError apiError ) {
224 DR_Node old = dr_nodes.get( nodeName );
226 apiError.setCode(Status.NOT_FOUND.getStatusCode());
227 apiError.setFields( "fqdn");
228 apiError.setMessage( "Node " + nodeName + " does not exist");
232 DrProv drProv = new DrProv();
233 if ( drProv.containsNode( old.getFqdn(), apiError ) && apiError.is2xx() ) {
234 drProv.removeNode( old.getFqdn(), apiError );
236 DcaeLocationService locService = new DcaeLocationService();
237 if ( locService.isEdgeLocation( old.getDcaeLocationName()) && drProv.containsStaticNode( old.getFqdn(), apiError ) ) {
238 if ( apiError.is2xx()) {
239 drProv.removeStaticNode( old.getFqdn(), apiError );
244 apiError.setCode(200);
245 return dr_nodes.remove(nodeName);
248 public String getNodePatternAtLocation( String loc, boolean allowMult ) {
249 logger.info( "loc=" + loc );
253 StringBuilder str = new StringBuilder();
254 for( DR_Node node : dr_nodes.values() ) {
255 if ( loc.equals( node.getDcaeLocationName()) ) {
256 if ( str.length() > 0 ) {
259 str.append( node.getFqdn());
265 logger.info( "returning " + str.toString() );
266 return str.toString();