e80b53129f7f4084ef86d862ef82889f48382534
[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                         DrProvConnection prov = new DrProvConnection();
45                         prov.makeNodesConnection( X );  
46                         String resp  = prov.doGetNodes(  apiError );
47                         logger.info( "rc=" + apiError.getCode() );
48                         return resp;
49                 }
50                 
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 );
55                 }
56                 
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 ) {
64                                                 res.append( "|" );
65                                         }
66                                         res.append(n);
67                                 }
68                         }
69                         logger.info( "result=" + res.toString() );
70                         return res.toString();
71                 }
72                 
73                  boolean containsNode( String aNode , ApiError apiError ){
74         
75                         //DrProvConnection prov = new DrProvConnection();
76                         //prov.makeNodesConnection();   
77                         currentNodes = getX( "NODES", apiError );
78                         if ( ! apiError.is2xx() || currentNodes == null ) {
79                                 return false;
80                         }
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) ) {
86                                         return true;
87                                 }
88                         }
89                         return false;
90                 }
91                 
92                  void addNode( String aNode, ApiError apiError ) {
93                         
94                         currentNodes = currentNodes + "|" + aNode;
95                         setX( "NODES", currentNodes, apiError );        
96
97                         
98                 }
99                 void removeNode( String aNode, ApiError apiError ) {
100                         currentNodes = removeFromList( aNode, currentNodes );
101                         setX( "NODES", currentNodes, apiError );                        
102                 }
103
104                 public boolean containsStaticNode(String aNode, ApiError apiError) {
105         
106                         //DrProvConnection prov = new DrProvConnection();
107                         //prov.makeNodesConnection();   
108                         currentStaticNodes = getX( "STATIC_ROUTING_NODES", apiError );
109                         if (! apiError.is2xx() || currentStaticNodes == null ) {
110                                 return false;
111                         }
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) ) {
117                                         return true;
118                                 }
119                         }
120                         return false;
121                 }
122                 
123
124                 public void addStaticNode(String aNode, ApiError apiError) {
125                         currentStaticNodes = currentStaticNodes + "|" + aNode;
126                         setX( "STATIC_ROUTING_NODES", currentStaticNodes, apiError );
127                 }
128                 void removeStaticNode( String aNode, ApiError apiError ) {
129                         currentStaticNodes = removeFromList( aNode, currentStaticNodes );
130                         setX( "STATIC_ROUTING_NODES", currentStaticNodes, apiError );                   
131                 }
132         }       
133         private Map<String, DR_Node> dr_nodes = DatabaseClass.getDr_nodes();
134         
135         public Map<String, DR_Node> getDr_Nodes() {
136                 return dr_nodes;
137         }
138         
139         public List<DR_Node> getAllDr_Nodes() {
140                 return new ArrayList<DR_Node>(dr_nodes.values());
141         }
142         
143         public DR_Node getDr_Node( String fqdn, ApiError apiError ) {
144                 DR_Node old = dr_nodes.get( fqdn );
145                 if ( old == null ) {
146                         apiError.setCode(Status.NOT_FOUND.getStatusCode());
147                         apiError.setFields( "fqdn");
148                         apiError.setMessage( "Node " + fqdn + " does not exist");
149                         return null;
150                 }
151                 apiError.setCode(200);
152                 return old;
153         }
154
155         public DR_Node addDr_Node( DR_Node node, ApiError apiError ) {
156                 String fqdn = node.getFqdn();
157                 DR_Node old = dr_nodes.get( fqdn );
158                 if ( old != null ) {
159                         apiError.setCode(Status.CONFLICT.getStatusCode());
160                         apiError.setFields( "fqdn");
161                         apiError.setMessage( "Node " + fqdn + " already exists");
162                         return null;
163                 }
164                 
165                 DrProv drProv = new DrProv();
166
167                 if ( ! drProv.containsNode( node.getFqdn(), apiError ) && apiError.is2xx() ) {
168                         drProv.addNode( node.getFqdn(), apiError );
169                 }
170                 if ( ! apiError.is2xx()) {
171                         return null;
172                 }
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 );
177                         }
178                 }
179                 if ( ! apiError.is2xx()) {
180                         return null;
181                 }
182                 
183                 node.setLastMod();
184                 node.setStatus(DmaapObject_Status.VALID);
185                 dr_nodes.put( node.getFqdn(), node );
186                 apiError.setCode(200);
187                 return node;
188         }
189         
190         public DR_Node updateDr_Node( DR_Node node, ApiError apiError ) {
191                 DR_Node old = dr_nodes.get( node );
192                 if ( old == null ) {
193                         apiError.setCode(Status.NOT_FOUND.getStatusCode());
194                         apiError.setFields( "fqdn");
195                         apiError.setMessage( "Node " + node + " does not exist");
196                         return null;
197                 }
198                 node.setLastMod();
199                 dr_nodes.put( node.getFqdn(), node );
200                 apiError.setCode(200);
201                 return node;
202         }
203         
204         public DR_Node removeDr_Node( String nodeName, ApiError apiError ) {
205                 DR_Node old = dr_nodes.get( nodeName );
206                 if ( old == null ) {
207                         apiError.setCode(Status.NOT_FOUND.getStatusCode());
208                         apiError.setFields( "fqdn");
209                         apiError.setMessage( "Node " + nodeName + " does not exist");
210                         return null;
211                 }
212                 
213                 DrProv drProv = new DrProv();
214                 if ( drProv.containsNode( old.getFqdn(), apiError ) && apiError.is2xx() ) {
215                         drProv.removeNode( old.getFqdn(), apiError );
216                 }
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 );
221                         }
222                         
223                 }
224                 
225                 apiError.setCode(200);
226                 return dr_nodes.remove(nodeName);
227         }               
228
229         public String getNodePatternAtLocation( String loc, boolean allowMult ) {
230                 logger.info( "loc=" + loc );
231                 if ( loc == null ) {
232                         return null;
233                 }
234                 StringBuilder str = new StringBuilder();
235                 for( DR_Node node : dr_nodes.values() ) {
236                         if ( loc.equals( node.getDcaeLocationName()) ) {
237                                 if ( str.length() > 0 ) {
238                                         str.append( ",");
239                                 }
240                                 str.append( node.getFqdn());
241                                 if ( ! allowMult ) {
242                                         break;
243                                 }
244                         }
245                 }
246                 logger.info( "returning " + str.toString() );
247                 return str.toString();
248         }
249 }