including missing files
[dmaap/dbcapi.git] / src / main / java / org / onap / dmaap / dbcapi / service / DR_NodeService.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.onap.dcae
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.dmaap.dbcapi.service;
22
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.Map;
26
27 import javax.ws.rs.core.Response.Status;
28
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;
36
37 public class DR_NodeService extends BaseLoggingClass {
38         private  class DrProv {
39                 String currentNodes;
40                 String currentStaticNodes;
41                 
42                 private String getX( String X, ApiError apiError ) {
43                         
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" );
53                         return resp;
54                 }
55                 
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 );
60                 }
61                 
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 ) {
69                                                 res.append( "|" );
70                                         }
71                                         res.append(n);
72                                 }
73                         }
74                         logger.info( "result=" + res.toString() );
75                         return res.toString();
76                 }
77                 
78                  boolean containsNode( String aNode , ApiError apiError ){
79         
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" );
87                                 return false;
88                         }
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) ) {
97                                         return true;
98                                 }
99                         }
100                         return false;
101                 }
102                 
103                  void addNode( String aNode, ApiError apiError ) {
104                         
105                         currentNodes = currentNodes + "|" + aNode;
106                         setX( "NODES", currentNodes, apiError );        
107
108                         
109                 }
110                 void removeNode( String aNode, ApiError apiError ) {
111                         currentNodes = removeFromList( aNode, currentNodes );
112                         setX( "NODES", currentNodes, apiError );                        
113                 }
114
115                 public boolean containsStaticNode(String aNode, ApiError apiError) {
116         
117                         //DrProvConnection prov = new DrProvConnection();
118                         //prov.makeNodesConnection();   
119                         currentStaticNodes = getX( "STATIC_ROUTING_NODES", apiError );
120                         if (! apiError.is2xx() || currentStaticNodes == null ) {
121                                 return false;
122                         }
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) ) {
128                                         return true;
129                                 }
130                         }
131                         return false;
132                 }
133                 
134
135                 public void addStaticNode(String aNode, ApiError apiError) {
136                         currentStaticNodes = currentStaticNodes + "|" + aNode;
137                         setX( "STATIC_ROUTING_NODES", currentStaticNodes, apiError );
138                 }
139                 void removeStaticNode( String aNode, ApiError apiError ) {
140                         currentStaticNodes = removeFromList( aNode, currentStaticNodes );
141                         setX( "STATIC_ROUTING_NODES", currentStaticNodes, apiError );                   
142                 }
143         }       
144         private Map<String, DR_Node> dr_nodes = DatabaseClass.getDr_nodes();
145         
146         public Map<String, DR_Node> getDr_Nodes() {
147                 return dr_nodes;
148         }
149         
150         public List<DR_Node> getAllDr_Nodes() {
151                 return new ArrayList<DR_Node>(dr_nodes.values());
152         }
153         
154         public DR_Node getDr_Node( String fqdn, ApiError apiError ) {
155                 DR_Node old = dr_nodes.get( fqdn );
156                 if ( old == null ) {
157                         apiError.setCode(Status.NOT_FOUND.getStatusCode());
158                         apiError.setFields( "fqdn");
159                         apiError.setMessage( "Node " + fqdn + " does not exist");
160                         return null;
161                 }
162                 apiError.setCode(200);
163                 return old;
164         }
165
166         public DR_Node addDr_Node( DR_Node node, ApiError apiError ) {
167                 String fqdn = node.getFqdn();
168                 DR_Node old = dr_nodes.get( fqdn );
169                 if ( old != null ) {
170                         apiError.setCode(Status.CONFLICT.getStatusCode());
171                         apiError.setFields( "fqdn");
172                         apiError.setMessage( "Node " + fqdn + " already exists");
173                         return null;
174                 }
175                 logger.info( "templog:addDr_Node at" + " 10" );
176                 
177                 DrProv drProv = new DrProv();
178                 logger.info( "templog:addDr_Node at" + " 12" );
179
180                 if ( ! drProv.containsNode( node.getFqdn(), apiError ) && apiError.is2xx() ) {
181                         logger.info( "templog:addDr_Node at" + " 15" );
182                         drProv.addNode( node.getFqdn(), apiError );
183                 }
184                 logger.info( "templog:addDr_Node at" + " 20" );
185                 if ( ! apiError.is2xx()) {
186                         return null;
187                 }
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 );
193                         }
194                 }
195                 logger.info( "templog:addDr_Node at" + " 40" );
196                 if ( ! apiError.is2xx()) {
197                         return null;
198                 }
199                 
200                 logger.info( "templog:addDr_Node at" + " 50" );
201                 node.setLastMod();
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);
206                 return node;
207         }
208         
209         public DR_Node updateDr_Node( DR_Node node, ApiError apiError ) {
210                 DR_Node old = dr_nodes.get( node );
211                 if ( old == null ) {
212                         apiError.setCode(Status.NOT_FOUND.getStatusCode());
213                         apiError.setFields( "fqdn");
214                         apiError.setMessage( "Node " + node + " does not exist");
215                         return null;
216                 }
217                 node.setLastMod();
218                 dr_nodes.put( node.getFqdn(), node );
219                 apiError.setCode(200);
220                 return node;
221         }
222         
223         public DR_Node removeDr_Node( String nodeName, ApiError apiError ) {
224                 DR_Node old = dr_nodes.get( nodeName );
225                 if ( old == null ) {
226                         apiError.setCode(Status.NOT_FOUND.getStatusCode());
227                         apiError.setFields( "fqdn");
228                         apiError.setMessage( "Node " + nodeName + " does not exist");
229                         return null;
230                 }
231                 
232                 DrProv drProv = new DrProv();
233                 if ( drProv.containsNode( old.getFqdn(), apiError ) && apiError.is2xx() ) {
234                         drProv.removeNode( old.getFqdn(), apiError );
235                 }
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 );
240                         }
241                         
242                 }
243                 
244                 apiError.setCode(200);
245                 return dr_nodes.remove(nodeName);
246         }               
247
248         public String getNodePatternAtLocation( String loc, boolean allowMult ) {
249                 logger.info( "loc=" + loc );
250                 if ( loc == null ) {
251                         return null;
252                 }
253                 StringBuilder str = new StringBuilder();
254                 for( DR_Node node : dr_nodes.values() ) {
255                         if ( loc.equals( node.getDcaeLocationName()) ) {
256                                 if ( str.length() > 0 ) {
257                                         str.append( ",");
258                                 }
259                                 str.append( node.getFqdn());
260                                 if ( ! allowMult ) {
261                                         break;
262                                 }
263                         }
264                 }
265                 logger.info( "returning " + str.toString() );
266                 return str.toString();
267         }
268 }