4106ce7c5ce4d840d2801b9fff28b48867c2b2c3
[msb/apigateway.git] /
1 /*******************************************************************************
2  * Copyright 2016-2017 ZTE, Inc. and others.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  ******************************************************************************/
16 package org.onap.msb.apiroute.wrapper.service;
17
18 import org.onap.msb.apiroute.api.CustomRouteInfo;
19 import org.onap.msb.apiroute.api.RouteServer;
20 import org.onap.msb.apiroute.wrapper.dao.DAOFactory;
21 import org.onap.msb.apiroute.wrapper.dao.route.IRouteDAO;
22 import org.onap.msb.apiroute.wrapper.dao.route.bean.Metadata;
23 import org.onap.msb.apiroute.wrapper.dao.route.bean.Node;
24 import org.onap.msb.apiroute.wrapper.dao.route.bean.RouteInfo;
25 import org.onap.msb.apiroute.wrapper.dao.route.bean.Spec;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 import java.util.ArrayList;
30 import java.util.Calendar;
31 import java.util.List;
32
33
34 public class CustomRouteService {
35     private static final Logger LOGGER = LoggerFactory.getLogger(CustomRouteService.class);
36
37     private static final CustomRouteService instance = new CustomRouteService();
38     private IRouteDAO routeDAO = DAOFactory.getRouteDAO();
39
40     private CustomRouteService() {
41     }
42
43     public static CustomRouteService getInstance() {
44         return instance;
45     }
46
47     public void saveCustomRouteService2Redis(CustomRouteInfo customRouteInfo, String routeKey) throws Exception {
48         if(customRouteInfo ==null){
49             throw new Exception("input customRouteInfo to be saved is null!");
50         }
51         RouteInfo routeInfo = CustomRouteAdapter.toRouteInfo(customRouteInfo);
52         routeDAO.saveRoute(routeKey, routeInfo);
53     }
54
55     public long deleteCustomRouteService2Redis(String routeKey) throws Exception {
56         return routeDAO.deleteRoute(routeKey);
57     }
58
59     public long deleteMultiCustomRouteService2Redis(String routeKeyPattern) throws Exception {
60         return routeDAO.deleteMultiRoute(routeKeyPattern);
61     }
62
63     public CustomRouteInfo getCustomRouteInstance(String routeKey) throws Exception {
64         CustomRouteInfo customRouteInfo = null;
65         RouteInfo routeInfo = null;
66         routeInfo = routeDAO.queryRoute(routeKey);
67         if(routeInfo!=null) {
68             customRouteInfo = CustomRouteAdapter.fromRouteInfo(routeInfo);
69         }
70         return customRouteInfo;
71     }
72
73     public List<CustomRouteInfo> getMultiCustomRouteInstances(String customRedisKeyPattern) throws Exception {
74         List<CustomRouteInfo> customRouteList = new ArrayList<>();
75         List<RouteInfo> routeInfoList = routeDAO.queryMultiRoute(customRedisKeyPattern);
76         for (RouteInfo routeInfo : routeInfoList) {
77             if (routeInfo != null) {
78                 CustomRouteInfo customRouteInfo = CustomRouteAdapter.fromRouteInfo(routeInfo);;
79                 customRouteList.add(customRouteInfo);
80             }
81         }
82         return customRouteList;
83     }
84
85     public void updateCustomRouteStatus2Redis(String routeKey,String status) throws Exception {
86         RouteInfo routeInfo = routeDAO.queryRoute(routeKey);
87         if(routeInfo != null){
88             routeInfo.setStatus(status);
89             routeDAO.saveRoute(routeKey,routeInfo);
90         }else{
91             throw new Exception("service to be updated is not exist! Update failed");
92         }
93     }
94
95 }
96
97 class CustomRouteAdapter {
98     public static RouteInfo toRouteInfo(CustomRouteInfo customRouteInfo) {
99         RouteInfo routeInfo = new RouteInfo();
100         routeInfo.setStatus(customRouteInfo.getStatus());
101
102
103         Spec spec = new Spec();
104         spec.setVisualRange(customRouteInfo.getVisualRange());
105         spec.setUrl(customRouteInfo.getUrl().trim());
106         spec.setPublish_port(customRouteInfo.getPublish_port());
107         spec.setHost(customRouteInfo.getHost());
108         spec.setConsulServiceName(customRouteInfo.getConsulServiceName());
109         spec.setUseOwnUpstream(customRouteInfo.getUseOwnUpstream());
110         spec.setPublish_protocol(customRouteInfo.getPublishProtocol());
111         spec.setEnable_ssl(customRouteInfo.isEnable_ssl());
112         spec.setControl(customRouteInfo.getControl());
113         RouteServer[] routeServers = customRouteInfo.getServers();
114         List<Node> nodeList = new ArrayList<>();
115         for (RouteServer server: routeServers){
116             Node node = new Node();
117             node.setIp(server.getIp());
118             node.setPort(Integer.parseInt(server.getPort()));
119             node.setWeight(server.getWeight());
120             nodeList.add(node);
121         }
122         spec.setNodes(nodeList.toArray(new Node[]{}));
123         routeInfo.setSpec(spec);
124
125         Metadata metadata = new Metadata();
126         metadata.setName(customRouteInfo.getServiceName());
127         metadata.setNamespace(customRouteInfo.getNamespace());
128         Calendar now = Calendar.getInstance();
129         now.set(Calendar.MILLISECOND, 0);
130         metadata.setUpdateTimestamp(now.getTime());
131         routeInfo.setMetadata(metadata);
132
133         return routeInfo;
134     }
135
136     public static CustomRouteInfo fromRouteInfo(RouteInfo routeInfo) {
137         CustomRouteInfo customRouteInfo = new CustomRouteInfo();
138         customRouteInfo.setStatus(routeInfo.getStatus());
139
140         Spec spec = routeInfo.getSpec();
141         customRouteInfo.setVisualRange(spec.getVisualRange());
142         customRouteInfo.setUrl(spec.getUrl());
143         customRouteInfo.setPublish_port(spec.getPublish_port());
144         customRouteInfo.setHost(spec.getHost());
145         customRouteInfo.setConsulServiceName(spec.getConsulServiceName());
146         customRouteInfo.setUseOwnUpstream(spec.getUseOwnUpstream());
147         customRouteInfo.setPublishProtocol(spec.getPublish_protocol());
148         customRouteInfo.setEnable_ssl(spec.isEnable_ssl());
149         customRouteInfo.setControl(spec.getControl());
150         Node[] nodes = spec.getNodes();
151         List<RouteServer> routeServerList = new ArrayList<>();
152         for (Node node: nodes){
153             RouteServer routeServer = new RouteServer();
154             routeServer.setIp(node.getIp());
155             routeServer.setPort(String.valueOf(node.getPort()));
156             routeServer.setWeight(node.getWeight());
157             routeServerList.add(routeServer);
158         }
159         customRouteInfo.setServers(routeServerList.toArray(new RouteServer[]{}));
160
161         Metadata metadata = routeInfo.getMetadata();
162         customRouteInfo.setServiceName(metadata.getName());
163         customRouteInfo.setNamespace(metadata.getNamespace());
164
165         return customRouteInfo;
166     }
167 }