[DMAAP-BC] Consolidate bus controller repos
[dmaap/buscontroller.git] / dmaap-bc / src / main / java / org / onap / dmaap / dbcapi / service / DR_PubService.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.onap.dmaap
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 import javax.ws.rs.core.Response.Status;
27 import org.onap.dmaap.dbcapi.client.DrProvConnection;
28 import org.onap.dmaap.dbcapi.database.DatabaseClass;
29 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
30 import org.onap.dmaap.dbcapi.logging.DmaapbcLogMessageEnum;
31 import org.onap.dmaap.dbcapi.model.ApiError;
32 import org.onap.dmaap.dbcapi.model.DR_Pub;
33 import org.onap.dmaap.dbcapi.model.DmaapObject.DmaapObject_Status;
34
35 public class DR_PubService  extends BaseLoggingClass{
36         
37         private Map<String, DR_Pub> dr_pubs = DatabaseClass.getDr_pubs();
38         private DR_NodeService nodeService = new DR_NodeService();
39         private static DrProvConnection prov;
40         
41         public DR_PubService() {
42                 super();
43                 prov = new DrProvConnection();
44         }
45
46         public Map<String, DR_Pub> getDr_Pubs() {                       
47                 return dr_pubs;
48         }
49                 
50         public List<DR_Pub> getAllDr_Pubs() {
51                 return new ArrayList<DR_Pub>(dr_pubs.values());
52         }
53         
54         public ArrayList<DR_Pub> getDr_PubsByFeedId( String feedId ) {
55                 ArrayList<DR_Pub> somePubs = new ArrayList<DR_Pub>();
56                 for( DR_Pub pub : dr_pubs.values() ) {
57                         if ( feedId.equals(  pub.getFeedId()  )) {
58                                 somePubs.add( pub );
59                         }
60                 }
61                         
62                 return somePubs;
63         }
64                 
65         public DR_Pub getDr_Pub( String key, ApiError err ) {   
66                 DR_Pub pub = dr_pubs.get( key );
67                 if ( pub == null ) {
68                         err.setCode(Status.NOT_FOUND.getStatusCode());
69                         err.setFields( "pubId");
70                         err.setMessage("DR_Pub with pubId = " + key + " not found");
71                 } else {
72                         err.setCode(Status.OK.getStatusCode());
73                 }
74                 return pub;
75         }
76         
77         private void addIngressRoute( DR_Pub pub, ApiError err ) {
78                 
79                 String nodePattern = nodeService.getNodePatternAtLocation( pub.getDcaeLocationName(), true );
80                 if ( nodePattern != null && nodePattern.length() > 0 ) {
81                         logger.info( "creating ingress rule: pub " + pub.getPubId() + " on feed " + pub.getFeedId() + " to " + nodePattern);
82                         prov.makeIngressConnection( pub.getFeedId(), pub.getUsername(), "-", nodePattern);
83                         int rc = prov.doXgressPost(err);
84                         logger.info( "rc=" + rc + " error code=" + err.getCode() );
85                         
86                         if ( rc != 200 ) {
87                                 switch( rc ) {
88                                 case 403:
89                                         logger.error( "Not authorized for DR ingress API");
90                                         err.setCode(500);
91                                         err.setMessage("API deployment/configuration error - contact support");
92                                         err.setFields( "PROV_AUTH_ADDRESSES");
93                                         break;
94                                 
95                                 default: 
96                                         logger.info( DmaapbcLogMessageEnum.INGRESS_CREATE_ERROR, Integer.toString(rc),  pub.getPubId(), pub.getFeedId(), nodePattern);
97                                 }
98                         }
99
100                 }
101         }
102
103         public DR_Pub addDr_Pub( DR_Pub pub ) {
104                 ApiError err = new ApiError();
105                 if ( pub.getPubId() != null && ! pub.getPubId().isEmpty() ) {
106                         addIngressRoute( pub, err);
107                         if ( err.getCode() > 0 ) {
108                                 pub.setStatus(DmaapObject_Status.INVALID);
109                         }
110                         pub.setLastMod();
111                         dr_pubs.put( pub.getPubId(), pub );
112                         return pub;
113                 }
114                 else {
115                         return null;
116                 }
117         }
118                 
119         public DR_Pub updateDr_Pub( DR_Pub pub ) {
120                 if ( pub.getPubId().isEmpty()) {
121                         return null;
122                 }
123                 pub.setLastMod();
124                 dr_pubs.put( pub.getPubId(), pub );
125                 return pub;
126         }
127                 
128         public DR_Pub removeDr_Pub( String pubId, ApiError err ) {
129                 return removeDr_Pub( pubId, err, true );
130         }
131                 
132         
133         public DR_Pub removeDr_Pub( String pubId, ApiError err, boolean hitDR ) {
134                 DR_Pub pub =  dr_pubs.get( pubId );
135                 if ( pub == null ) {
136                         err.setCode(Status.NOT_FOUND.getStatusCode());
137                         err.setFields( "pubId");
138                         err.setMessage( "pubId " + pubId + " not found");
139                 } else {
140                         dr_pubs.remove(pubId);
141                         err.setCode(Status.OK.getStatusCode());
142                 }
143                 return pub;
144                                 
145         }       
146
147 }