09bf8fdbc886dad1325ec57d93d1a728b80727fb
[dmaap/dbcapi.git] / src / main / java / org / onap / dmaap / dbcapi / service / DR_SubService.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.onap.dmaap.dbcapi.aaf.client.DrProvConnection;
30 import org.onap.dmaap.dbcapi.aaf.database.DatabaseClass;
31 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
32 import org.onap.dmaap.dbcapi.logging.DmaapbcLogMessageEnum;
33 import org.onap.dmaap.dbcapi.model.ApiError;
34 import org.onap.dmaap.dbcapi.model.DR_Sub;
35
36 public class DR_SubService extends BaseLoggingClass {
37
38         private Map<String, DR_Sub> dr_subs = DatabaseClass.getDr_subs();
39         private DR_NodeService nodeService = new DR_NodeService();
40         private String provURL;
41         private static DrProvConnection prov;
42         
43         
44         public DR_SubService(  ) {
45                 logger.debug( "Entry: DR_SubService (with no args)" );
46
47         }       
48         public DR_SubService( String subURL ) {
49                 logger.debug( "Entry: DR_SubService " + subURL );
50                 provURL = subURL;
51         }
52         public Map<String, DR_Sub> getDR_Subs() {
53                 logger.debug( "enter getDR_Subs()");
54                 return dr_subs;
55         }
56                 
57         public List<DR_Sub> getAllDr_Subs() {
58                 logger.debug( "enter getAllDR_Subs()");
59                 return new ArrayList<DR_Sub>(dr_subs.values());
60         }
61         
62         public ArrayList<DR_Sub> getDr_SubsByFeedId( String pubId ) {
63                 ArrayList<DR_Sub> someSubs = new ArrayList<DR_Sub>();
64                 for( DR_Sub sub : dr_subs.values() ) {
65                         if ( pubId.equals(  sub.getFeedId()  )) {
66                                 someSubs.add( sub );
67                         }
68                 }
69                         
70                 return someSubs;
71         }
72         public DR_Sub getDr_Sub( String key, ApiError apiError ) {      
73                 logger.debug( "enter getDR_Sub()");
74                 DR_Sub sub = dr_subs.get( key );
75                 if ( sub == null ) {
76                         apiError.setCode(Status.NOT_FOUND.getStatusCode());
77                         apiError.setFields( "subId");
78                         apiError.setMessage("subId " + key + " not found");
79                 } else {
80                         apiError.setCode(200);
81                 }
82                 return sub;
83         }
84
85         public DR_Sub addDr_Sub( DR_Sub sub, ApiError apiError ) {
86                 logger.debug( "enter addDR_Subs()");
87                 prov = new DrProvConnection();
88                 prov.makeSubPostConnection( provURL );
89                 String resp = prov.doPostDr_Sub( sub, apiError );
90                 logger.debug( "resp=" + resp );
91
92                 DR_Sub snew = null;
93
94                 if ( resp != null ) {
95                         snew = new DR_Sub( resp );
96                         snew.setDcaeLocationName(sub.getDcaeLocationName());
97                         snew.setLastMod();
98                         addEgressRoute( snew, apiError );
99                         dr_subs.put( snew.getSubId(), snew );   
100                         apiError.setCode(200);
101                 } else {
102                         apiError.setCode(400);
103                 }
104                 
105                 return snew;
106         }
107
108         private void addEgressRoute( DR_Sub sub, ApiError err ) {
109                 
110                 String nodePattern = nodeService.getNodePatternAtLocation( sub.getDcaeLocationName(), false );
111                 if ( nodePattern != null && nodePattern.length() > 0 ) {
112                         logger.info( "creating egress rule: sub " + sub.getSubId() + " on feed " + sub.getFeedId() + " to " + nodePattern);
113                         prov.makeEgressConnection( sub.getSubId(),  nodePattern);
114                         int rc = prov.doXgressPost(err);
115                         logger.info( "rc=" + rc + " error code=" + err.getCode() );
116                         
117                         if ( rc != 200 ) {
118                                 switch( rc ) {
119                                 case 403:
120                                         logger.error( "Not authorized for DR egress API");
121                                         err.setCode(500);
122                                         err.setMessage("API deployment/configuration error - contact support");
123                                         err.setFields( "PROV_AUTH_ADDRESSES");
124                                         break;
125                                 
126                                 default: 
127                                         logger.info( DmaapbcLogMessageEnum.EGRESS_CREATE_ERROR, Integer.toString(rc),  sub.getSubId(), sub.getFeedId(), nodePattern);
128                                 }
129                         }
130
131                 }
132         }
133         
134         public DR_Sub updateDr_Sub( DR_Sub obj, ApiError apiError ) {
135                 logger.debug( "enter updateDR_Subs()");
136
137                 DrProvConnection prov = new DrProvConnection();
138                 prov.makeSubPutConnection( obj.getSubId() );
139                 String resp = prov.doPutDr_Sub( obj, apiError );
140                 logger.debug( "resp=" + resp );
141
142                 DR_Sub snew = null;
143
144                 if ( resp != null ) {
145                         snew = new DR_Sub( resp );
146                         snew.setDcaeLocationName(obj.getDcaeLocationName());
147                         snew.setLastMod();
148                         dr_subs.put( snew.getSubId(), snew );   
149                         apiError.setCode(200);
150                 } else if ( apiError.is2xx()) {
151                         apiError.setCode(400);
152                         apiError.setMessage("unexpected empty response from DR Prov");
153                 }
154                 
155                 return snew;
156         }
157                 
158         public void removeDr_Sub( String key, ApiError apiError ) {
159                 logger.debug( "enter removeDR_Subs()");
160                 DR_Sub sub = dr_subs.get( key );
161                 if ( sub == null ) {
162                         apiError.setCode(Status.NOT_FOUND.getStatusCode());
163                         apiError.setFields( "subId");
164                         apiError.setMessage("subId " + key + " not found");
165                 } else {        
166                         dr_subs.remove(key);
167                         apiError.setCode(200);
168                 }
169
170                 return;
171         }       
172
173 }