0d1a63e505ddb9306444e715db50899e74d805dd
[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
26 import java.util.Collections;
27 import java.util.Map;
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.ArrayList;
31
32 import org.acumos.federation.gateway.config.EELFLoggerDelegate;
33 import org.acumos.federation.gateway.util.MapBuilder;
34 import org.acumos.federation.gateway.service.PeerService;
35 import org.acumos.federation.gateway.service.ServiceContext;
36 import org.acumos.federation.gateway.service.ServiceException;
37 import org.acumos.federation.gateway.cds.PeerStatus;
38
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.core.env.Environment;
41 import org.springframework.stereotype.Service;
42 import org.springframework.context.annotation.Conditional;
43
44 import org.acumos.cds.client.ICommonDataServiceRestClient;
45 import org.acumos.cds.domain.MLPPeer;
46 import org.acumos.cds.transport.RestPageResponse;
47 import org.acumos.cds.transport.RestPageRequest;
48
49 /**
50  * 
51  *
52  */
53 @Service
54 public class PeerServiceImpl extends AbstractServiceImpl implements PeerService {
55
56         private static final EELFLoggerDelegate log = EELFLoggerDelegate.getLogger(PeerServiceImpl.class.getName());
57
58         /**
59          * 
60          */
61         public PeerServiceImpl() {
62         }
63
64         @Override
65         public MLPPeer getSelf() {
66                 RestPageResponse<MLPPeer> response = 
67                         getClient().searchPeers(new MapBuilder().put("isSelf", Boolean.TRUE).build(), false, null);
68                 log.debug(EELFLoggerDelegate.errorLogger, "Peers representing 'self': " + response.getContent());
69                 if (response.getNumberOfElements() != 1) {
70                         log.warn(EELFLoggerDelegate.errorLogger, "Number of peers representing 'self' not 1. First page contains " + response.getNumberOfElements() + ".");
71                         return null;
72                 }
73                 return response.getContent().get(0);
74         }
75
76         /**
77          * ToDo:
78          */
79         @Override
80         public List<MLPPeer> getPeers(ServiceContext theContext) {
81                 log.debug(EELFLoggerDelegate.debugLogger, "getPeers");
82
83                 RestPageRequest pageRequest = new RestPageRequest(0, 100);
84                 RestPageResponse<MLPPeer> pageResponse = null;
85                 List<MLPPeer> peers = new ArrayList<MLPPeer>(),
86                                                                         pagePeers = null;
87                 ICommonDataServiceRestClient cdsClient = getClient();
88
89                 do {
90                         pageResponse = cdsClient.getPeers(pageRequest);
91                         peers.addAll(pageResponse.getContent());
92                 
93                         pageRequest.setPage(pageResponse.getNumber() + 1);
94                 }
95                 while (!pageResponse.isLast());
96
97                 return peers;
98         }
99
100         @Override
101         public List<MLPPeer> getPeerBySubjectName(String theSubjectName, ServiceContext theContext) {
102                 log.debug(EELFLoggerDelegate.debugLogger, "getPeerBySubjectName");
103                 RestPageResponse<MLPPeer> response = 
104                         getClient().searchPeers(new MapBuilder().put("subjectName", theSubjectName).build(), false, null);
105                 if (response.getSize() != 1) {
106                         log.warn(EELFLoggerDelegate.errorLogger, "getPeerBySubjectName returned more then one peer: {}", response.getSize());
107                 }
108                 return response.getContent();
109         }
110
111         @Override
112         public MLPPeer getPeerById(String thePeerId, ServiceContext theContext) {
113                 log.debug(EELFLoggerDelegate.debugLogger, "getPeerById: {}", thePeerId);
114                 MLPPeer mlpPeer = getClient().getPeer(thePeerId);
115                 if (mlpPeer != null) {
116                         log.error(EELFLoggerDelegate.debugLogger, "getPeerById: {}", mlpPeer.toString());
117                 }
118                 return mlpPeer;
119         }
120
121         @Override
122         public void registerPeer(MLPPeer thePeer) throws ServiceException {
123                 log.debug(EELFLoggerDelegate.debugLogger, "registerPeer");
124
125                 String subjectName = thePeer.getSubjectName();
126                 if (subjectName == null)
127                         throw new ServiceException("No subject name is available");
128
129                 ICommonDataServiceRestClient cdsClient = getClient();
130                 RestPageResponse<MLPPeer> response = 
131                         cdsClient.searchPeers(new MapBuilder().put("subjectName", subjectName).build(), false, null);
132
133                 if (response.getSize() != 0) {
134                         //if (response.getSize() == 1) { //should be the only alternative
135                         MLPPeer peer = response.getContent().get(0);
136                         PeerStatus status = PeerStatus.forCode(peer.getStatusCode());
137                         if (null == status) {
138                                 throw new ServiceException("Invalid peer status found: " + peer.getStatusCode());
139                         }
140
141                         if (status == PeerStatus.Requested) {
142                                 throw new ServiceException("Peer registration request is pending");
143                         }
144                         else if (status == PeerStatus.Active || status == PeerStatus.Inactive) {
145                                 log.info(EELFLoggerDelegate.applicationLogger, "registering an active/inactive peer: " + peer);
146                                 return;
147                         }
148                         else if (status == PeerStatus.Declined) {
149                                 throw new ServiceException("Peer registration request was declined");
150                         }
151                         else if (status == PeerStatus.Renounced) {
152                                 throw new ServiceException("Peer unregistration request is pending");
153                         }
154                         throw new ServiceException("Peer with subjectName '" + subjectName + "' already exists: " + peer);
155                 }
156
157                 log.error(EELFLoggerDelegate.debugLogger, "registerPeer: new peer with subjectName {}, create CDS record",
158                                 thePeer.getSubjectName());
159                 //enforce
160                 thePeer.setStatusCode(PeerStatus.Requested.code());
161
162                 try {
163                         cdsClient.createPeer(thePeer);
164                 }
165                 catch (Exception x) {
166                         throw new ServiceException("Failed to create peer");
167                 }
168         }
169
170         @Override
171         public void unregisterPeer(MLPPeer thePeer) throws ServiceException {
172                 log.debug(EELFLoggerDelegate.debugLogger, "unregisterPeer");
173
174                 String subjectName = thePeer.getSubjectName();
175                 if (subjectName == null)
176                         throw new ServiceException("No subject name is available");
177
178                 ICommonDataServiceRestClient cdsClient = getClient();
179                 RestPageResponse<MLPPeer> response = 
180                         cdsClient.searchPeers(new MapBuilder().put("subjectName", subjectName).build(), false, null);
181
182                 if (response.getSize() != 1) {
183                         throw new ServiceException("Search for peer with subjectName '" + subjectName + "' yielded invalid number of items: " + response);
184                 }
185
186                 MLPPeer peer = response.getContent().get(0);
187                 PeerStatus status = PeerStatus.forCode(peer.getStatusCode());
188                 if (null == status) {
189                         throw new ServiceException("Invalid peer status found: " + peer.getStatusCode());
190                 }
191
192                 if (status == PeerStatus.Requested) {
193                         throw new ServiceException("Peer registration request is pending");
194                         //can we simply delete the peer ??
195                 }
196                 else if (status == PeerStatus.Declined) {
197                         throw new ServiceException("Peer registration request was declined");
198                         //can we simply delete the peer ??
199                 }
200                 else if (status == PeerStatus.Renounced) {
201                         throw new ServiceException("Peer unregistration request is pending");
202                 }
203                 //active/inactive peers moved to renounced
204
205                 log.error(EELFLoggerDelegate.debugLogger, "unregisterPeer: peer with subjectName {}, update CDS record",
206                                 thePeer.getSubjectName());
207                 thePeer.setStatusCode(PeerStatus.Renounced.code());
208
209                 try {
210                         cdsClient.updatePeer(thePeer);
211                 }
212                 catch (Exception x) {
213                         throw new ServiceException("Failed to update peer", x);
214                 }
215         }
216
217 }