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.apache.log4j.Logger;
30 import org.onap.dmaap.dbcapi.client.DrProvConnection;
31 import org.onap.dmaap.dbcapi.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;
38 public class DR_PubService extends BaseLoggingClass{
40 private Map<String, DR_Pub> dr_pubs = DatabaseClass.getDr_pubs();
41 private DR_NodeService nodeService = new DR_NodeService();
42 private static DrProvConnection prov;
44 public DR_PubService() {
46 prov = new DrProvConnection();
49 public Map<String, DR_Pub> getDr_Pubs() {
53 public List<DR_Pub> getAllDr_Pubs() {
54 return new ArrayList<DR_Pub>(dr_pubs.values());
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() )) {
68 public DR_Pub getDr_Pub( String key, ApiError err ) {
69 DR_Pub pub = dr_pubs.get( key );
71 err.setCode(Status.NOT_FOUND.getStatusCode());
72 err.setFields( "pubId");
73 err.setMessage("DR_Pub with pubId = " + key + " not found");
75 err.setCode(Status.OK.getStatusCode());
80 private void addIngressRoute( DR_Pub pub, ApiError err ) {
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() );
92 logger.error( "Not authorized for DR ingress API");
94 err.setMessage("API deployment/configuration error - contact support");
95 err.setFields( "PROV_AUTH_ADDRESSES");
99 logger.info( DmaapbcLogMessageEnum.INGRESS_CREATE_ERROR, Integer.toString(rc), pub.getPubId(), pub.getFeedId(), nodePattern);
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);
114 dr_pubs.put( pub.getPubId(), pub );
122 public DR_Pub updateDr_Pub( DR_Pub pub ) {
123 if ( pub.getPubId().isEmpty()) {
127 dr_pubs.put( pub.getPubId(), pub );
131 public DR_Pub removeDr_Pub( String pubId, ApiError err ) {
133 DR_Pub pub = dr_pubs.get( pubId );
135 err.setCode(Status.NOT_FOUND.getStatusCode());
136 err.setFields( "pubId");
137 err.setMessage( "pubId " + pubId + " not found");
139 dr_pubs.remove(pubId);
140 err.setCode(Status.OK.getStatusCode());