update link to upper-constraints.txt
[dmaap/buscontroller.git] / dmaap-bc / src / main / java / org / onap / dmaap / dbcapi / service / MR_ClientService.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.onap.dmaap
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  *
7  * Modifications Copyright (C) 2019 IBM.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.dmaap.dbcapi.service;
24
25 import jakarta.ws.rs.core.Response.Status;
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.Map;
29 import org.onap.dmaap.dbcapi.aaf.AafService.ServiceType;
30 import org.onap.dmaap.dbcapi.aaf.AafServiceFactory;
31 import org.onap.dmaap.dbcapi.client.MrProvConnection;
32 import org.onap.dmaap.dbcapi.database.DatabaseClass;
33 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
34 import org.onap.dmaap.dbcapi.model.ApiError;
35 import org.onap.dmaap.dbcapi.model.DcaeLocation;
36 import org.onap.dmaap.dbcapi.model.DmaapObject.DmaapObject_Status;
37 import org.onap.dmaap.dbcapi.model.MR_Client;
38 import org.onap.dmaap.dbcapi.model.MR_Cluster;
39 import org.onap.dmaap.dbcapi.model.Topic;
40 import org.onap.dmaap.dbcapi.util.DmaapConfig;
41
42 public class MR_ClientService extends BaseLoggingClass {
43
44     private static final String MR_CLIENT_ID = "mrClientId";
45     private int deleteLevel;
46     private Map<String, MR_Client> mr_clients = DatabaseClass.getMr_clients();
47     private Map<String, MR_Cluster> clusters = DatabaseClass.getMr_clusters();
48     private Map<String, DcaeLocation> locations = DatabaseClass.getDcaeLocations();
49     private DmaapService dmaap = new DmaapService();
50     private AafPermissionService aafPermissionService =
51             new AafPermissionService(new AafServiceFactory().initAafService(ServiceType.AAF_TopicMgr), dmaap);
52     private String centralCname;
53
54     public MR_ClientService() {
55         DmaapConfig p = (DmaapConfig) DmaapConfig.getConfig();
56
57         centralCname = p.getProperty("MR.CentralCname", "MRcname.not.set");
58         deleteLevel = Integer.valueOf(p.getProperty("MR.ClientDeleteLevel", "0"));
59     }
60
61     public List<MR_Client> getAllMr_Clients() {
62         return new ArrayList<>(mr_clients.values());
63     }
64
65     List<MR_Client> getAllMrClients(String fqtn) {
66         ArrayList<MR_Client> results = new ArrayList<>();
67         for (Map.Entry<String, MR_Client> entry : mr_clients.entrySet()) {
68             MR_Client client = entry.getValue();
69             if (fqtn.equals(client.getFqtn())) {
70                 results.add(client);
71             }
72         }
73         return results;
74     }
75
76     List<MR_Client> getClientsByLocation(String location) {
77         List<MR_Client> results = new ArrayList<>();
78         for (Map.Entry<String, MR_Client> entry : mr_clients.entrySet()) {
79             MR_Client client = entry.getValue();
80             if (location.equals(client.getDcaeLocationName())) {
81                 results.add(client);
82             }
83         }
84         return results;
85     }
86
87     public MR_Client getMr_Client(String key, ApiError apiError) {
88         MR_Client c = mr_clients.get(key);
89         if (c == null) {
90             apiError.setCode(Status.NOT_FOUND.getStatusCode());
91             apiError.setFields(MR_CLIENT_ID);
92             apiError.setMessage(MR_CLIENT_ID + " " + key + " not found");
93         } else {
94             apiError.setCode(200);
95         }
96         return c;
97     }
98
99     public MR_Client addMr_Client(MR_Client client, Topic topic, ApiError err) {
100         if (client.getDcaeLocationName().isEmpty()) {
101             logger.info("Client  dcaeLocation that doesn't exist or not specified");
102             return null;
103         }
104         // original style: clients specified Role.  This has precedence for backwards
105         //                 compatibility.
106         // ONAP style: clients specify Identity to be assigned to generated Role
107         String role = client.getClientRole();
108         if (role != null) {
109             updateApiError(err, aafPermissionService.grantClientRolePerms(client));
110         } else if (client.hasClientIdentity()) {
111             if (client.isSubscriber()) {
112                 role = topic.getSubscriberRole();
113                 updateApiError(err, aafPermissionService.assignClientToRole(client, role));
114             }
115             if (client.isPublisher()) {
116                 role = topic.getPublisherRole();
117                 updateApiError(err, aafPermissionService.assignClientToRole(client, role));
118             }
119         }
120         if (!client.isStatusValid()) {
121             return null;
122         }
123         String centralFqdn = null;
124         DcaeLocation candidate = locations.get(client.getDcaeLocationName());
125
126         MR_Cluster cluster = clusters.get(client.getDcaeLocationName());
127         if (cluster != null && candidate != null) {
128             if (candidate.isCentral() && !topic.getReplicationCase().involvesFQDN()) {
129                 centralFqdn = centralCname;
130             }
131             client.setTopicURL(cluster.genTopicURL(centralFqdn, client.getFqtn()));
132             if (centralFqdn == null) {
133                 client.setStatus(addTopicToCluster(cluster, topic, err));
134                 if (!err.is2xx() && err.getCode() != 409) {
135                     topic.setFqtn(err.getMessage());
136                     return null;
137                 }
138
139             } else {
140                 MR_ClusterService clusters = new MR_ClusterService();
141                 //  MM should only exist for edge-to-central
142                 //  we use a cname for the central target (default resiliency with no replicationGroup set)
143                 // but still need to provision topics on all central MRs
144                 for (MR_Cluster central : clusters.getCentralClusters()) {
145                     client.setStatus(addTopicToCluster(central, topic, err));
146                     if (!err.is2xx() && err.getCode() != 409) {
147                         topic.setFqtn(err.getMessage());
148                         return null;
149                     }
150                 }
151             }
152
153         } else {
154             logger.warn("Client references a dcaeLocation that doesn't exist:" + client.getDcaeLocationName());
155             client.setStatus(DmaapObject_Status.STAGED);
156         }
157
158         mr_clients.put(client.getMrClientId(), client);
159
160         err.setCode(200);
161
162         return client;
163     }
164
165     private DmaapObject_Status addTopicToCluster(MR_Cluster cluster, Topic topic, ApiError err) {
166
167         MrProvConnection prov = new MrProvConnection();
168         logger.info("POST topic " + topic.getFqtn() + " to cluster " + cluster.getFqdn() + " in loc " + cluster.getDcaeLocationName());
169         if (prov.makeTopicConnection(cluster)) {
170             prov.doPostTopic(topic, err);
171             logger.info("response code: " + err.getCode());
172             if (err.is2xx() || err.getCode() == 409) {
173                 return DmaapObject_Status.VALID;
174             }
175         }
176         return DmaapObject_Status.INVALID;
177     }
178
179     public MR_Client updateMr_Client(MR_Client client, ApiError apiError) {
180         MR_Client c = mr_clients.get(client.getMrClientId());
181         if (c == null) {
182             apiError.setCode(Status.NOT_FOUND.getStatusCode());
183             apiError.setFields(MR_CLIENT_ID);
184             apiError.setMessage("mrClientId " + client.getMrClientId() + " not found");
185         } else {
186             apiError.setCode(200);
187         }
188         mr_clients.put(client.getMrClientId(), client);
189         return client;
190     }
191
192     public void removeMr_Client(String key, boolean updateTopicView, ApiError apiError) {
193         MR_Client client = mr_clients.get(key);
194         if (client == null) {
195             apiError.setCode(Status.NOT_FOUND.getStatusCode());
196             apiError.setFields(MR_CLIENT_ID);
197             apiError.setMessage("mrClientId " + key + " not found");
198             return;
199         } else {
200             apiError.setCode(200);
201         }
202
203         if (updateTopicView) {
204
205             TopicService topics = new TopicService();
206
207             Topic t = topics.getTopic(client.getFqtn(), apiError);
208             if (t != null) {
209                 List<MR_Client> tc = t.getClients();
210                 for (MR_Client c : tc) {
211                     if (c.getMrClientId().equals(client.getMrClientId())) {
212                         tc.remove(c);
213                         break;
214                     }
215                 }
216                 t.setClients(tc);
217                 topics.updateTopic(t, apiError);
218             }
219
220         }
221
222         // remove from DB
223         if (deleteLevel >= 1) {
224             mr_clients.remove(key);
225         }
226     }
227
228     private void updateApiError(ApiError err, ApiError permissionServiceError) {
229         err.setCode(permissionServiceError.getCode());
230         err.setMessage(permissionServiceError.getMessage());
231         err.setFields(permissionServiceError.getFields());
232     }
233 }