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;
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;
35 public class DR_PubService extends BaseLoggingClass{
37 private Map<String, DR_Pub> dr_pubs = DatabaseClass.getDr_pubs();
38 private DR_NodeService nodeService = new DR_NodeService();
39 private static DrProvConnection prov;
41 public DR_PubService() {
43 prov = new DrProvConnection();
46 public Map<String, DR_Pub> getDr_Pubs() {
50 public List<DR_Pub> getAllDr_Pubs() {
51 return new ArrayList<DR_Pub>(dr_pubs.values());
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() )) {
65 public DR_Pub getDr_Pub( String key, ApiError err ) {
66 DR_Pub pub = dr_pubs.get( key );
68 err.setCode(Status.NOT_FOUND.getStatusCode());
69 err.setFields( "pubId");
70 err.setMessage("DR_Pub with pubId = " + key + " not found");
72 err.setCode(Status.OK.getStatusCode());
77 private void addIngressRoute( DR_Pub pub, ApiError err ) {
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() );
89 logger.error( "Not authorized for DR ingress API");
91 err.setMessage("API deployment/configuration error - contact support");
92 err.setFields( "PROV_AUTH_ADDRESSES");
96 logger.info( DmaapbcLogMessageEnum.INGRESS_CREATE_ERROR, Integer.toString(rc), pub.getPubId(), pub.getFeedId(), nodePattern);
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);
111 dr_pubs.put( pub.getPubId(), pub );
119 public DR_Pub updateDr_Pub( DR_Pub pub ) {
120 if ( pub.getPubId().isEmpty()) {
124 dr_pubs.put( pub.getPubId(), pub );
128 public DR_Pub removeDr_Pub( String pubId, ApiError err ) {
129 return removeDr_Pub( pubId, err, true );
133 public DR_Pub removeDr_Pub( String pubId, ApiError err, boolean hitDR ) {
134 DR_Pub pub = dr_pubs.get( pubId );
136 err.setCode(Status.NOT_FOUND.getStatusCode());
137 err.setFields( "pubId");
138 err.setMessage( "pubId " + pubId + " not found");
140 dr_pubs.remove(pubId);
141 err.setCode(Status.OK.getStatusCode());