d6012a7c944cc2ef0825a766d22860788a0fbb79
[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.client.DrProvConnection;
30 import org.onap.dmaap.dbcapi.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 import org.onap.dmaap.dbcapi.util.DmaapConfig;
36 import org.onap.dmaap.dbcapi.util.RandomInteger;
37
38 public class DR_SubService extends BaseLoggingClass {
39
40         private Map<String, DR_Sub> dr_subs = DatabaseClass.getDr_subs();
41         private DR_NodeService nodeService = new DR_NodeService();
42         private String provURL;
43         private static DrProvConnection prov;
44         
45         private String unit_test;
46         
47         
48         public DR_SubService(  ) {
49                 logger.debug( "Entry: DR_SubService (with no args)" );
50                 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
51                 unit_test = p.getProperty( "UnitTest", "No" );
52         }       
53         public DR_SubService( String subURL ) {
54                 logger.debug( "Entry: DR_SubService " + subURL );
55                 provURL = subURL;
56                 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
57                 unit_test = p.getProperty( "UnitTest", "No" );
58         }
59         public Map<String, DR_Sub> getDR_Subs() {
60                 logger.debug( "enter getDR_Subs()");
61                 return dr_subs;
62         }
63                 
64         public List<DR_Sub> getAllDr_Subs() {
65                 logger.debug( "enter getAllDR_Subs()");
66                 return new ArrayList<DR_Sub>(dr_subs.values());
67         }
68         
69         public ArrayList<DR_Sub> getDr_SubsByFeedId( String pubId ) {
70                 ArrayList<DR_Sub> someSubs = new ArrayList<DR_Sub>();
71                 for( DR_Sub sub : dr_subs.values() ) {
72                         if ( pubId.equals(  sub.getFeedId()  )) {
73                                 someSubs.add( sub );
74                         }
75                 }
76                         
77                 return someSubs;
78         }
79         public DR_Sub getDr_Sub( String key, ApiError apiError ) {      
80                 logger.debug( "enter getDR_Sub()");
81                 DR_Sub sub = dr_subs.get( key );
82                 if ( sub == null ) {
83                         apiError.setCode(Status.NOT_FOUND.getStatusCode());
84                         apiError.setFields( "subId");
85                         apiError.setMessage("subId " + key + " not found");
86                 } else {
87                         apiError.setCode(200);
88                 }
89                 return sub;
90         }
91
92         public DR_Sub addDr_Sub( DR_Sub sub, ApiError apiError ) {
93                 logger.debug( "enter addDR_Subs()");
94                 prov = new DrProvConnection();
95                 prov.makeSubPostConnection( provURL );
96                 String resp = prov.doPostDr_Sub( sub, apiError );
97                 if ( unit_test.equals( "Yes" ) ) {
98                         resp = simulateResp( sub, "POST" );
99                         apiError.setCode(200);
100                 }
101                 logger.debug( "addDr_Sub resp=" + resp );
102
103                 DR_Sub snew = null;
104
105                 if ( resp != null ) {
106                         snew = new DR_Sub( resp );
107                         snew.setDcaeLocationName(sub.getDcaeLocationName());
108                         snew.setLastMod();
109                         addEgressRoute( snew, apiError );
110                         dr_subs.put( snew.getSubId(), snew );   
111                         apiError.setCode(200);
112                 } else {
113                         apiError.setCode(400);
114                 }
115                 
116                 return snew;
117         }
118
119         private void addEgressRoute( DR_Sub sub, ApiError err ) {
120                 
121                 String nodePattern = nodeService.getNodePatternAtLocation( sub.getDcaeLocationName(), false );
122                 if ( nodePattern != null && nodePattern.length() > 0 ) {
123                         logger.info( "creating egress rule: sub " + sub.getSubId() + " on feed " + sub.getFeedId() + " to " + nodePattern);
124                         prov.makeEgressConnection( sub.getSubId(),  nodePattern);
125                         int rc = prov.doXgressPost(err);
126                         logger.info( "rc=" + rc + " error code=" + err.getCode() );
127                         
128                         if ( rc != 200 ) {
129                                 switch( rc ) {
130                                 case 403:
131                                         logger.error( "Not authorized for DR egress API");
132                                         err.setCode(500);
133                                         err.setMessage("API deployment/configuration error - contact support");
134                                         err.setFields( "PROV_AUTH_ADDRESSES");
135                                         break;
136                                 
137                                 default: 
138                                         logger.info( DmaapbcLogMessageEnum.EGRESS_CREATE_ERROR, Integer.toString(rc),  sub.getSubId(), sub.getFeedId(), nodePattern);
139                                 }
140                         }
141
142                 }
143         }
144         
145         public DR_Sub updateDr_Sub( DR_Sub obj, ApiError apiError ) {
146                 logger.debug( "enter updateDR_Subs()");
147
148                 DrProvConnection prov = new DrProvConnection();
149                 prov.makeSubPutConnection( obj.getSubId() );
150                 String resp = prov.doPutDr_Sub( obj, apiError );
151                 if ( unit_test.equals( "Yes" ) ) {
152                         resp = simulateResp( obj, "PUT" );
153                         apiError.setCode(200);
154                 }
155                 logger.debug( "resp=" + resp );
156
157                 DR_Sub snew = null;
158
159                 if ( resp != null ) {
160                         snew = new DR_Sub( resp );
161                         snew.setDcaeLocationName(obj.getDcaeLocationName());
162                         snew.setLastMod();
163                         dr_subs.put( snew.getSubId(), snew );   
164                         apiError.setCode(200);
165                 } else if ( apiError.is2xx()) {
166                         apiError.setCode(400);
167                         apiError.setMessage("unexpected empty response from DR Prov");
168                 }
169                 
170                 return snew;
171         }
172                 
173         public void removeDr_Sub( String key, ApiError apiError ) {
174                 logger.debug( "enter removeDR_Subs()");
175                 
176                 DR_Sub sub = dr_subs.get( key );
177                 if ( sub == null ) {
178                         apiError.setCode(Status.NOT_FOUND.getStatusCode());
179                         apiError.setFields( "subId");
180                         apiError.setMessage("subId " + key + " not found");
181                 } else {        
182                         DrProvConnection prov = new DrProvConnection();
183                         prov.makeSubPutConnection( key );
184                         String resp = prov.doDeleteDr_Sub( sub, apiError );
185                         logger.debug( "resp=" + resp );
186                         
187                         if ( apiError.is2xx() || unit_test.equals( "Yes" ) ) {
188                                 dr_subs.remove(key);
189                         }
190                 }
191
192                 return;
193         }       
194
195         private String simulateResp( DR_Sub sub, String action ){
196                 String server = "subscriber.onap.org";
197                 String subid;
198                 if ( action.equals( "POST" ) ) { 
199                         RandomInteger ran = new RandomInteger(10000);
200                         subid = Integer.toString( ran.next() );
201                 } else if ( action.equals( "PUT" ) ) {
202                         subid = sub.getSubId();
203                 } else {
204                         subid = "99";
205                 }
206                 String ret = String.format("{\"suspend\": false, \"delivery\": {\"url\": \"https://%s/delivery/%s\", \"user\": \"%s\", \"password\": \"%s\", \"use100\":  true}, \"metadataOnly\": false, \"groupid\": \"0\" , \"follow_redirect\": true, ", 
207                         server, subid, sub.getUsername(), sub.getUserpwd());
208                 String links = String.format( "\"links\": {\"feed\": \"https://dr-prov/feedlog/%s\", \"self\": \"https://dr-prov/sub/%s\", \"log\": \"https://dr-prov/sublog/%s\" }", 
209                                 sub.getFeedId(),
210                                 sub.getSubId(),
211                                 sub.getSubId() );
212                 ret += links + "}";
213                 logger.info( "DR_SubService:simulateResp=" + ret);
214
215                 return ret;
216         }
217 }