diff options
7 files changed, 787 insertions, 29 deletions
diff --git a/holmes-actions/pom.xml b/holmes-actions/pom.xml index 66b462e..cf110c4 100644 --- a/holmes-actions/pom.xml +++ b/holmes-actions/pom.xml @@ -150,23 +150,6 @@ <groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
-
- <dependency>
- <groupId>net.sf.json-lib</groupId>
- <artifactId>json-lib</artifactId>
- <version>2.4</version>
- <classifier>jdk15</classifier>
- <exclusions>
- <exclusion>
- <groupId>commons-collections</groupId>
- <artifactId>commons-collections</artifactId>
- </exclusion>
- <exclusion>
- <groupId>commons-beanutils</groupId>
- <artifactId>commons-beanutils</artifactId>
- </exclusion>
- </exclusions>
- </dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiQuery.java b/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiQuery.java index a13c627..1aa8d75 100644 --- a/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiQuery.java +++ b/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiQuery.java @@ -79,20 +79,20 @@ public class AaiQuery { } private String getResourceLinksResponse(String vserverId, String vserverName) throws CorrelationException { - String url = getBaseUrl(getMsbSuffixAddr(AaiConfig.AAI_VM_ADDR) + "vserver-id:EQUALS:" + vserverId); + String url = getBaseUrl(getMsbSuffixAddr(AaiConfig.AaiConsts.AAI_VM_ADDR) + "vserver-id:EQUALS:" + vserverId); String response = getResponse(url); if ("".equals(response) || "{}".equals(response)) { - url = getBaseUrl(getMsbSuffixAddr(AaiConfig.AAI_VM_ADDR) + "vserver-name:EQUALS:" + vserverName); + url = getBaseUrl(getMsbSuffixAddr(AaiConfig.AaiConsts.AAI_VM_ADDR) + "vserver-name:EQUALS:" + vserverName); response = getResponse(url); } return response; } private String getVnfDataResponse(String vnfId, String vnfName) throws CorrelationException { - String url = getBaseUrl(getMsbSuffixAddr(AaiConfig.AAI_VNF_ADDR)+ "/" + vnfId); + String url = getBaseUrl(getMsbSuffixAddr(AaiConfig.AaiConsts.AAI_VNF_ADDR)+ "/" + vnfId); String response = getResponse(url); if ("".equals(response) || "{}".equals(response)) { - url = getBaseUrl(getMsbSuffixAddr(AaiConfig.AAI_VNF_ADDR) + "vnf-name=" + vnfName); + url = getBaseUrl(getMsbSuffixAddr(AaiConfig.AaiConsts.AAI_VNF_ADDR) + "vnf-name=" + vnfName); response = getResponse(url); } return response; diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiQuery4Ccvpn.java b/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiQuery4Ccvpn.java new file mode 100644 index 0000000..e49b289 --- /dev/null +++ b/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiQuery4Ccvpn.java @@ -0,0 +1,234 @@ +/** + * Copyright 2018 ZTE Corporation. + * <p> + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ + +package org.onap.holmes.common.aai; + + +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; +import lombok.extern.slf4j.Slf4j; +import org.onap.holmes.common.aai.config.AaiConfig; +import org.onap.holmes.common.config.MicroServiceConfig; +import org.onap.holmes.common.exception.CorrelationException; + +import javax.ws.rs.client.Client; +import javax.ws.rs.client.ClientBuilder; +import javax.ws.rs.client.Entity; +import javax.ws.rs.client.WebTarget; +import javax.ws.rs.core.MultivaluedHashMap; +import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.core.Response; +import java.util.HashMap; +import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +@Slf4j +public class AaiQuery4Ccvpn { + + private MultivaluedMap<String, Object> headers; + + public AaiQuery4Ccvpn() { + headers = new MultivaluedHashMap<>(); + headers.add("X-TransactionId", AaiConfig.X_TRANSACTION_ID); + headers.add("X-FromAppId", AaiConfig.X_FROMAPP_ID); + headers.add("Authorization", AaiConfig.getAuthenticationCredentials()); + headers.add("Accept", "application/json"); + } + + public String getLogicLink(String networkId, String pnfName, String ifName, String status) { + Map<String, String> params = new HashMap<>(); + params.put("networkId", networkId); + params.put("pnfName", pnfName); + params.put("ifName", ifName); + params.put("status", status); + Response response = get(getHostAddr(), getPath(AaiConfig.MsbConsts.AAI_LINK_UPDATE, params)); + if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) { + throw new RuntimeException("Failed to connect to AAI. Cause: " + + response.getStatusInfo().getReasonPhrase()); + } + + JSONObject linkInfo = getInfo(JSONObject.toJSONString(response.getEntity()), "p-interface", "logical-link"); + return linkInfo.getJSONObject("relationship-data").getString("relationship-value"); + } + + public JSONArray getServiceInstances(String networkId, String pnfName, String ifName, String status) { + try { + JSONObject vpnBindingInfo = getVpnBindingInfo(networkId, pnfName, ifName, status); + String vpnBindingId = vpnBindingInfo.getJSONObject("relationship-data").getString("relationship-value"); + JSONObject connectivityInfo = getConnectivityInfo(vpnBindingId); + String connectivityId = connectivityInfo.getJSONObject("relationship-data").getString("relationship-value"); + JSONObject serviceInstanceInfo = getServiceInstanceByConn(connectivityId); + String serviceInstancePath = serviceInstanceInfo.getString("related-link"); + serviceInstancePath = serviceInstancePath.substring(0, serviceInstancePath.lastIndexOf('/')); + + Response response = get(getHostAddr(), getPath(serviceInstancePath)); + if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) { + throw new RuntimeException("Failed to connect to AAI. Cause: " + + response.getStatusInfo().getReasonPhrase()); + } + return getInstances(JSONObject.toJSONString(response.getEntity())); + } catch (CorrelationException e) { + throw new RuntimeException(e.getMessage(), e); + } + } + + public void updateTerminalPointStatus(String networkId, String pnfName, String ifName, Map<String, Object> body) throws CorrelationException { + Map<String, String> params = new HashMap<>(); + params.put("networkId", networkId); + params.put("pnfName", pnfName); + params.put("ifName", ifName); + Response response = patch(getHostAddr(), getPath(AaiConfig.MsbConsts.AAI_TP_UPDATE, params), body); + if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) { + throw new CorrelationException("Failed to connecto to AAI. Cause: " + + response.getStatusInfo().getReasonPhrase()); + } + } + + public void updateLogicLinkStatus(String linkName, Map<String, Object> body) throws CorrelationException { + Response response = patch(getHostAddr(), getPath(AaiConfig.MsbConsts.AAI_TP_UPDATE, "linkName", linkName), body); + if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) { + throw new CorrelationException("Failed to connecto to AAI. Cause: " + + response.getStatusInfo().getReasonPhrase()); + } + } + + public JSONObject getVpnBindingInfo(String networkId, String pnfName, String ifName, String status) throws CorrelationException { + Map<String, String> params = new HashMap(); + params.put("networkId", networkId); + params.put("pnfName", pnfName); + params.put("ifName", ifName); + params.put("status", status); + Response response = get(getHostAddr(), getPath(AaiConfig.MsbConsts.AAI_VPN_ADDR, params)); + if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) { + throw new CorrelationException("Failed to connecto to AAI. Cause: " + + response.getStatusInfo().getReasonPhrase()); + } + return getInfo(JSONObject.toJSONString(response.getEntity()), "p-interface", "vpn-binding"); + } + + public JSONObject getConnectivityInfo(String vpnId) throws CorrelationException { + Response response = get(getHostAddr(), getPath(AaiConfig.MsbConsts.AAI_CONN_ADDR, "vpnId", vpnId)); + if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) { + throw new CorrelationException("Failed to connect to AAI. Cause: " + + response.getStatusInfo().getReasonPhrase()); + } + return getInfo(JSONObject.toJSONString(response.getEntity()), "vpn-binding", "connectivity"); + } + + public JSONObject getServiceInstanceByConn(String connectivityId) throws CorrelationException { + Response response = get(getHostAddr(), getPath(AaiConfig.MsbConsts.AAI_SERVICE_INSTANCE_ADDR_4_CCVPN, "connectivityId", connectivityId)); + if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) { + throw new CorrelationException("Failed to connect to AAI. Cause: " + + response.getStatusInfo().getReasonPhrase()); + } + return getInfo(JSONObject.toJSONString(response.getEntity()), "connectivity", "service-instance"); + } + + public JSONArray getServiceInstances(String globalCustomerId, String serviceType) throws CorrelationException { + Map<String, String> params = new HashMap(); + params.put("global-customer-id", globalCustomerId); + params.put("service-type", serviceType); + Response response = get(getHostAddr(), getPath(AaiConfig.MsbConsts.AAI_SERVICE_INSTANCES_ADDR_4_CCVPN, params)); + if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) { + throw new CorrelationException("Failed to connect to AAI. Cause: " + + response.getStatusInfo().getReasonPhrase()); + } + return getInstances(JSONObject.toJSONString(response.getEntity())); + } + + private String getPath(String urlTemplate, Map<String, String> pathParams) { + String url = urlTemplate; + for (String key : pathParams.keySet()) { + url = url.replaceAll("\\{" + key + "\\}", pathParams.get(key)); + } + return url; + } + + private String getPath(String urlTemplate, String paramName, String paramValue) { + return urlTemplate.replaceAll("\\{" + paramName + "\\}", paramValue); + } + + private String getPath(String serviceInstancePath) { + Pattern pattern = Pattern.compile("/aai/(v\\d+)/([A-Za-z0-9\\-]+[^/])(/*.*)"); + Matcher matcher = pattern.matcher(serviceInstancePath); + String ret = "/api"; + if (matcher.find()) { + ret += "/aai-" + matcher.group(2) + "/" + matcher.group(1) + matcher.group(3); + } + + return ret; + } + + public Response get(String host, String path) { + Client client = ClientBuilder.newClient(); + WebTarget target = client.target(host).path(path); + return target.request().headers(getAaiHeaders()).get(); + } + + public Response patch(String host, String path, Map<String, Object> body) { + Client client = ClientBuilder.newClient(); + WebTarget target = client.target(host).path(path); + return target.request().headers(getAaiHeaders()).method("PATCH", Entity.json(body)); + } + + private JSONObject getInfo(String response, String pField, String field) { + JSONArray results = extractJsonArray(JSONObject.parseObject(response), "results"); + JSONObject pInterface = extractJsonObject(results.getJSONObject(0), pField); + JSONObject relationshipList = extractJsonObject(pInterface, "relationship-list"); + JSONArray relationShip = extractJsonArray(relationshipList, "relationship"); + if (relationShip != null) { + for (int i = 0; i < relationShip.size(); ++i) { + final JSONObject object = relationShip.getJSONObject(i); + if (object.getString("related-to").equals(field)) { + return object; + } + } + } + return null; + } + + private JSONArray getInstances(String response) { + JSONArray results = extractJsonArray(JSONObject.parseObject(response), "results"); + JSONObject pInterface = extractJsonObject(results.getJSONObject(0), "service-subscription"); + JSONObject serviceInstances = extractJsonObject(pInterface, "service-instances"); + JSONArray instance = extractJsonArray(serviceInstances, "service-instance"); + return instance; + } + + private JSONObject extractJsonObject(JSONObject obj, String key) { + if (obj != null && key != null && obj.containsKey(key)) { + return obj.getJSONObject(key); + } + return null; + } + + private JSONArray extractJsonArray(JSONObject obj, String key) { + if (obj != null && key != null && obj.containsKey(key)) { + return obj.getJSONArray(key); + } + return null; + } + + private MultivaluedMap getAaiHeaders() { + return headers; + } + + private String getHostAddr() { + String[] msbInfo = MicroServiceConfig.getMsbServerAddrWithHttpPrefix().split(":"); + StringBuilder sb = new StringBuilder("http://"); + sb.append(msbInfo[0]).append(msbInfo[1]); + return sb.toString(); + } +} diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/aai/config/AaiConfig.java b/holmes-actions/src/main/java/org/onap/holmes/common/aai/config/AaiConfig.java index d0ddf85..dda9922 100644 --- a/holmes-actions/src/main/java/org/onap/holmes/common/aai/config/AaiConfig.java +++ b/holmes-actions/src/main/java/org/onap/holmes/common/aai/config/AaiConfig.java @@ -14,10 +14,8 @@ package org.onap.holmes.common.aai.config; public class AaiConfig { - - public static final String AAI_VNF_ADDR = "/aai/v11/network/generic-vnfs/generic-vnf"; - - public static final String AAI_VM_ADDR = "/aai/v11/search/nodes-query?search-node-type=vserver&filter="; + + private static final String AAI_API_VERSION = "v14"; public static final String X_TRANSACTION_ID = "9999"; @@ -32,5 +30,61 @@ public class AaiConfig { + AAI_AUTHENTICATION_PAASWORD; return "Basic " + java.util.Base64.getEncoder().encodeToString(usernameAndPassword.getBytes()); } + + public static class AaiConsts { + + private static final String AAI_PREF = "/aai/"; + + public static final String AAI_VNF_ADDR = AAI_PREF + AAI_API_VERSION + "/network/generic-vnfs/generic-vnf"; + + public static final String AAI_TP_UPDATE = AAI_PREF + AAI_API_VERSION + "/network/network-resources/network-resource/{networkId}/pnfs/pnf/{pnfName}/p-interfaces/p-interface/{ifName}"; + + public static final String AAI_LINK_QUERY = AAI_PREF + AAI_API_VERSION + "/network-resources/network-resource/{networkId}/pnfs/pnf/{pnfName}/p-interfaces?interface-name={ifName}&operational-status={status}"; + + public static final String AAI_LINK_UPDATE = AAI_PREF + AAI_API_VERSION + "/network/logical-links/logical-link/{linkName}"; + + public static final String AAI_TP_ADDR = AAI_PREF + AAI_API_VERSION + "/network/pnfs/pnf/{node-Id}/p-interfaces/p-interface/{tp-id}"; + + public static final String AAI_VPN_ADDR = AAI_PREF + AAI_API_VERSION + "/network/network-resources/network-resource/{networkId}/pnfs/pnf/{pnfName}/p-interfaces?interface-name={ifName}&operational-status={status}"; + + public static final String AAI_CONN_ADDR =AAI_PREF + AAI_API_VERSION + "/network/vpn-bindings?vpn-id={vpnId}"; + + public static final String AAI_SERVICE_INSTANCE_ADDR_4_CCVPN = AAI_PREF + AAI_API_VERSION + "/network/connectivities/connectivity/{connectivityId}"; + + public static final String AAI_SERVICE_INSTANCES_ADDR_4_CCVPN = AAI_PREF + AAI_API_VERSION + "/business/customers/customer/{global-customer-id}/service-subscriptions/service-subscription/{service-type}"; + + public static final String AAI_VM_ADDR = AAI_PREF + AAI_API_VERSION + "/search/nodes-query?search-node-type=vserver&filter="; + } + + + public static class MsbConsts { + + private static final String AAI_MSB_PREF = "/api"; + + private static final String AAI_NETWORK = "/aai-network/"; + + private static final String AAI_BUSINESS = "/aai-business/"; + + private static final String AAI_SEARCH = "/aai-search/"; + + public static final String AAI_VNF_ADDR = AAI_MSB_PREF + AAI_NETWORK + AAI_API_VERSION + "/generic-vnfs/generic-vnf"; + + public static final String AAI_TP_UPDATE = AAI_MSB_PREF + AAI_NETWORK + AAI_API_VERSION + "/network-resources/network-resource/{networkId}/pnfs/pnf/{pnfName}/p-interfaces/p-interface/{ifName}"; + + public static final String AAI_LINK_QUERY = AAI_MSB_PREF + AAI_NETWORK + AAI_API_VERSION + "/network-resources/network-resource/{networkId}/pnfs/pnf/{pnfName}/p-interfaces?interface-name={ifName}&operational-status={status}"; + + public static final String AAI_LINK_UPDATE = AAI_MSB_PREF + AAI_NETWORK + AAI_API_VERSION + "/logical-links/logical-link/{linkName}"; + public static final String AAI_TP_ADDR = AAI_MSB_PREF + AAI_NETWORK + AAI_API_VERSION + "/pnfs/pnf/{node-Id}/p-interfaces/p-interface/{tp-id}"; + + public static final String AAI_VPN_ADDR = AAI_MSB_PREF + AAI_NETWORK + AAI_API_VERSION + "/network-resources/network-resource/{networkId}/pnfs/pnf/{pnfName}/p-interfaces?interface-name={ifName}&operational-status={status}"; + + public static final String AAI_CONN_ADDR =AAI_MSB_PREF + AAI_NETWORK + AAI_API_VERSION + "/vpn-bindings?vpn-id={vpnId}"; + + public static final String AAI_SERVICE_INSTANCE_ADDR_4_CCVPN = AAI_MSB_PREF + AAI_NETWORK + AAI_API_VERSION + "/connectivities/connectivity/{connectivityId}"; + + public static final String AAI_SERVICE_INSTANCES_ADDR_4_CCVPN = AAI_MSB_PREF + AAI_BUSINESS + AAI_API_VERSION + "/customers/customer/{global-customer-id}/service-subscriptions/service-subscription/{service-type}"; + + public static final String AAI_VM_ADDR = AAI_MSB_PREF + AAI_SEARCH + AAI_API_VERSION + "/nodes-query?search-node-type=vserver&filter="; + } } diff --git a/holmes-actions/src/test/java/org/onap/holmes/common/aai/AaiQuery4CcvpnTest.java b/holmes-actions/src/test/java/org/onap/holmes/common/aai/AaiQuery4CcvpnTest.java new file mode 100644 index 0000000..51cba1c --- /dev/null +++ b/holmes-actions/src/test/java/org/onap/holmes/common/aai/AaiQuery4CcvpnTest.java @@ -0,0 +1,319 @@ +/** + * Copyright 2018 ZTE Corporation. + * <p> + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ + +package org.onap.holmes.common.aai; + +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; +import org.easymock.EasyMock; +import org.junit.*; +import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; +import org.onap.holmes.common.aai.config.AaiConfig; +import org.onap.holmes.common.exception.CorrelationException; +import org.powermock.api.easymock.PowerMock; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; +import org.powermock.reflect.Whitebox; + +import javax.ws.rs.client.*; +import javax.ws.rs.client.Invocation.Builder; +import javax.ws.rs.core.MultivaluedHashMap; +import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.core.Response; +import java.io.*; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.junit.Assert.assertThat; +import static org.onap.holmes.common.config.MicroServiceConfig.MSB_ADDR; + + +@RunWith(PowerMockRunner.class) +@PrepareForTest({ClientBuilder.class, Client.class, Builder.class, WebTarget.class, Response.class}) +public class AaiQuery4CcvpnTest { + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + private static JSONObject data; + + private static AaiQuery4Ccvpn aai = new AaiQuery4Ccvpn(); + + private static MultivaluedMap<String, Object> headers = new MultivaluedHashMap<>(); + private static Client client; + private static WebTarget webTarget; + private static Builder builder; + private static Response response; + + @BeforeClass + static public void beforeClass() { + System.setProperty(MSB_ADDR, "127.0.0.1:80"); + + File file = new File(AaiQuery4CcvpnTest.class.getClassLoader().getResource("./ccvpn.data.json").getFile()); + BufferedReader reader = null; + try { + reader = new BufferedReader(new FileReader(file)); + StringBuilder sb = new StringBuilder(); + reader.lines().forEach(l -> sb.append(l)); + data = JSONObject.parseObject(sb.toString()); + } catch (FileNotFoundException e) { + // Do nothing + } catch (IOException e) { + // Do nothing + } finally { + if (reader != null) { + try { + reader.close(); + } catch (IOException e) { + // Do nothing + } + } + } + + headers.add("X-TransactionId", AaiConfig.X_TRANSACTION_ID); + headers.add("X-FromAppId", AaiConfig.X_FROMAPP_ID); + headers.add("Authorization", AaiConfig.getAuthenticationCredentials()); + headers.add("Accept", "application/json"); + Whitebox.setInternalState(aai, "headers", headers); + } + + @Before + public void before() { + PowerMock.mockStatic(ClientBuilder.class); + client = PowerMock.createMock(Client.class); + webTarget = PowerMock.createMock(WebTarget.class); + builder = PowerMock.createMock(Builder.class); + response = PowerMock.createMock(Response.class); + } + + @After + public void after() { + PowerMock.resetAll(); + } + + @Test + public void test_getPath() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { + String path = "/aai/v14/business/customers/customer/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances?service-instance-id={servId}"; + + Method method = AaiQuery4Ccvpn.class.getDeclaredMethod("getPath", String.class); + method.setAccessible(true); + + String ret = (String) method.invoke(aai, path); + + assertThat(ret, equalTo("/api/aai-business/v14/customers/customer/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances?service-instance-id={servId}")); + + } + + @Test + public void test_getLogicLink_exception() { + mockGetMethod(); + EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.NOT_FOUND).times(2); + + thrown.expect(RuntimeException.class); + + PowerMock.replayAll(); + + String linkId = aai.getLogicLink("network-1", "pnf-1", "interface-1", "DOWN"); + + PowerMock.verifyAll(); + + assertThat(linkId, equalTo("logic-link-1")); + + } + + @Test + public void test_getLogicLink() { + mockGetMethod(); + EasyMock.expect(response.getEntity()).andReturn(data.getJSONObject("logic-link")); + EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK); + + PowerMock.replayAll(); + + String linkId = aai.getLogicLink("network-1", "pnf-1", "interface-1", "DOWN"); + + PowerMock.verifyAll(); + + assertThat(linkId, equalTo("logic-link-1")); + + } + + @Test + public void test_getServiceInstances_exception() { + mockGetMethod(); + EasyMock.expect(response.getEntity()).andReturn(data.getJSONObject("vpn-binding")); + EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK); + + mockGetMethod(); + EasyMock.expect(response.getEntity()).andReturn(data.getJSONObject("connectivity")); + EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK); + + mockGetMethod(); + EasyMock.expect(response.getEntity()).andReturn(data.getJSONObject("service-instance-by-connectivity")); + EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK); + + mockGetMethod(); + EasyMock.expect(response.getEntity()).andReturn(data.getJSONObject("service-instances-by-service-type")); + EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.NOT_FOUND).times(2); + + thrown.expect(RuntimeException.class); + + PowerMock.replayAll(); + + JSONArray instances = aai.getServiceInstances("network-1", "pnf-1", "interface-1", "DOWN"); + + PowerMock.verifyAll(); + + assertThat(instances, equalTo("logic-link-1")); + + } + + @Test + public void test_getServiceInstances() { + mockGetMethod(); + EasyMock.expect(response.getEntity()).andReturn(data.getJSONObject("vpn-binding")); + EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK); + + mockGetMethod(); + EasyMock.expect(response.getEntity()).andReturn(data.getJSONObject("connectivity")); + EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK); + + mockGetMethod(); + EasyMock.expect(response.getEntity()).andReturn(data.getJSONObject("service-instance-by-connectivity")); + EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK); + + mockGetMethod(); + EasyMock.expect(response.getEntity()).andReturn(data.getJSONObject("service-instances-by-service-type")); + EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK); + + PowerMock.replayAll(); + + JSONArray instances = aai.getServiceInstances("network-1", "pnf-1", "interface-1", "DOWN"); + + PowerMock.verifyAll(); + + assertThat(instances.getJSONObject(0).getString("service-instance-id"), equalTo("some id 1")); + assertThat(instances.getJSONObject(1).getString("service-instance-id"), equalTo("some id 2")); + assertThat(instances.getJSONObject(2).getString("service-instance-id"), equalTo("some id 3")); + } + + @Test + public void test_getServiceInstances_1() throws CorrelationException { + mockGetMethod(); + EasyMock.expect(response.getEntity()).andReturn(data.getJSONObject("service-instances-by-service-type")); + EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK); + + PowerMock.replayAll(); + + JSONArray instances = aai.getServiceInstances("custom-1", "service-type-1"); + + PowerMock.verifyAll(); + + assertThat(instances.getJSONObject(0).getString("service-instance-id"), equalTo("some id 1")); + assertThat(instances.getJSONObject(1).getString("service-instance-id"), equalTo("some id 2")); + assertThat(instances.getJSONObject(2).getString("service-instance-id"), equalTo("some id 3")); + } + + @Test + public void test_getServiceInstances_1_exception() throws CorrelationException { + mockGetMethod(); + EasyMock.expect(response.getEntity()).andReturn(data.getJSONObject("service-instances-by-service-type")); + EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.NOT_FOUND).times(2); + + thrown.expect(CorrelationException.class); + + PowerMock.replayAll(); + + JSONArray instances = aai.getServiceInstances("custom-1", "service-type-1"); + + PowerMock.verifyAll(); + + assertThat(instances.getJSONObject(0).getString("service-instance-id"), equalTo("some id 1")); + assertThat(instances.getJSONObject(1).getString("service-instance-id"), equalTo("some id 2")); + assertThat(instances.getJSONObject(2).getString("service-instance-id"), equalTo("some id 3")); + } + + @Test + public void test_updateTerminalPointStatus() throws CorrelationException { + mockPatchMethod(); + EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK); + + PowerMock.replayAll(); + + aai.updateTerminalPointStatus("network-1", "pnf-1", "if-1", new HashMap<>()); + + PowerMock.verifyAll(); + } + + @Test + public void test_updateTerminalPointStatus_exception() throws CorrelationException { + mockPatchMethod(); + EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.NOT_FOUND).times(2); + + thrown.expect(CorrelationException.class); + + PowerMock.replayAll(); + + aai.updateTerminalPointStatus("network-1", "pnf-1", "if-1", new HashMap<>()); + + PowerMock.verifyAll(); + } + + @Test + public void test_updateLogicLinkStatus() throws CorrelationException { + mockPatchMethod(); + EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK); + + PowerMock.replayAll(); + + aai.updateLogicLinkStatus("link-1", new HashMap<>()); + + PowerMock.verifyAll(); + } + + @Test + public void test_updateLogicLinkStatus_exception() throws CorrelationException { + mockPatchMethod(); + EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.NOT_FOUND).times(2); + + thrown.expect(CorrelationException.class); + + PowerMock.replayAll(); + + aai.updateLogicLinkStatus("link-1", new HashMap<>()); + + PowerMock.verifyAll(); + + } + + private void mockGetMethod() { + initCommonMock(); + EasyMock.expect(builder.get()).andReturn(response); + } + + private void mockPatchMethod() { + initCommonMock(); + EasyMock.expect(builder.method(EasyMock.anyObject(String.class), EasyMock.anyObject(Entity.class))).andReturn(response); + } + + private void initCommonMock() { + EasyMock.expect(ClientBuilder.newClient()).andReturn(client); + EasyMock.expect(client.target(EasyMock.anyObject(String.class))).andReturn(webTarget); + EasyMock.expect(webTarget.path(EasyMock.anyObject(String.class))).andReturn(webTarget); + EasyMock.expect(webTarget.request()).andReturn(builder); + EasyMock.expect(builder.headers(headers)).andReturn(builder); + } +}
\ No newline at end of file diff --git a/holmes-actions/src/test/java/org/onap/holmes/common/aai/config/AaiConfigTest.java b/holmes-actions/src/test/java/org/onap/holmes/common/aai/config/AaiConfigTest.java index 6cbc56e..1d3d2e9 100644 --- a/holmes-actions/src/test/java/org/onap/holmes/common/aai/config/AaiConfigTest.java +++ b/holmes-actions/src/test/java/org/onap/holmes/common/aai/config/AaiConfigTest.java @@ -24,12 +24,12 @@ public class AaiConfigTest { @Test public void testAaiConfig_get_static_fields() { - String aaiVnfAddr = "/aai/v11/network/generic-vnfs/generic-vnf"; - String aaiVmAddr = "/aai/v11/search/nodes-query?search-node-type=vserver&filter="; + String aaiVnfAddr = "/aai/v14/network/generic-vnfs/generic-vnf"; + String aaiVmAddr = "/aai/v14/search/nodes-query?search-node-type=vserver&filter="; String xTransactionId = "9999"; String xFromAppId = "jimmy-postman"; - assertThat(aaiVnfAddr, equalTo(AaiConfig.AAI_VNF_ADDR)); - assertThat(aaiVmAddr, equalTo(AaiConfig.AAI_VM_ADDR)); + assertThat(aaiVnfAddr, equalTo(AaiConfig.AaiConsts.AAI_VNF_ADDR)); + assertThat(aaiVmAddr, equalTo(AaiConfig.AaiConsts.AAI_VM_ADDR)); assertThat(xTransactionId, equalTo(AaiConfig.X_TRANSACTION_ID)); assertThat(xFromAppId, equalTo(AaiConfig.X_FROMAPP_ID)); } diff --git a/holmes-actions/src/test/resources/ccvpn.data.json b/holmes-actions/src/test/resources/ccvpn.data.json new file mode 100644 index 0000000..6632cc2 --- /dev/null +++ b/holmes-actions/src/test/resources/ccvpn.data.json @@ -0,0 +1,168 @@ +{ + "logic-link": { + "results": [ + { + "p-interface": { + "interface-name": "{ifName}", + "network-ref": "some ref", + "transparent": "some value", + "operational-status ": "{status}", + "speed-value": "some speed", + "relationship-list": { + "relationship": [ + { + "related-to": "logical-link", + "related-link": "url of logical-link", + "relationship-data": { + "relationship-key": "logical-link.link-name", + "relationship-value": "logic-link-1" + } + } + ] + } + } + } + ] + }, + "vpn-binding": { + "results": [ + { + "p-interface": { + "interface-name": "{ifName}", + "network-ref": "some ref", + "transparent": "some value", + "operational-status ": "{status}", + "speed-value": "some speed", + "relationship-list": { + "relationship": [ + { + "related-to": "vpn-binding", + "related-link": "url of vpn-binding", + "relationship-data": { + "relationship-key": "vpn-binding.vpn-id", + "relationship-value": "some id" + } + } + ] + } + } + } + ] + }, + "connectivity": { + "results": [ + { + "vpn-binding": { + "vpn-id": "{vpnId}", + "vpn-name": "some name", + "access-provider-id": "provider id", + "access-client-id": "client id", + "access-topology-id": "topology id", + "src-access-node-id": "src node id", + "src-access-ltp-id": "src ltp id", + "dst-access-node-id": "dst node id", + "dst-access-ltp-id": "dst ltp id", + "operational-status": "some status", + "relationship-list": { + "relationship": [ + { + "related-to": "connectivity", + "related-link": "url of connectivity", + "relationship-data": { + "relationship-key": "connectivity. connectivity-id", + "relationship-value": "some id" + } + } + ] + } + } + } + ] + }, + "service-instance-by-connectivity": { + "results": [ + { + "connectivity": { + "connectivity-id": "{connectivityId}", + "bandwidth-profile-name": "some profile", + "vpn-type": "some type", + "cir": "cir value", + "eir": "eir value", + "cbs": "cbs value", + "ebs": "ebs value", + "color-aware": "color value", + "coupling-flag": "flag value", + "etht-svc-name": "some name", + "access-provider-id": "provider id", + "access-client-id": "client id", + "access-topology-id": "topology id", + "access-node-id": "node id", + "access-ltp-id": "ltp id", + "connectivity-selflink": "some URL", + "cvlan ": "some tag", + "operational-status": "some status", + "relationship-list": { + "relationship": [ + { + "related-to": "service-instance", + "related-link": "/aai/v14/business/customers/customer/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances?service-instance-id=service-1", + "relationship-data": { + "relationship-key": "service-instance.service-instance-id", + "relationship-value": "some id" + } + } + ] + } + } + } + ] + }, + "service-instances-by-service-type": { + "results": [ + { + "service-subscription": { + "service-type": "{service-type}", + "temp-ub-sub-account-id": "some sub account", + "service-instances": { + "service-instance": [ + { + "service-instance-id": "some id 1", + "service-instance-name": "some name 1", + "environment-context": "some context 1", + "workload-context": "some workload 1", + "relationship-list": { + "relationship": [ + ] + } + }, + { + "service-instance-id": "some id 2", + "service-instance-name": "some name 2", + "environment-context": "some context 2", + "workload-context": "some workload 2", + "relationship-list": { + "relationship": [ + ] + } + }, + { + "service-instance-id": "some id 3", + "service-instance-name": "some name 3", + "environment-context": "some context 3", + "workload-context": "some workload 3", + "relationship-list": { + "relationship": [ + ] + } + } + ] + }, + "relationship-list": { + "relationship": [ + ] + } + } + } + ] + } +}
\ No newline at end of file |