Refine access control, peer access
[federation.git] / gateway / src / main / java / org / acumos / federation / gateway / service / impl / PeerServiceImpl.java
1 /*-
2  * ===============LICENSE_START=======================================================
3  * Acumos
4  * ===================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property & Tech Mahindra. All rights reserved.
6  * ===================================================================================
7  * This Acumos software file is distributed by AT&T and Tech Mahindra
8  * under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *  
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *  
14  * This file 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=========================================================
19  */
20
21 /**
22  * 
23  */
24 package org.acumos.federation.gateway.service.impl;
25 import java.util.Collections;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Collections;
30
31 import org.acumos.federation.gateway.common.GatewayCondition;
32 import org.acumos.federation.gateway.config.EELFLoggerDelegate;
33 import org.acumos.federation.gateway.service.PeerService;
34 import org.acumos.federation.gateway.service.ServiceContext;
35
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.core.env.Environment;
38 import org.springframework.stereotype.Service;
39 import org.springframework.context.annotation.Conditional;
40
41 import org.acumos.cds.client.ICommonDataServiceRestClient;
42 import org.acumos.cds.domain.MLPPeer;
43
44 /**
45  * 
46  *
47  */
48 @Service
49 @Conditional(GatewayCondition.class)
50 public class PeerServiceImpl extends AbstractServiceImpl implements PeerService {
51
52         /**
53          * 
54          */
55         public PeerServiceImpl() {
56                 // TODO Auto-generated constructor stub
57         }
58
59         @Override
60         public List<MLPPeer> getPeers() {
61                 log.debug(EELFLoggerDelegate.debugLogger, "getPeers");
62                 ICommonDataServiceRestClient dataServiceRestClient = getClient();
63                 List<MLPPeer> mlpPeers = dataServiceRestClient.searchPeers(Collections.EMPTY_MAP, false);
64                 if(mlpPeers !=null) {
65                         log.debug(EELFLoggerDelegate.debugLogger, "getPeers size:{}", mlpPeers.size());
66                 }
67                 return mlpPeers;
68         }
69
70         @Override
71         public List<MLPPeer> getPeers(ServiceContext theContext) {
72                 log.debug(EELFLoggerDelegate.debugLogger, "getPeers(ServiceContext)");
73                 return getPeers();
74         }
75
76         @Override
77         public List<MLPPeer> getPeer(String subjectName) {
78                 log.debug(EELFLoggerDelegate.debugLogger, "savePeer");
79                 ICommonDataServiceRestClient dataServiceRestClient = getClient();
80                 Map<String, Object> queryParameters = new HashMap<String, Object>();
81                 queryParameters.put("subjectName", subjectName); //I believe it should be unique
82                 List<MLPPeer> existingMLPPeers = null;
83                 existingMLPPeers = dataServiceRestClient.searchPeers(queryParameters, false);
84                 if(existingMLPPeers != null && existingMLPPeers.size() > 0) {
85                         log.debug(EELFLoggerDelegate.debugLogger, "getPeer size:{}", existingMLPPeers.size());
86                 }
87                 return existingMLPPeers;
88         }
89         
90         @Override
91         public MLPPeer getOnePeer(String peerId) {
92                 log.debug(EELFLoggerDelegate.debugLogger, "getPeer: {}", peerId);
93                 ICommonDataServiceRestClient dataServiceRestClient = getClient();
94                 MLPPeer mlpPeer = dataServiceRestClient.getPeer(peerId);
95                 if(mlpPeer !=null) {
96                         log.error(EELFLoggerDelegate.debugLogger, "getOnePeer: {}", mlpPeer.toString());
97                 }
98                 return mlpPeer;
99         }
100         
101         @Override
102         public MLPPeer savePeer(MLPPeer mlpPeer) {
103                 log.debug(EELFLoggerDelegate.debugLogger, "savePeer");
104                 ICommonDataServiceRestClient dataServiceRestClient = getClient();
105                 Map<String, Object> queryParameters = new HashMap<String, Object>();
106                 queryParameters.put("subjectName", mlpPeer.getSubjectName()); //I believe it should be unique
107                 boolean isPeerExists = false;
108                 List<MLPPeer> existingMLPPeers = null;
109                 MLPPeer mlpPeerCreated = null;
110                 try{
111                         existingMLPPeers = getPeer(mlpPeer.getSubjectName());
112                         if(existingMLPPeers != null && existingMLPPeers.size() > 0) {
113                                 isPeerExists = true;
114                                 log.error(EELFLoggerDelegate.debugLogger, "savePeer");
115                         }
116                 } catch (Exception e) {
117                         isPeerExists = false;
118                         log.error(EELFLoggerDelegate.debugLogger, "savePeer: There is no existing MLPPeer for subjectName:{}, Create a record in DB", mlpPeer.getSubjectName());
119                 }
120                 
121                 if(!isPeerExists) {
122                         mlpPeerCreated = dataServiceRestClient.createPeer(mlpPeer);
123                         if(mlpPeerCreated !=null) {
124                                 log.debug(EELFLoggerDelegate.debugLogger, "savePeer :{}", mlpPeer.toString());
125                         }
126                 }
127                 return mlpPeerCreated;
128         }
129
130         @Override
131         public boolean updatePeer(MLPPeer mlpPeer) {
132                 log.debug(EELFLoggerDelegate.debugLogger, "updatePeer");
133                 ICommonDataServiceRestClient dataServiceRestClient = getClient();
134                 boolean isUpdatedSuccessfully = false;
135                 List<MLPPeer> existingMLPPeers = null;
136                 try{
137                         existingMLPPeers = getPeer(mlpPeer.getSubjectName());
138                         if(existingMLPPeers != null && existingMLPPeers.size() > 0) {
139                                 dataServiceRestClient.updatePeer(mlpPeer);
140                                 isUpdatedSuccessfully = true;
141                         }
142                 } catch (Exception e) {
143                         isUpdatedSuccessfully = false;
144                         log.error(EELFLoggerDelegate.debugLogger, "updatePeer: Exception while deleting the MLPPeer record:", e);
145                 }
146                 return isUpdatedSuccessfully;
147         }
148
149         @Override
150         public boolean deletePeer(MLPPeer mlpPeer) {
151                 log.debug(EELFLoggerDelegate.debugLogger, "deletePeer");
152                 boolean isDeletedSuccessfully = false;
153                 ICommonDataServiceRestClient dataServiceRestClient = getClient();
154                 try {
155                         dataServiceRestClient.deletePeer(mlpPeer.getPeerId());
156                         isDeletedSuccessfully = true;
157                 } catch (Exception e) {
158                         isDeletedSuccessfully = false;
159                         log.error(EELFLoggerDelegate.debugLogger, "deletePeer: Exception while deleting the MLPPeer record:", e);
160                 }
161                 return isDeletedSuccessfully;
162         }
163 }