5fe6b66a95e714e3211779e45188bb0dbca6b57e
[dmaap/dbcapi.git] / 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 org.onap.dmaap.dbcapi.aaf.AafService.ServiceType;
26 import org.onap.dmaap.dbcapi.aaf.AafServiceImpl;
27 import org.onap.dmaap.dbcapi.client.MrProvConnection;
28 import org.onap.dmaap.dbcapi.database.DatabaseClass;
29 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
30 import org.onap.dmaap.dbcapi.model.ApiError;
31 import org.onap.dmaap.dbcapi.model.DcaeLocation;
32 import org.onap.dmaap.dbcapi.model.DmaapObject.DmaapObject_Status;
33 import org.onap.dmaap.dbcapi.model.MR_Client;
34 import org.onap.dmaap.dbcapi.model.MR_Cluster;
35 import org.onap.dmaap.dbcapi.model.Topic;
36 import org.onap.dmaap.dbcapi.util.DmaapConfig;
37
38 import javax.ws.rs.core.Response.Status;
39 import java.util.ArrayList;
40 import java.util.List;
41 import java.util.Map;
42
43 public class MR_ClientService extends BaseLoggingClass {
44
45     private static final String MR_CLIENT_ID = "mrClientId";
46     private int deleteLevel;
47     private Map<String, MR_Client> mr_clients = DatabaseClass.getMr_clients();
48     private Map<String, MR_Cluster> clusters = DatabaseClass.getMr_clusters();
49     private Map<String, DcaeLocation> locations = DatabaseClass.getDcaeLocations();
50     private DmaapService dmaap = new DmaapService();
51     private AafPermissionService aafPermissionService =
52             new AafPermissionService(new AafServiceImpl(ServiceType.AAF_TopicMgr), dmaap);
53     private String centralCname;
54
55     public MR_ClientService() {
56         DmaapConfig p = (DmaapConfig) DmaapConfig.getConfig();
57
58         centralCname = p.getProperty("MR.CentralCname", "MRcname.not.set");
59         deleteLevel = Integer.valueOf(p.getProperty("MR.ClientDeleteLevel", "0"));
60     }
61
62     public List<MR_Client> getAllMr_Clients() {
63         return new ArrayList<>(mr_clients.values());
64     }
65
66     List<MR_Client> getAllMrClients(String fqtn) {
67         ArrayList<MR_Client> results = new ArrayList<>();
68         for (Map.Entry<String, MR_Client> entry : mr_clients.entrySet()) {
69             MR_Client client = entry.getValue();
70             if (fqtn.equals(client.getFqtn())) {
71                 results.add(client);
72             }
73         }
74         return results;
75     }
76
77     List<MR_Client> getClientsByLocation(String location) {
78         List<MR_Client> results = new ArrayList<>();
79         for (Map.Entry<String, MR_Client> entry : mr_clients.entrySet()) {
80             MR_Client client = entry.getValue();
81             if (location.equals(client.getDcaeLocationName())) {
82                 results.add(client);
83             }
84         }
85         return results;
86     }
87
88
89     public MR_Client getMr_Client(String key, ApiError apiError) {
90         MR_Client c = mr_clients.get(key);
91         if (c == null) {
92             apiError.setCode(Status.NOT_FOUND.getStatusCode());
93             apiError.setFields(MR_CLIENT_ID);
94             apiError.setMessage(MR_CLIENT_ID + " " + key + " not found");
95         } else {
96             apiError.setCode(200);
97         }
98         return c;
99     }
100
101     public MR_Client addMr_Client(MR_Client client, Topic topic, ApiError err) {
102         if (client.getDcaeLocationName().isEmpty()) {
103             logger.info("Client  dcaeLocation that doesn't exist or not specified");
104             return null;
105         }
106         // original style: clients specified Role.  This has precedence for backwards
107         //                 compatibility.
108         // ONAP style: clients specify Identity to be assigned to generated Role
109         String role = client.getClientRole();
110         if (role != null) {
111             updateApiError(err, aafPermissionService.grantClientRolePerms(client));
112         } else if (client.hasClientIdentity()) {
113             if (client.isSubscriber()) {
114                 role = topic.getSubscriberRole();
115                 updateApiError(err, aafPermissionService.assignClientToRole(client, role));
116             }
117             if (client.isPublisher()) {
118                 role = topic.getPublisherRole();
119                 updateApiError(err, aafPermissionService.assignClientToRole(client, role));
120             }
121         }
122         if (!client.isStatusValid()) {
123             return null;
124         }
125         String centralFqdn = null;
126         DcaeLocation candidate = locations.get(client.getDcaeLocationName());
127
128         MR_Cluster cluster = clusters.get(client.getDcaeLocationName());
129         if (cluster != null && candidate != null) {
130             if (candidate.isCentral() && !topic.getReplicationCase().involvesFQDN()) {
131                 centralFqdn = centralCname;
132             }
133             client.setTopicURL(cluster.genTopicURL(centralFqdn, client.getFqtn()));
134             if (centralFqdn == null) {
135                 client.setStatus(addTopicToCluster(cluster, topic, err));
136                 if (!err.is2xx() && err.getCode() != 409) {
137                     topic.setFqtn(err.getMessage());
138                     return null;
139                 }
140
141             } else {
142                 MR_ClusterService clusters = new MR_ClusterService();
143                 //  MM should only exist for edge-to-central
144                 //  we use a cname for the central target (default resiliency with no replicationGroup set)
145                 // but still need to provision topics on all central MRs
146                 for (MR_Cluster central : clusters.getCentralClusters()) {
147                     client.setStatus(addTopicToCluster(central, topic, err));
148                     if (!err.is2xx() && err.getCode() != 409) {
149                         topic.setFqtn(err.getMessage());
150                         return null;
151                     }
152                 }
153             }
154
155         } else {
156             logger.warn("Client references a dcaeLocation that doesn't exist:" + client.getDcaeLocationName());
157             client.setStatus(DmaapObject_Status.STAGED);
158         }
159
160         mr_clients.put(client.getMrClientId(), client);
161
162         err.setCode(200);
163
164         return client;
165     }
166
167     private DmaapObject_Status addTopicToCluster(MR_Cluster cluster, Topic topic, ApiError err) {
168
169         MrProvConnection prov = new MrProvConnection();
170         logger.info("POST topic " + topic.getFqtn() + " to cluster " + cluster.getFqdn() + " in loc " + cluster.getDcaeLocationName());
171         if (prov.makeTopicConnection(cluster)) {
172             prov.doPostTopic(topic, err);
173             logger.info("response code: " + err.getCode());
174             if (err.is2xx() || err.getCode() == 409) {
175                 return DmaapObject_Status.VALID;
176             }
177         }
178         return DmaapObject_Status.INVALID;
179     }
180
181     public MR_Client updateMr_Client(MR_Client client, ApiError apiError) {
182         MR_Client c = mr_clients.get(client.getMrClientId());
183         if (c == null) {
184             apiError.setCode(Status.NOT_FOUND.getStatusCode());
185             apiError.setFields(MR_CLIENT_ID);
186             apiError.setMessage("mrClientId " + client.getMrClientId() + " not found");
187         } else {
188             apiError.setCode(200);
189         }
190         mr_clients.put(client.getMrClientId(), client);
191         return client;
192     }
193
194     public void removeMr_Client(String key, boolean updateTopicView, ApiError apiError) {
195         MR_Client client = mr_clients.get(key);
196         if (client == null) {
197             apiError.setCode(Status.NOT_FOUND.getStatusCode());
198             apiError.setFields(MR_CLIENT_ID);
199             apiError.setMessage("mrClientId " + key + " not found");
200             return;
201         } else {
202             apiError.setCode(200);
203         }
204
205         if (updateTopicView) {
206
207             TopicService topics = new TopicService();
208
209             Topic t = topics.getTopic(client.getFqtn(), apiError);
210             if (t != null) {
211                 List<MR_Client> tc = t.getClients();
212                 for (MR_Client c : tc) {
213                     if (c.getMrClientId().equals(client.getMrClientId())) {
214                         tc.remove(c);
215                         break;
216                     }
217                 }
218                 t.setClients(tc);
219                 topics.updateTopic(t, apiError);
220             }
221
222         }
223
224         // remove from AAF
225         if (deleteLevel >= 2) {
226             updateApiError(apiError, aafPermissionService.revokeClientPerms(client));
227             if (!apiError.is2xx()) {
228                 return;
229             }
230         }
231         // remove from DB
232         if (deleteLevel >= 1) {
233             mr_clients.remove(key);
234         }
235     }
236
237     private void updateApiError(ApiError err, ApiError permissionServiceError) {
238         err.setCode(permissionServiceError.getCode());
239         err.setMessage(permissionServiceError.getMessage());
240         err.setFields(permissionServiceError.getFields());
241     }
242 }