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