05d08c8287fe8b3dc1731ec61222e1083e24d840
[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
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.core.env.Environment;
37 import org.springframework.stereotype.Service;
38 import org.springframework.context.annotation.Conditional;
39
40 import org.acumos.cds.client.ICommonDataServiceRestClient;
41 import org.acumos.cds.domain.MLPPeer;
42
43 /**
44  * 
45  *
46  */
47 @Service
48 @Conditional(GatewayCondition.class)
49 public class PeerServiceImpl extends AbstractServiceImpl implements PeerService {
50
51         /**
52          * 
53          */
54         public PeerServiceImpl() {
55                 // TODO Auto-generated constructor stub
56         }
57
58         @Override
59         public List<MLPPeer> getPeers() {
60                 log.debug(EELFLoggerDelegate.debugLogger, "getPeers");
61                 ICommonDataServiceRestClient dataServiceRestClient = getClient();
62                 List<MLPPeer> mlpPeers = dataServiceRestClient.searchPeers(Collections.EMPTY_MAP, false);
63                 if(mlpPeers !=null) {
64                         log.debug(EELFLoggerDelegate.debugLogger, "getPeers size:{}", mlpPeers.size());
65                 }
66                 return mlpPeers;
67         }
68
69         @Override
70         public List<MLPPeer> getPeer(String subjectName) {
71                 log.debug(EELFLoggerDelegate.debugLogger, "savePeer");
72                 ICommonDataServiceRestClient dataServiceRestClient = getClient();
73                 Map<String, Object> queryParameters = new HashMap<String, Object>();
74                 queryParameters.put("subjectName", subjectName); //I believe it should be unique
75                 List<MLPPeer> existingMLPPeers = null;
76                 existingMLPPeers = dataServiceRestClient.searchPeers(queryParameters, false);
77                 if(existingMLPPeers != null && existingMLPPeers.size() > 0) {
78                         log.debug(EELFLoggerDelegate.debugLogger, "getPeer size:{}", existingMLPPeers.size());
79                 }
80                 return existingMLPPeers;
81         }
82         
83         @Override
84         public MLPPeer getOnePeer(String peerId) {
85                 log.debug(EELFLoggerDelegate.debugLogger, "getPeer: {}", peerId);
86                 ICommonDataServiceRestClient dataServiceRestClient = getClient();
87                 MLPPeer mlpPeer = dataServiceRestClient.getPeer(peerId);
88                 if(mlpPeer !=null) {
89                         log.error(EELFLoggerDelegate.debugLogger, "getOnePeer: {}", mlpPeer.toString());
90                 }
91                 return mlpPeer;
92         }
93         
94         @Override
95         public MLPPeer savePeer(MLPPeer mlpPeer) {
96                 log.debug(EELFLoggerDelegate.debugLogger, "savePeer");
97                 ICommonDataServiceRestClient dataServiceRestClient = getClient();
98                 Map<String, Object> queryParameters = new HashMap<String, Object>();
99                 queryParameters.put("subjectName", mlpPeer.getSubjectName()); //I believe it should be unique
100                 boolean isPeerExists = false;
101                 List<MLPPeer> existingMLPPeers = null;
102                 MLPPeer mlpPeerCreated = null;
103                 try{
104                         existingMLPPeers = getPeer(mlpPeer.getSubjectName());
105                         if(existingMLPPeers != null && existingMLPPeers.size() > 0) {
106                                 isPeerExists = true;
107                                 log.error(EELFLoggerDelegate.debugLogger, "savePeer");
108                         }
109                 } catch (Exception e) {
110                         isPeerExists = false;
111                         log.error(EELFLoggerDelegate.debugLogger, "savePeer: There is no existing MLPPeer for subjectName:{}, Create a record in DB", mlpPeer.getSubjectName());
112                 }
113                 
114                 if(!isPeerExists) {
115                         mlpPeerCreated = dataServiceRestClient.createPeer(mlpPeer);
116                         if(mlpPeerCreated !=null) {
117                                 log.debug(EELFLoggerDelegate.debugLogger, "savePeer :{}", mlpPeer.toString());
118                         }
119                 }
120                 return mlpPeerCreated;
121         }
122
123         @Override
124         public boolean updatePeer(MLPPeer mlpPeer) {
125                 log.debug(EELFLoggerDelegate.debugLogger, "updatePeer");
126                 ICommonDataServiceRestClient dataServiceRestClient = getClient();
127                 boolean isUpdatedSuccessfully = false;
128                 List<MLPPeer> existingMLPPeers = null;
129                 try{
130                         existingMLPPeers = getPeer(mlpPeer.getSubjectName());
131                         if(existingMLPPeers != null && existingMLPPeers.size() > 0) {
132                                 dataServiceRestClient.updatePeer(mlpPeer);
133                                 isUpdatedSuccessfully = true;
134                         }
135                 } catch (Exception e) {
136                         isUpdatedSuccessfully = false;
137                         log.error(EELFLoggerDelegate.debugLogger, "updatePeer: Exception while deleting the MLPPeer record:", e);
138                 }
139                 return isUpdatedSuccessfully;
140         }
141
142         @Override
143         public boolean deletePeer(MLPPeer mlpPeer) {
144                 log.debug(EELFLoggerDelegate.debugLogger, "deletePeer");
145                 boolean isDeletedSuccessfully = false;
146                 ICommonDataServiceRestClient dataServiceRestClient = getClient();
147                 try {
148                         dataServiceRestClient.deletePeer(mlpPeer.getPeerId());
149                         isDeletedSuccessfully = true;
150                 } catch (Exception e) {
151                         isDeletedSuccessfully = false;
152                         log.error(EELFLoggerDelegate.debugLogger, "deletePeer: Exception while deleting the MLPPeer record:", e);
153                 }
154                 return isDeletedSuccessfully;
155         }
156 }