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
diff --git a/apiroute/apiroute-service/src/main/java/org/onap/msb/apiroute/wrapper/service/CustomRouteService.java b/apiroute/apiroute-service/src/main/java/org/onap/msb/apiroute/wrapper/service/CustomRouteService.java
new file mode 100644 (file)
index 0000000..6ea680e
--- /dev/null
@@ -0,0 +1,152 @@
+package org.onap.msb.apiroute.wrapper.service;
+
+import org.onap.msb.apiroute.api.CustomRouteInfo;
+import org.onap.msb.apiroute.api.RouteServer;
+import org.onap.msb.apiroute.wrapper.dao.DAOFactory;
+import org.onap.msb.apiroute.wrapper.dao.route.IRouteDAO;
+import org.onap.msb.apiroute.wrapper.dao.route.bean.Metadata;
+import org.onap.msb.apiroute.wrapper.dao.route.bean.Node;
+import org.onap.msb.apiroute.wrapper.dao.route.bean.RouteInfo;
+import org.onap.msb.apiroute.wrapper.dao.route.bean.Spec;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.ArrayList;
+import java.util.Calendar;
+import java.util.List;
+
+
+public class CustomRouteService {
+    private static final Logger LOGGER = LoggerFactory.getLogger(CustomRouteService.class);
+
+    private static final CustomRouteService instance = new CustomRouteService();
+    private IRouteDAO routeDAO = DAOFactory.getRouteDAO();
+
+    private CustomRouteService() {
+    }
+
+    public static CustomRouteService getInstance() {
+        return instance;
+    }
+
+    public void saveCustomRouteService2Redis(CustomRouteInfo customRouteInfo, String routeKey) throws Exception {
+        if(customRouteInfo ==null){
+            throw new Exception("input customRouteInfo to be saved is null!");
+        }
+        RouteInfo routeInfo = CustomRouteAdapter.toRouteInfo(customRouteInfo);
+        routeDAO.saveRoute(routeKey, routeInfo);
+    }
+
+    public long deleteCustomRouteService2Redis(String routeKey) throws Exception {
+        return routeDAO.deleteRoute(routeKey);
+    }
+
+    public long deleteMultiCustomRouteService2Redis(String routeKeyPattern) throws Exception {
+        return routeDAO.deleteMultiRoute(routeKeyPattern);
+    }
+
+    public CustomRouteInfo getCustomRouteInstance(String routeKey) throws Exception {
+        CustomRouteInfo customRouteInfo = null;
+        RouteInfo routeInfo = null;
+        routeInfo = routeDAO.queryRoute(routeKey);
+        if(routeInfo!=null) {
+            customRouteInfo = CustomRouteAdapter.fromRouteInfo(routeInfo);
+        }
+        return customRouteInfo;
+    }
+
+    public List<CustomRouteInfo> getMultiCustomRouteInstances(String customRedisKeyPattern) throws Exception {
+        List<CustomRouteInfo> customRouteList = new ArrayList<>();
+        List<RouteInfo> routeInfoList = routeDAO.queryMultiRoute(customRedisKeyPattern);
+        for (RouteInfo routeInfo : routeInfoList) {
+            if (routeInfo != null) {
+                CustomRouteInfo customRouteInfo = CustomRouteAdapter.fromRouteInfo(routeInfo);;
+                customRouteList.add(customRouteInfo);
+            }
+        }
+        return customRouteList;
+    }
+
+    public void updateCustomRouteStatus2Redis(String routeKey,String status) throws Exception {
+        RouteInfo routeInfo = routeDAO.queryRoute(routeKey);
+        if(routeInfo != null){
+            routeInfo.setStatus(status);
+            routeDAO.saveRoute(routeKey,routeInfo);
+        }else{
+            throw new Exception("service to be updated is not exist! Update failed");
+        }
+    }
+
+}
+
+class CustomRouteAdapter {
+    public static RouteInfo toRouteInfo(CustomRouteInfo customRouteInfo) {
+        RouteInfo routeInfo = new RouteInfo();
+        routeInfo.setStatus(customRouteInfo.getStatus());
+
+
+        Spec spec = new Spec();
+        spec.setVisualRange(customRouteInfo.getVisualRange());
+        spec.setUrl(customRouteInfo.getUrl().trim());
+        spec.setPublish_port(customRouteInfo.getPublish_port());
+        spec.setHost(customRouteInfo.getHost());
+        spec.setConsulServiceName(customRouteInfo.getConsulServiceName());
+        spec.setUseOwnUpstream(customRouteInfo.getUseOwnUpstream());
+        spec.setPublish_protocol(customRouteInfo.getPublishProtocol());
+        spec.setEnable_ssl(customRouteInfo.isEnable_ssl());
+        spec.setControl(customRouteInfo.getControl());
+        RouteServer[] routeServers = customRouteInfo.getServers();
+        List<Node> nodeList = new ArrayList<>();
+        for (RouteServer server: routeServers){
+            Node node = new Node();
+            node.setIp(server.getIp());
+            node.setPort(Integer.parseInt(server.getPort()));
+            node.setWeight(server.getWeight());
+            nodeList.add(node);
+        }
+        spec.setNodes(nodeList.toArray(new Node[]{}));
+        routeInfo.setSpec(spec);
+
+        Metadata metadata = new Metadata();
+        metadata.setName(customRouteInfo.getServiceName());
+        metadata.setNamespace(customRouteInfo.getNamespace());
+        Calendar now = Calendar.getInstance();
+        now.set(Calendar.MILLISECOND, 0);
+        metadata.setUpdateTimestamp(now.getTime());
+        routeInfo.setMetadata(metadata);
+
+        return routeInfo;
+    }
+
+    public static CustomRouteInfo fromRouteInfo(RouteInfo routeInfo) {
+        CustomRouteInfo customRouteInfo = new CustomRouteInfo();
+        customRouteInfo.setStatus(routeInfo.getStatus());
+
+        Spec spec = routeInfo.getSpec();
+        customRouteInfo.setVisualRange(spec.getVisualRange());
+        customRouteInfo.setUrl(spec.getUrl());
+        customRouteInfo.setPublish_port(spec.getPublish_port());
+        customRouteInfo.setHost(spec.getHost());
+        customRouteInfo.setConsulServiceName(spec.getConsulServiceName());
+        customRouteInfo.setUseOwnUpstream(spec.getUseOwnUpstream());
+        customRouteInfo.setPublishProtocol(spec.getPublish_protocol());
+        customRouteInfo.setEnable_ssl(spec.isEnable_ssl());
+        customRouteInfo.setControl(spec.getControl());
+        Node[] nodes = spec.getNodes();
+        List<RouteServer> routeServerList = new ArrayList<>();
+        for (Node node: nodes){
+            RouteServer routeServer = new RouteServer();
+            routeServer.setIp(node.getIp());
+            routeServer.setPort(String.valueOf(node.getPort()));
+            routeServer.setWeight(node.getWeight());
+            routeServerList.add(routeServer);
+        }
+        customRouteInfo.setServers(routeServerList.toArray(new RouteServer[]{}));
+
+        Metadata metadata = routeInfo.getMetadata();
+        customRouteInfo.setServiceName(metadata.getName());
+        customRouteInfo.setNamespace(metadata.getNamespace());
+
+        return customRouteInfo;
+    }
+}
\ No newline at end of file