1 package org.onap.msb.apiroute.wrapper.service;
3 import java.util.ArrayList;
4 import java.util.Calendar;
5 import java.util.HashSet;
8 import java.util.regex.Matcher;
9 import java.util.regex.Pattern;
11 import org.onap.msb.apiroute.api.MicroServiceFullInfo;
12 import org.onap.msb.apiroute.wrapper.dao.DAOFactory;
13 import org.onap.msb.apiroute.wrapper.dao.RedisAccessWrapper;
14 import org.onap.msb.apiroute.wrapper.dao.service.IServiceDAO;
15 import org.onap.msb.apiroute.wrapper.dao.service.bean.Metadata;
16 import org.onap.msb.apiroute.wrapper.dao.service.bean.ServiceInfo;
17 import org.onap.msb.apiroute.wrapper.dao.service.bean.Spec;
18 import org.onap.msb.apiroute.wrapper.util.MicroServiceUtil;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
22 import com.google.common.collect.ImmutableSet;
24 public class MicroServiceFullService {
25 private static final Logger LOGGER = LoggerFactory.getLogger(MicroServiceFullService.class);
27 private static MicroServiceFullService instance = new MicroServiceFullService();
29 private IServiceDAO serviceDAO = DAOFactory.getServiceDAO();
31 private MicroServiceFullService() {
34 public static MicroServiceFullService getInstance() {
38 public List<MicroServiceFullInfo> getAllMicroServiceInstances() throws Exception {
39 String serviceKeyPattern = MicroServiceUtil.getPrefixedKey("*");
41 List<MicroServiceFullInfo> microServiceFullInfoList = new ArrayList<>();
42 List<ServiceInfo> serviceInfoList = serviceDAO.queryMultiService(serviceKeyPattern);
43 for (ServiceInfo serviceInfo : serviceInfoList) {
44 if (serviceInfo != null) {
45 MicroServiceFullInfo microServiceFullInfo = MicroServiceFullAdapter.fromServiceInfo(serviceInfo);
47 microServiceFullInfoList.add(microServiceFullInfo);
50 return microServiceFullInfoList;
53 public Set<String> getAllMicroServiceKey() throws Exception {
54 final Set<String> builder = new HashSet<String>();
56 String serviceKeyPattern = MicroServiceUtil.getPrefixedKey("*");
57 Set<String> serviceKeySet = RedisAccessWrapper.filterKeys(serviceKeyPattern);
59 Pattern serviceKeyRegexPattern = MicroServiceUtil.getServiceKeyRegexPattern();
60 for (String serviceKey : serviceKeySet) {
61 Matcher matcher = serviceKeyRegexPattern.matcher(serviceKey);
62 if (matcher.matches()) {
63 builder.add(matcher.group("servicename"));
69 public void saveMicroServiceInfo2Redis(MicroServiceFullInfo microServiceFullInfo) throws Exception {
70 if(microServiceFullInfo ==null){
71 throw new Exception("input microServiceInfo to be saved is null!");
73 ServiceInfo serviceInfo = MicroServiceFullAdapter.toServiceInfo(microServiceFullInfo);
74 String serviceKey = MicroServiceUtil.getServiceKey(microServiceFullInfo.getServiceName(),microServiceFullInfo.getVersion());
75 serviceDAO.saveService(serviceKey,serviceInfo);
78 public void updateMicroServiceStatus(String serviceName, String version, String status)
80 if (null == version || "null".equals(version)) {
83 String serviceKey = MicroServiceUtil.getServiceKey(serviceName, version);
84 ServiceInfo serviceInfo = serviceDAO.queryService(serviceKey);
85 if(serviceInfo != null){
86 serviceInfo.setStatus(status);
87 serviceDAO.saveService(serviceKey,serviceInfo);
91 public boolean existsMicroServiceInstance(String serviceName, String version)
93 if (null == version || "null".equals(version)) {
96 String serviceKey = MicroServiceUtil.getServiceKey(serviceName, version);
97 return RedisAccessWrapper.isExist(serviceKey);
100 public MicroServiceFullInfo getMicroServiceInstance(String serviceName, String version)
102 if (null == version || "null".equals(version)) {
105 String serviceKey = MicroServiceUtil.getServiceKey(serviceName, version);
107 MicroServiceFullInfo microServiceInfo = null;
109 ServiceInfo serviceInfo = null;
110 serviceInfo = serviceDAO.queryService(serviceKey);
111 if(serviceInfo!=null) {
112 microServiceInfo = MicroServiceFullAdapter.fromServiceInfo(serviceInfo);
114 return microServiceInfo;
118 * query all the versions of the given ServiceName
123 public List<MicroServiceFullInfo> getAllVersionsOfTheService(String serviceName) throws Exception {
124 String serviceKeyPattern = MicroServiceUtil.getPrefixedKey(serviceName, "*");
126 List<MicroServiceFullInfo> microServiceFullInfoList = new ArrayList<>();
127 List<ServiceInfo> serviceInfoList = serviceDAO.queryMultiService(serviceKeyPattern);
128 for (ServiceInfo serviceInfo : serviceInfoList) {
129 if (serviceInfo != null) {
130 MicroServiceFullInfo microServiceFullInfo = MicroServiceFullAdapter.fromServiceInfo(serviceInfo);
131 microServiceFullInfoList.add(microServiceFullInfo);
134 return microServiceFullInfoList;
137 public void deleteMicroService(String serviceName, String version) throws Exception {
138 if (null == version || "null".equals(version)) {
141 String serviceKey = MicroServiceUtil.getServiceKey(serviceName, version);
142 serviceDAO.deleteService(serviceKey);
145 public long deleteMultiMicroService(String keyPattern) throws Exception {
146 return serviceDAO.deleteMultiService(keyPattern);
150 class MicroServiceFullAdapter {
151 public static ServiceInfo toServiceInfo(MicroServiceFullInfo microServiceFullInfo) {
152 ServiceInfo serviceInfo = new ServiceInfo();
153 serviceInfo.setApiVersion(microServiceFullInfo.getVersion());
154 serviceInfo.setStatus(microServiceFullInfo.getStatus());
157 Spec spec = new Spec();
158 spec.setVisualRange(microServiceFullInfo.getVisualRange());
159 spec.setUrl(microServiceFullInfo.getUrl());
160 spec.setPublish_port(microServiceFullInfo.getPublish_port());
161 spec.setHost(microServiceFullInfo.getHost());
162 spec.setProtocol(microServiceFullInfo.getProtocol());
163 spec.setLb_policy(microServiceFullInfo.getLb_policy());
164 spec.setEnable_ssl(microServiceFullInfo.isEnable_ssl());
165 Set<org.onap.msb.apiroute.api.Node> nodeSet = microServiceFullInfo.getNodes();
166 List<org.onap.msb.apiroute.wrapper.dao.service.bean.Node> serviceNodeList = new ArrayList<>();
167 for (org.onap.msb.apiroute.api.Node node : nodeSet) {
168 org.onap.msb.apiroute.wrapper.dao.service.bean.Node serviceNode = new org.onap.msb.apiroute.wrapper.dao.service.bean.Node();
169 serviceNode.setIp(node.getIp());
170 serviceNode.setPort(node.getPort());
171 serviceNode.setTtl(node.getTtl());
172 serviceNodeList.add(serviceNode);
174 spec.setNodes(serviceNodeList.toArray(new org.onap.msb.apiroute.wrapper.dao.service.bean.Node[]{}));
175 serviceInfo.setSpec(spec);
177 Metadata metadata = new Metadata();
178 metadata.setName(microServiceFullInfo.getServiceName());
179 metadata.setNamespace(microServiceFullInfo.getNamespace());
180 Calendar now = Calendar.getInstance();
181 now.set(Calendar.MILLISECOND, 0);
182 metadata.setUpdateTimestamp(now.getTime());
183 serviceInfo.setMetadata(metadata);
188 public static MicroServiceFullInfo fromServiceInfo(ServiceInfo serviceInfo) {
189 MicroServiceFullInfo microServiceFullInfo = new MicroServiceFullInfo();
191 microServiceFullInfo.setVersion(serviceInfo.getApiVersion());
192 microServiceFullInfo.setStatus(serviceInfo.getStatus());
194 Spec spec = serviceInfo.getSpec();
195 microServiceFullInfo.setVisualRange(spec.getVisualRange());
196 microServiceFullInfo.setUrl(spec.getUrl());
197 microServiceFullInfo.setPath(spec.getPath());
198 microServiceFullInfo.setPublish_port(spec.getPublish_port());
199 microServiceFullInfo.setHost(spec.getHost());
200 microServiceFullInfo.setProtocol(spec.getProtocol());
201 microServiceFullInfo.setLb_policy(spec.getLb_policy());
202 microServiceFullInfo.setEnable_ssl(spec.isEnable_ssl());
203 org.onap.msb.apiroute.wrapper.dao.service.bean.Node[] serviceNodes = spec.getNodes();
204 List<org.onap.msb.apiroute.api.Node> nodeList = new ArrayList<>();
205 for (org.onap.msb.apiroute.wrapper.dao.service.bean.Node serviceNode : serviceNodes) {
206 org.onap.msb.apiroute.api.Node node = new org.onap.msb.apiroute.api.Node();
207 node.setIp(serviceNode.getIp());
208 node.setPort(String.valueOf(serviceNode.getPort()));
209 node.setTtl(serviceNode.getTtl());
212 microServiceFullInfo.setNodes(new HashSet<org.onap.msb.apiroute.api.Node>(nodeList));
214 Metadata metadata = serviceInfo.getMetadata();
215 microServiceFullInfo.setServiceName(metadata.getName());
216 microServiceFullInfo.setNamespace(metadata.getNamespace());
218 return microServiceFullInfo;