2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.dmaap.dbcapi.service;
23 import java.util.ArrayList;
24 import java.util.List;
27 import javax.ws.rs.core.Response.Status;
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;
36 public class DR_SubService extends BaseLoggingClass {
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;
44 public DR_SubService( ) {
45 logger.debug( "Entry: DR_SubService (with no args)" );
48 public DR_SubService( String subURL ) {
49 logger.debug( "Entry: DR_SubService " + subURL );
52 public Map<String, DR_Sub> getDR_Subs() {
53 logger.debug( "enter getDR_Subs()");
57 public List<DR_Sub> getAllDr_Subs() {
58 logger.debug( "enter getAllDR_Subs()");
59 return new ArrayList<DR_Sub>(dr_subs.values());
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() )) {
72 public DR_Sub getDr_Sub( String key, ApiError apiError ) {
73 logger.debug( "enter getDR_Sub()");
74 DR_Sub sub = dr_subs.get( key );
76 apiError.setCode(Status.NOT_FOUND.getStatusCode());
77 apiError.setFields( "subId");
78 apiError.setMessage("subId " + key + " not found");
80 apiError.setCode(200);
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 );
95 snew = new DR_Sub( resp );
96 snew.setDcaeLocationName(sub.getDcaeLocationName());
98 addEgressRoute( snew, apiError );
99 dr_subs.put( snew.getSubId(), snew );
100 apiError.setCode(200);
102 apiError.setCode(400);
108 private void addEgressRoute( DR_Sub sub, ApiError err ) {
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() );
120 logger.error( "Not authorized for DR egress API");
122 err.setMessage("API deployment/configuration error - contact support");
123 err.setFields( "PROV_AUTH_ADDRESSES");
127 logger.info( DmaapbcLogMessageEnum.EGRESS_CREATE_ERROR, Integer.toString(rc), sub.getSubId(), sub.getFeedId(), nodePattern);
134 public DR_Sub updateDr_Sub( DR_Sub obj, ApiError apiError ) {
135 logger.debug( "enter updateDR_Subs()");
137 DrProvConnection prov = new DrProvConnection();
138 prov.makeSubPutConnection( obj.getSubId() );
139 String resp = prov.doPutDr_Sub( obj, apiError );
140 logger.debug( "resp=" + resp );
144 if ( resp != null ) {
145 snew = new DR_Sub( resp );
146 snew.setDcaeLocationName(obj.getDcaeLocationName());
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");
158 public void removeDr_Sub( String key, ApiError apiError ) {
159 logger.debug( "enter removeDR_Subs()");
160 DR_Sub sub = dr_subs.get( key );
162 apiError.setCode(Status.NOT_FOUND.getStatusCode());
163 apiError.setFields( "subId");
164 apiError.setMessage("subId " + key + " not found");
167 apiError.setCode(200);