Deal the sonar issue.
[aai/esr-server.git] / esr-mgr / src / main / java / org / onap / aai / esr / externalservice / aai / ExternalSystemProxy.java
1 /**
2  * Copyright 2017 ZTE Corporation.
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.aai.esr.externalservice.aai;
17
18 import org.glassfish.jersey.client.ClientConfig;
19 import org.onap.aai.esr.common.MsbConfig;
20 import org.onap.aai.esr.entity.aai.EsrEmsDetail;
21 import org.onap.aai.esr.entity.aai.EsrThirdpartySdncDetail;
22 import org.onap.aai.esr.entity.aai.EsrVnfmDetail;
23 import org.onap.aai.esr.exception.ExtsysException;
24 import com.eclipsesource.jaxrs.consumer.ConsumerFactory;
25
26 public class ExternalSystemProxy {
27
28     public static boolean isTest = false;
29     private static IExternalSystem externalSystemproxy;
30     private static String transactionId = "9999";
31     private static String fromAppId = "esr-server";
32     private static String authorization = AaiCommon.getAuthenticationCredentials();
33     static {
34         ClientConfig config = new ClientConfig();
35         externalSystemproxy =
36                 ConsumerFactory.createConsumer(MsbConfig.getExternalSystemAddr(), config, IExternalSystem.class);
37     }
38
39     public static void registerVnfm(String vnfmId, EsrVnfmDetail esrVnfmDetail) throws ExtsysException {
40         if (!isTest){
41             ClientConfig config = new ClientConfig(new VnfmRegisterProvider());
42             IExternalSystem registerVnfmServiceproxy =
43                     ConsumerFactory.createConsumer(MsbConfig.getExternalSystemAddr(), config, IExternalSystem.class);
44             try {
45                 registerVnfmServiceproxy.registerVNFM(transactionId, fromAppId, authorization, vnfmId, esrVnfmDetail);
46             } catch (Exception e) {
47                 throw new ExtsysException("PUT VNFM to A&AI failed.", e);
48             }
49         }
50     }
51
52     public static String queryVnfmDetail(String vnfmId) throws ExtsysException {
53         if (isTest) {
54             String esrVnfmDetailStr = "{\"vnfm-id\":\"123456\",\"vim-id\":\"987654\","
55                     + "\"certificate-url\":\"http://ip:5000/v3\",\"esr-system-info-list\":{"
56                     + "\"esr-system-info\":[{\"esr-system-info-id\":\"qwerty\",\"system-name\":\"ONAP VNFM\","
57                     + "\"type\":\"vnfm\",\"vendor\":\"zte\",\"version\":\"v1\","
58                     + "\"service-url\":\"http://ip:8000\",\"user-name\":\"onap\","
59                     + "\"password\":\"987654\",\"system-type\":\"VNFM\"}]}}";
60             return esrVnfmDetailStr;
61         }
62         try {
63             return externalSystemproxy.queryVNFMDetail(transactionId, fromAppId, authorization, vnfmId);
64         } catch (Exception e) {
65             throw new ExtsysException("Query VNFM detail from A&AI failed.", e);
66         }
67     }
68
69     public static String queryVnfmList() throws ExtsysException {
70         if (isTest) {
71             String vnfmListStr = "{\"esr-vnfm\": [{\"vnfm-id\": \"123456\",\"vim-id\": \"987654\","
72                     + "\"certificate-url\": \"http://ip:5000/v3\",\"resource-version\": \"1\"}]}";
73             return vnfmListStr;
74         }
75         try {
76             return externalSystemproxy.queryVNFMList(transactionId, fromAppId, authorization);
77         } catch (Exception e) {
78             throw new ExtsysException("Query VNFM list from A&AI failed.", e);
79         }
80     }
81
82     public static void deleteVnfm(String vnfmId, String resourceVersion) throws ExtsysException {
83         if (!isTest) {
84             try {
85                 externalSystemproxy.deleteVNFM(transactionId, fromAppId, authorization, vnfmId, resourceVersion);
86             } catch (Exception e) {
87                 throw new ExtsysException("Delete VNFM from A&AI failed.", e);
88             }
89         }
90     }
91
92     public static void registerSdnc(String thirdpartySdncId, EsrThirdpartySdncDetail esrSdncDetail)
93             throws ExtsysException {
94         if (!isTest) {
95             ClientConfig config = new ClientConfig(new ThirdpartySdncRegisterProvider());
96             IExternalSystem registerSdncServiceproxy =
97                     ConsumerFactory.createConsumer(MsbConfig.getExternalSystemAddr(), config, IExternalSystem.class);
98             try {
99                 registerSdncServiceproxy.registerThirdpartySdnc(transactionId, fromAppId, authorization,
100                         thirdpartySdncId, esrSdncDetail);
101             } catch (Exception e) {
102                 throw new ExtsysException("PUT thirdparty SDNC to A&AI failed.", e);
103             }
104         }
105     }
106
107     public static String queryThirdpartySdncDetail(String thirdpartySdncId) throws ExtsysException {
108         if (isTest) {
109             String sdncDetail = "{\"thirdparty-sdnc-id\":\"123456\",\"location\":\"edge\","
110                     + "\"product-name\":\"thirdparty SDNC\",\"esr-system-info-list\":{\"esr-system-info\":"
111                     + "[{\"esr-system-info-id\":\"987654\",\"system-name\":\"SDNC_TEST\",\"type\":\"SDNC\","
112                     + "\"vendor\":\"zte\",\"version\":\"v1\",\"service-url\":\"http://ip:8000\","
113                     + "\"user-name\":\"nancy\",\"password\":\"123987\",\"system-type\":\"thirdparty_SDNC\","
114                     + "\"protocol\":\"protocol\"}]}}";
115             return sdncDetail;
116         }
117         try {
118             return externalSystemproxy.queryThirdpartySdncDetail(transactionId, fromAppId, authorization,
119                     thirdpartySdncId);
120         } catch (Exception e) {
121             throw new ExtsysException("Query thirdparty SDNC detail from A&AI failed.", e);
122         }
123     }
124
125     public static String querySdncList() throws ExtsysException {
126         if (isTest) {
127             String sdncList =
128                     "{\"esr-thirdparty-sdnc\": [{\"thirdparty-sdnc-id\": \"123456\",\"location\": \"edge\","
129                             + "\"product-name\": \"thirdparty SDNC\",\"resource-version\": \"1\"}]}";
130             return sdncList;
131         }
132         try {
133             return externalSystemproxy.queryThirdpartySdncList(transactionId, fromAppId, authorization);
134         } catch (Exception e) {
135             throw new ExtsysException("Query thirdparty SDNC list from A&AI failed.", e);
136         }
137     }
138
139     public static void deleteThirdpartySdnc(String sdncId, String resourceVersion) throws ExtsysException {
140         if (!isTest) {
141             try {
142                 externalSystemproxy.deleteThirdpartySdnc(transactionId, fromAppId, authorization, sdncId,
143                         resourceVersion);
144             } catch (Exception e) {
145                 throw new ExtsysException("Delete thirdparty SDNC from A&AI failed.", e);
146             }
147         }
148     }
149
150     public static void registerEms(String emsId, EsrEmsDetail emsDetail) throws ExtsysException {
151         if (!isTest) {
152             ClientConfig config = new ClientConfig(new EmsRegisterProvider());
153             IExternalSystem registerEmsServiceproxy =
154                     ConsumerFactory.createConsumer(MsbConfig.getExternalSystemAddr(), config, IExternalSystem.class);
155             try {
156                 registerEmsServiceproxy.registerEMS(transactionId, fromAppId, authorization, emsId, emsDetail);
157             } catch (Exception e) {
158                 throw new ExtsysException("PUT EMS to A&AI failed.", e);
159             }
160         }
161     }
162
163     public static String queryEmsDetail(String emsId) throws ExtsysException {
164         if (isTest) {
165             String emsDetailStr = "{\"ems-id\":\"123456\",\"esr-system-info-list\":{\"esr-system-info\":"
166                     + "[{\"esr-system-info-id\":\"234567\",\"system-name\":\"EMS_TEST\",\"type\":\"sftp\","
167                     + "\"vendor\":\"ZTE\",\"version\":\"V1\",\"user-name\":\"nancy\",\"password\":\"asdf\","
168                     + "\"system-type\":\"EMS_RESOUCE\",\"ip-address\":\"ip\",\"port\":\"5000\","
169                     + "\"passive\":true,\"remote-path\":\"/home/per\"},{\"esr-system-info-id\":\"345678\","
170                     + "\"system-name\":\"EMS_TEST\",\"type\":\"sftp\",\"vendor\":\"ZTE\",\"version\":\"V1\","
171                     + "\"user-name\":\"nancy\",\"password\":\"asdf\",\"system-type\":\"EMS_PERFORMANCE\","
172                     + "\"ip-address\":\"ip\",\"port\":\"5000\",\"passive\":true,"
173                     + "\"remote-path\":\"/home/per\"},{\"esr-system-info-id\":\"456789\","
174                     + "\"system-name\":\"EMS_TEST\",\"vendor\":\"ZTE\",\"version\":\"V1\","
175                     + "\"user-name\":\"nancy\",\"password\":\"987654\",\"system-type\":\"EMS_ALARM\","
176                     + "\"ip-address\":\"ip\",\"port\":\"5000\"}]}}";
177             return emsDetailStr;
178         }
179         try {
180             return externalSystemproxy.queryEMSDetail(transactionId, fromAppId, authorization, emsId);
181         } catch (Exception e) {
182             throw new ExtsysException("Query EMS detail from A&AI failed.", e);
183         }
184     }
185
186     public static String queryEmsList() throws ExtsysException {
187         if (isTest) {
188             return "{\"esr-ems\": [ {\"ems-id\": \"123456\",\"resource-version\": \"1\"}]}";
189         }
190         try {
191             return externalSystemproxy.queryEMSList(transactionId, fromAppId, authorization);
192         } catch (Exception e) {
193             throw new ExtsysException("Query EMS list from A&AI failed.", e);
194         }
195     }
196
197     public static void deleteEms(String emsId, String resourceVersion) throws ExtsysException {
198         if (!isTest) {
199             try {
200                 externalSystemproxy.deleteEMS(transactionId, fromAppId, authorization, emsId, resourceVersion);
201             } catch (Exception e) {
202                 throw new ExtsysException("Delete EMS from A&AI failed.", e);
203             }
204         }
205     }
206 }