Divide the MSB source codes into two repos
[msb/apigateway.git] / apiroute / apiroute-service / src / main / java / org / onap / msb / apiroute / wrapper / service / CustomRouteService.java
1 package org.onap.msb.apiroute.wrapper.service;
2
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;
13
14 import java.util.ArrayList;
15 import java.util.Calendar;
16 import java.util.List;
17
18
19 public class CustomRouteService {
20     private static final Logger LOGGER = LoggerFactory.getLogger(CustomRouteService.class);
21
22     private static final CustomRouteService instance = new CustomRouteService();
23     private IRouteDAO routeDAO = DAOFactory.getRouteDAO();
24
25     private CustomRouteService() {
26     }
27
28     public static CustomRouteService getInstance() {
29         return instance;
30     }
31
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!");
35         }
36         RouteInfo routeInfo = CustomRouteAdapter.toRouteInfo(customRouteInfo);
37         routeDAO.saveRoute(routeKey, routeInfo);
38     }
39
40     public long deleteCustomRouteService2Redis(String routeKey) throws Exception {
41         return routeDAO.deleteRoute(routeKey);
42     }
43
44     public long deleteMultiCustomRouteService2Redis(String routeKeyPattern) throws Exception {
45         return routeDAO.deleteMultiRoute(routeKeyPattern);
46     }
47
48     public CustomRouteInfo getCustomRouteInstance(String routeKey) throws Exception {
49         CustomRouteInfo customRouteInfo = null;
50         RouteInfo routeInfo = null;
51         routeInfo = routeDAO.queryRoute(routeKey);
52         if(routeInfo!=null) {
53             customRouteInfo = CustomRouteAdapter.fromRouteInfo(routeInfo);
54         }
55         return customRouteInfo;
56     }
57
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);
65             }
66         }
67         return customRouteList;
68     }
69
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);
75         }else{
76             throw new Exception("service to be updated is not exist! Update failed");
77         }
78     }
79
80 }
81
82 class CustomRouteAdapter {
83     public static RouteInfo toRouteInfo(CustomRouteInfo customRouteInfo) {
84         RouteInfo routeInfo = new RouteInfo();
85         routeInfo.setStatus(customRouteInfo.getStatus());
86
87
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());
105             nodeList.add(node);
106         }
107         spec.setNodes(nodeList.toArray(new Node[]{}));
108         routeInfo.setSpec(spec);
109
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);
117
118         return routeInfo;
119     }
120
121     public static CustomRouteInfo fromRouteInfo(RouteInfo routeInfo) {
122         CustomRouteInfo customRouteInfo = new CustomRouteInfo();
123         customRouteInfo.setStatus(routeInfo.getStatus());
124
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);
143         }
144         customRouteInfo.setServers(routeServerList.toArray(new RouteServer[]{}));
145
146         Metadata metadata = routeInfo.getMetadata();
147         customRouteInfo.setServiceName(metadata.getName());
148         customRouteInfo.setNamespace(metadata.getNamespace());
149
150         return customRouteInfo;
151     }
152 }