1 package org.onap.msb.apiroute.wrapper.service;
3 import org.onap.msb.apiroute.api.CustomRouteInfo;
4 import org.onap.msb.apiroute.api.RouteServer;
5 import org.onap.msb.apiroute.wrapper.dao.DAOFactory;
6 import org.onap.msb.apiroute.wrapper.dao.route.IRouteDAO;
7 import org.onap.msb.apiroute.wrapper.dao.route.bean.Metadata;
8 import org.onap.msb.apiroute.wrapper.dao.route.bean.Node;
9 import org.onap.msb.apiroute.wrapper.dao.route.bean.RouteInfo;
10 import org.onap.msb.apiroute.wrapper.dao.route.bean.Spec;
11 import org.slf4j.Logger;
12 import org.slf4j.LoggerFactory;
14 import java.util.ArrayList;
15 import java.util.Calendar;
16 import java.util.List;
19 public class CustomRouteService {
20 private static final Logger LOGGER = LoggerFactory.getLogger(CustomRouteService.class);
22 private static final CustomRouteService instance = new CustomRouteService();
23 private IRouteDAO routeDAO = DAOFactory.getRouteDAO();
25 private CustomRouteService() {
28 public static CustomRouteService getInstance() {
32 public void saveCustomRouteService2Redis(CustomRouteInfo customRouteInfo, String routeKey) throws Exception {
33 if(customRouteInfo ==null){
34 throw new Exception("input customRouteInfo to be saved is null!");
36 RouteInfo routeInfo = CustomRouteAdapter.toRouteInfo(customRouteInfo);
37 routeDAO.saveRoute(routeKey, routeInfo);
40 public long deleteCustomRouteService2Redis(String routeKey) throws Exception {
41 return routeDAO.deleteRoute(routeKey);
44 public long deleteMultiCustomRouteService2Redis(String routeKeyPattern) throws Exception {
45 return routeDAO.deleteMultiRoute(routeKeyPattern);
48 public CustomRouteInfo getCustomRouteInstance(String routeKey) throws Exception {
49 CustomRouteInfo customRouteInfo = null;
50 RouteInfo routeInfo = null;
51 routeInfo = routeDAO.queryRoute(routeKey);
53 customRouteInfo = CustomRouteAdapter.fromRouteInfo(routeInfo);
55 return customRouteInfo;
58 public List<CustomRouteInfo> getMultiCustomRouteInstances(String customRedisKeyPattern) throws Exception {
59 List<CustomRouteInfo> customRouteList = new ArrayList<>();
60 List<RouteInfo> routeInfoList = routeDAO.queryMultiRoute(customRedisKeyPattern);
61 for (RouteInfo routeInfo : routeInfoList) {
62 if (routeInfo != null) {
63 CustomRouteInfo customRouteInfo = CustomRouteAdapter.fromRouteInfo(routeInfo);;
64 customRouteList.add(customRouteInfo);
67 return customRouteList;
70 public void updateCustomRouteStatus2Redis(String routeKey,String status) throws Exception {
71 RouteInfo routeInfo = routeDAO.queryRoute(routeKey);
72 if(routeInfo != null){
73 routeInfo.setStatus(status);
74 routeDAO.saveRoute(routeKey,routeInfo);
76 throw new Exception("service to be updated is not exist! Update failed");
82 class CustomRouteAdapter {
83 public static RouteInfo toRouteInfo(CustomRouteInfo customRouteInfo) {
84 RouteInfo routeInfo = new RouteInfo();
85 routeInfo.setStatus(customRouteInfo.getStatus());
88 Spec spec = new Spec();
89 spec.setVisualRange(customRouteInfo.getVisualRange());
90 spec.setUrl(customRouteInfo.getUrl().trim());
91 spec.setPublish_port(customRouteInfo.getPublish_port());
92 spec.setHost(customRouteInfo.getHost());
93 spec.setConsulServiceName(customRouteInfo.getConsulServiceName());
94 spec.setUseOwnUpstream(customRouteInfo.getUseOwnUpstream());
95 spec.setPublish_protocol(customRouteInfo.getPublishProtocol());
96 spec.setEnable_ssl(customRouteInfo.isEnable_ssl());
97 spec.setControl(customRouteInfo.getControl());
98 RouteServer[] routeServers = customRouteInfo.getServers();
99 List<Node> nodeList = new ArrayList<>();
100 for (RouteServer server: routeServers){
101 Node node = new Node();
102 node.setIp(server.getIp());
103 node.setPort(Integer.parseInt(server.getPort()));
104 node.setWeight(server.getWeight());
107 spec.setNodes(nodeList.toArray(new Node[]{}));
108 routeInfo.setSpec(spec);
110 Metadata metadata = new Metadata();
111 metadata.setName(customRouteInfo.getServiceName());
112 metadata.setNamespace(customRouteInfo.getNamespace());
113 Calendar now = Calendar.getInstance();
114 now.set(Calendar.MILLISECOND, 0);
115 metadata.setUpdateTimestamp(now.getTime());
116 routeInfo.setMetadata(metadata);
121 public static CustomRouteInfo fromRouteInfo(RouteInfo routeInfo) {
122 CustomRouteInfo customRouteInfo = new CustomRouteInfo();
123 customRouteInfo.setStatus(routeInfo.getStatus());
125 Spec spec = routeInfo.getSpec();
126 customRouteInfo.setVisualRange(spec.getVisualRange());
127 customRouteInfo.setUrl(spec.getUrl());
128 customRouteInfo.setPublish_port(spec.getPublish_port());
129 customRouteInfo.setHost(spec.getHost());
130 customRouteInfo.setConsulServiceName(spec.getConsulServiceName());
131 customRouteInfo.setUseOwnUpstream(spec.getUseOwnUpstream());
132 customRouteInfo.setPublishProtocol(spec.getPublish_protocol());
133 customRouteInfo.setEnable_ssl(spec.isEnable_ssl());
134 customRouteInfo.setControl(spec.getControl());
135 Node[] nodes = spec.getNodes();
136 List<RouteServer> routeServerList = new ArrayList<>();
137 for (Node node: nodes){
138 RouteServer routeServer = new RouteServer();
139 routeServer.setIp(node.getIp());
140 routeServer.setPort(String.valueOf(node.getPort()));
141 routeServer.setWeight(node.getWeight());
142 routeServerList.add(routeServer);
144 customRouteInfo.setServers(routeServerList.toArray(new RouteServer[]{}));
146 Metadata metadata = routeInfo.getMetadata();
147 customRouteInfo.setServiceName(metadata.getName());
148 customRouteInfo.setNamespace(metadata.getNamespace());
150 return customRouteInfo;