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