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 DrProvConnection prov = new DrProvConnection();
45 prov.makeNodesConnection( X );
46 String resp = prov.doGetNodes( apiError );
47 logger.info( "rc=" + apiError.getCode() );
51 private void setX( String X, String list, ApiError apiError ) {
52 DrProvConnection prov = new DrProvConnection();
53 prov.makeNodesConnection( X, list );
54 String resp = prov.doPutNodes( apiError );
57 private String removeFromList( String aNode, String aList ) {
58 String[] nodeList = aList.split("\\|");
59 StringBuilder res = new StringBuilder();
60 for ( String n: nodeList ) {
61 logger.info( "compare existing node " + n + " vs " + aNode );
62 if ( ! n.equals(aNode)) {
63 if (res.length() > 0 ) {
69 logger.info( "result=" + res.toString() );
70 return res.toString();
73 boolean containsNode( String aNode , ApiError apiError ){
75 //DrProvConnection prov = new DrProvConnection();
76 //prov.makeNodesConnection();
77 currentNodes = getX( "NODES", apiError );
78 if ( ! apiError.is2xx() || currentNodes == null ) {
81 logger.info( "NODES now=" + currentNodes );
82 String[] nodeList = currentNodes.split("\\|");
83 for( String n: nodeList ) {
84 logger.info( "compare existing node " + n + " vs " + aNode );
85 if ( n.equals(aNode) ) {
92 void addNode( String aNode, ApiError apiError ) {
94 currentNodes = currentNodes + "|" + aNode;
95 setX( "NODES", currentNodes, apiError );
99 void removeNode( String aNode, ApiError apiError ) {
100 currentNodes = removeFromList( aNode, currentNodes );
101 setX( "NODES", currentNodes, apiError );
104 public boolean containsStaticNode(String aNode, ApiError apiError) {
106 //DrProvConnection prov = new DrProvConnection();
107 //prov.makeNodesConnection();
108 currentStaticNodes = getX( "STATIC_ROUTING_NODES", apiError );
109 if (! apiError.is2xx() || currentStaticNodes == null ) {
112 logger.info( "STATIC_ROUTING_NODES now=" + currentNodes );
113 String[] nodeList = currentStaticNodes.split("\\|");
114 for( String n: nodeList ) {
115 logger.info( "compare existing node " + n + " vs " + aNode );
116 if ( n.equals(aNode) ) {
124 public void addStaticNode(String aNode, ApiError apiError) {
125 currentStaticNodes = currentStaticNodes + "|" + aNode;
126 setX( "STATIC_ROUTING_NODES", currentStaticNodes, apiError );
128 void removeStaticNode( String aNode, ApiError apiError ) {
129 currentStaticNodes = removeFromList( aNode, currentStaticNodes );
130 setX( "STATIC_ROUTING_NODES", currentStaticNodes, apiError );
133 private Map<String, DR_Node> dr_nodes = DatabaseClass.getDr_nodes();
135 public Map<String, DR_Node> getDr_Nodes() {
139 public List<DR_Node> getAllDr_Nodes() {
140 return new ArrayList<DR_Node>(dr_nodes.values());
143 public DR_Node getDr_Node( String fqdn, ApiError apiError ) {
144 DR_Node old = dr_nodes.get( fqdn );
146 apiError.setCode(Status.NOT_FOUND.getStatusCode());
147 apiError.setFields( "fqdn");
148 apiError.setMessage( "Node " + fqdn + " does not exist");
151 apiError.setCode(200);
155 public DR_Node addDr_Node( DR_Node node, ApiError apiError ) {
156 String fqdn = node.getFqdn();
157 DR_Node old = dr_nodes.get( fqdn );
159 apiError.setCode(Status.CONFLICT.getStatusCode());
160 apiError.setFields( "fqdn");
161 apiError.setMessage( "Node " + fqdn + " already exists");
165 DrProv drProv = new DrProv();
167 if ( ! drProv.containsNode( node.getFqdn(), apiError ) && apiError.is2xx() ) {
168 drProv.addNode( node.getFqdn(), apiError );
170 if ( ! apiError.is2xx()) {
173 DcaeLocationService locService = new DcaeLocationService();
174 if ( locService.isEdgeLocation( node.getDcaeLocationName()) && ! drProv.containsStaticNode( node.getFqdn(), apiError ) ) {
175 if ( apiError.is2xx() ) {
176 drProv.addStaticNode( node.getFqdn(), apiError );
179 if ( ! apiError.is2xx()) {
184 node.setStatus(DmaapObject_Status.VALID);
185 dr_nodes.put( node.getFqdn(), node );
186 apiError.setCode(200);
190 public DR_Node updateDr_Node( DR_Node node, ApiError apiError ) {
191 DR_Node old = dr_nodes.get( node );
193 apiError.setCode(Status.NOT_FOUND.getStatusCode());
194 apiError.setFields( "fqdn");
195 apiError.setMessage( "Node " + node + " does not exist");
199 dr_nodes.put( node.getFqdn(), node );
200 apiError.setCode(200);
204 public DR_Node removeDr_Node( String nodeName, ApiError apiError ) {
205 DR_Node old = dr_nodes.get( nodeName );
207 apiError.setCode(Status.NOT_FOUND.getStatusCode());
208 apiError.setFields( "fqdn");
209 apiError.setMessage( "Node " + nodeName + " does not exist");
213 DrProv drProv = new DrProv();
214 if ( drProv.containsNode( old.getFqdn(), apiError ) && apiError.is2xx() ) {
215 drProv.removeNode( old.getFqdn(), apiError );
217 DcaeLocationService locService = new DcaeLocationService();
218 if ( locService.isEdgeLocation( old.getDcaeLocationName()) && drProv.containsStaticNode( old.getFqdn(), apiError ) ) {
219 if ( apiError.is2xx()) {
220 drProv.removeStaticNode( old.getFqdn(), apiError );
225 apiError.setCode(200);
226 return dr_nodes.remove(nodeName);
229 public String getNodePatternAtLocation( String loc, boolean allowMult ) {
230 logger.info( "loc=" + loc );
234 StringBuilder str = new StringBuilder();
235 for( DR_Node node : dr_nodes.values() ) {
236 if ( loc.equals( node.getDcaeLocationName()) ) {
237 if ( str.length() > 0 ) {
240 str.append( node.getFqdn());
246 logger.info( "returning " + str.toString() );
247 return str.toString();