Change the unit test about EMS with mock.
[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 void registerVnfm(String vnfmId, EsrVnfmDetail esrVnfmDetail) throws ExtsysException {
42         ClientConfig config = new ClientConfig(new VnfmRegisterProvider());
43         IExternalSystem registerVnfmServiceproxy =
44                 ConsumerFactory.createConsumer(MsbConfig.getExternalSystemAddr(), config, IExternalSystem.class);
45         try {
46             registerVnfmServiceproxy.registerVNFM(transactionId, fromAppId, authorization, vnfmId, esrVnfmDetail);
47         } catch (Exception e) {
48             throw new ExtsysException("PUT VNFM to A&AI failed.", e);
49         }
50     }
51
52     public String queryVnfmDetail(String vnfmId) throws ExtsysException {
53         try {
54             return externalSystemproxy.queryVNFMDetail(transactionId, fromAppId, authorization, vnfmId);
55         } catch (Exception e) {
56             throw new ExtsysException("Query VNFM detail from A&AI failed.", e);
57         }
58     }
59
60     public String queryVnfmList() throws ExtsysException {
61         try {
62             return externalSystemproxy.queryVNFMList(transactionId, fromAppId, authorization);
63         } catch (Exception e) {
64             throw new ExtsysException("Query VNFM list from A&AI failed.", e);
65         }
66     }
67
68     public void deleteVnfm(String vnfmId, String resourceVersion) throws ExtsysException {
69         try {
70             externalSystemproxy.deleteVNFM(transactionId, fromAppId, authorization, vnfmId, resourceVersion);
71         } catch (Exception e) {
72             throw new ExtsysException("Delete VNFM from A&AI failed.", e);
73         }
74     }
75
76     public void registerSdnc(String thirdpartySdncId, EsrThirdpartySdncDetail esrSdncDetail)
77             throws ExtsysException {
78         if (!test.getIsTest()) {
79             ClientConfig config = new ClientConfig(new ThirdpartySdncRegisterProvider());
80             IExternalSystem registerSdncServiceproxy =
81                     ConsumerFactory.createConsumer(MsbConfig.getExternalSystemAddr(), config, IExternalSystem.class);
82             try {
83                 registerSdncServiceproxy.registerThirdpartySdnc(transactionId, fromAppId, authorization,
84                         thirdpartySdncId, esrSdncDetail);
85             } catch (Exception e) {
86                 throw new ExtsysException("PUT thirdparty SDNC to A&AI failed.", e);
87             }
88         }
89     }
90
91     public String queryThirdpartySdncDetail(String thirdpartySdncId) throws ExtsysException {
92         if (test.getIsTest()) {
93             String sdncDetail = "{\"thirdparty-sdnc-id\":\"123456\",\"location\":\"edge\","
94                     + "\"product-name\":\"thirdparty SDNC\",\"esr-system-info-list\":{\"esr-system-info\":"
95                     + "[{\"esr-system-info-id\":\"987654\",\"system-name\":\"SDNC_TEST\",\"type\":\"SDNC\","
96                     + "\"vendor\":\"zte\",\"version\":\"v1\",\"service-url\":\"http://ip:8000\","
97                     + "\"user-name\":\"nancy\",\"password\":\"123987\",\"system-type\":\"thirdparty_SDNC\","
98                     + "\"protocol\":\"protocol\"}]}}";
99             return sdncDetail;
100         }
101         try {
102             return externalSystemproxy.queryThirdpartySdncDetail(transactionId, fromAppId, authorization,
103                     thirdpartySdncId);
104         } catch (Exception e) {
105             throw new ExtsysException("Query thirdparty SDNC detail from A&AI failed.", e);
106         }
107     }
108
109     public String querySdncList() throws ExtsysException {
110         if (test.getIsTest()) {
111             String sdncList =
112                     "{\"esr-thirdparty-sdnc\": [{\"thirdparty-sdnc-id\": \"123456\",\"location\": \"edge\","
113                             + "\"product-name\": \"thirdparty SDNC\",\"resource-version\": \"1\"}]}";
114             return sdncList;
115         }
116         try {
117             return externalSystemproxy.queryThirdpartySdncList(transactionId, fromAppId, authorization);
118         } catch (Exception e) {
119             throw new ExtsysException("Query thirdparty SDNC list from A&AI failed.", e);
120         }
121     }
122
123     public void deleteThirdpartySdnc(String sdncId, String resourceVersion) throws ExtsysException {
124         if (!test.getIsTest()) {
125             try {
126                 externalSystemproxy.deleteThirdpartySdnc(transactionId, fromAppId, authorization, sdncId,
127                         resourceVersion);
128             } catch (Exception e) {
129                 throw new ExtsysException("Delete thirdparty SDNC from A&AI failed.", e);
130             }
131         }
132     }
133
134     public void registerEms(String emsId, EsrEmsDetail emsDetail) throws ExtsysException {
135         ClientConfig config = new ClientConfig(new EmsRegisterProvider());
136         IExternalSystem registerEmsServiceproxy =
137                 ConsumerFactory.createConsumer(MsbConfig.getExternalSystemAddr(), config, IExternalSystem.class);
138         try {
139             registerEmsServiceproxy.registerEMS(transactionId, fromAppId, authorization, emsId, emsDetail);
140         } catch (Exception e) {
141             throw new ExtsysException("PUT EMS to A&AI failed.", e);
142         }
143     }
144
145     public String queryEmsDetail(String emsId) throws ExtsysException {
146         try {
147             return externalSystemproxy.queryEMSDetail(transactionId, fromAppId, authorization, emsId);
148         } catch (Exception e) {
149             throw new ExtsysException("Query EMS detail from A&AI failed.", e);
150         }
151     }
152
153     public String queryEmsList() throws ExtsysException {
154         try {
155             return externalSystemproxy.queryEMSList(transactionId, fromAppId, authorization);
156         } catch (Exception e) {
157             throw new ExtsysException("Query EMS list from A&AI failed.", e);
158         }
159     }
160
161     public void deleteEms(String emsId, String resourceVersion) throws ExtsysException {
162         try {
163             externalSystemproxy.deleteEMS(transactionId, fromAppId, authorization, emsId, resourceVersion);
164         } catch (Exception e) {
165             throw new ExtsysException("Delete EMS from A&AI failed.", e);
166         }
167     }
168 }