diff options
4 files changed, 596 insertions, 1 deletions
diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiQueryMdons.java b/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiQueryMdons.java new file mode 100644 index 0000000..e2bc357 --- /dev/null +++ b/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiQueryMdons.java @@ -0,0 +1,221 @@ +/** + * Copyright 2020 Fujitsu Limited. + * <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.google.gson.JsonArray; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; + +import org.apache.http.HttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPut; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.CloseableHttpClient; +import org.onap.holmes.common.aai.config.AaiConfig; +import org.onap.holmes.common.config.MicroServiceConfig; +import org.onap.holmes.common.exception.CorrelationException; +import org.onap.holmes.common.utils.HttpsUtils; + +import static org.onap.holmes.common.aai.AaiJsonParserUtil.getInfo; +import static org.onap.holmes.common.aai.AaiJsonParserUtil.getPath; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +import javax.ws.rs.core.MultivaluedHashMap; +import javax.ws.rs.core.MultivaluedMap; + +import org.jvnet.hk2.annotations.Service; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@Service +public class AaiQueryMdons { + + private final Logger log = LoggerFactory.getLogger(AaiQueryMdons.class); + private static final String RELATIONSHIP_VALUE = "relationship-value"; + private static final String RELATIONSHIP_LIST = "relationship-list"; + private static final String RELATIONSHIP_DATA = "relationship-data"; + private MultivaluedMap<String, Object> headers; + + public static AaiQueryMdons newInstance() { + return new AaiQueryMdons(); + } + + private AaiQueryMdons() { + 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"); + headers.add("Content-Type", "application/json"); + } + + private String getCompletePath(String urlTemplate, Map<String, String> pathParams) { + String url = urlTemplate; + for (Map.Entry<String, String> entry : pathParams.entrySet()) { + url = url.replaceAll("\\{" + entry.getKey() + "\\}", entry.getValue()); + } + return url; + } + + private String getResponse(String url) throws CorrelationException { + String response; + CloseableHttpClient httpClient = null; + HttpGet httpGet = new HttpGet(url); + try { + httpClient = HttpsUtils.getHttpsClient(HttpsUtils.DEFUALT_TIMEOUT); + HttpResponse httpResponse = HttpsUtils.get(httpGet, getHeaders(), httpClient); + response = HttpsUtils.extractResponseEntity(httpResponse); + } catch (Exception e) { + throw new CorrelationException("Failed to get data from aai", e); + } finally { + httpGet.releaseConnection(); + if (httpClient != null) { + try { + httpClient.close(); + } catch (IOException e) { + log.warn("Failed to close http client!"); + } + } + } + return response; + } + + private Map<String, String> getHeaders() { + Map<String, String> headers = new HashMap<>(); + headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID); + headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID); + headers.put("Authorization", AaiConfig.getAuthenticationCredentials()); + headers.put("Accept", "application/json"); + return headers; + } + + public Map<String, String> processPnf(String pnfId) throws CorrelationException { + Map<String, String> accessServiceMap = new HashMap<>(); + String url = MicroServiceConfig.getAaiAddr() + getPath(AaiConfig.AaiConsts.AAI_PNF_VALUE, "pnfName", pnfId); + String pnf = getResponse(url); + JsonObject jObject = JsonParser.parseString(pnf).getAsJsonObject(); + JsonObject pInterfaces = AaiJsonParserUtil.extractJsonObject(jObject, "p-interfaces"); + JsonArray pInterface = AaiJsonParserUtil.extractJsonArray(pInterfaces, "p-interface"); + for (int i = 0; i < pInterface.size(); i++) { + JsonObject relationshiplist = + AaiJsonParserUtil.extractJsonObject(pInterface.get(i).getAsJsonObject(), RELATIONSHIP_LIST); + JsonArray relationship = AaiJsonParserUtil.extractJsonArray(relationshiplist, "relationship"); + if (relationship != null) { + for (int j = 0; j < relationship.size(); j++) { + JsonObject object = relationship.get(j).getAsJsonObject(); + if (object.get("related-to").getAsString().equals("service-instance")) { + String domain = object.get(RELATIONSHIP_DATA).getAsJsonArray().get(2).getAsJsonObject() + .get(RELATIONSHIP_VALUE).getAsString(); + + String access = getAccessServiceForDomain(domain); + String[] accessSplit = access.split("__"); + accessServiceMap.put(accessSplit[0], accessSplit[1]); + + } + + } + + } + } + return accessServiceMap; + } + + private String getServiceInstanceAai(String serviceInstanceId) throws CorrelationException { + Map<String, String> paramMap = new HashMap<>(); + paramMap.put("global-customer-id", "Orange"); + paramMap.put("service-type", "MDONS_OTN"); + paramMap.put("instance-id", serviceInstanceId); + String url = MicroServiceConfig.getAaiAddr() + getCompletePath(AaiConfig.AaiConsts.AAI_SERVICE, paramMap); + return getResponse(url); + } + + private String getAccessServiceForDomain(String serviceInstanceId) throws CorrelationException { + String domainInstance = getServiceInstanceAai(serviceInstanceId); + JsonObject matchedObject = getInfo(domainInstance, "service-instance"); + String accessInstanceId = matchedObject.get(RELATIONSHIP_DATA).getAsJsonArray().get(2).getAsJsonObject() + .get(RELATIONSHIP_VALUE).getAsString(); + String accessName = matchedObject.get("related-to-property").getAsJsonArray().get(0).getAsJsonObject() + .get("property-value").getAsString(); + return accessInstanceId + "__" + accessName; + } + + public void updateLinksForAccessService(Map<String, String> accessInstanceList) throws CorrelationException { + for (String access : accessInstanceList.keySet()) { + String response = getServiceInstanceAai(access); + JsonObject matchedObject = getInfo(response, "logical-link"); + if (matchedObject != null) { + String linkName = matchedObject.get(RELATIONSHIP_DATA).getAsJsonArray().get(0).getAsJsonObject() + .get(RELATIONSHIP_VALUE).getAsString(); + updateLogicLinkStatus(linkName, "down"); + } + + } + + } + + public String getPnfNameFromPnfId(String pnfId) throws CorrelationException { + String url = MicroServiceConfig.getAaiAddr() + getPath(AaiConfig.AaiConsts.AAI_PNF_ID, "pnfId", pnfId); + String pnf = getResponse(url); + JsonObject jsonObject = JsonParser.parseString(pnf).getAsJsonObject(); + JsonArray pnfList = AaiJsonParserUtil.extractJsonArray(jsonObject, "pnf"); + return pnfList.get(0).getAsJsonObject().get("pnf-name").getAsString(); + + } + + public void updatePnfOperationalStatus(String pnfName, String status) throws CorrelationException { + String url = MicroServiceConfig.getAaiAddr() + getPath(AaiConfig.AaiConsts.AAI_PNF, "pnfName", pnfName); + String pnf = getResponse(url); + JsonObject jsonObject = JsonParser.parseString(pnf).getAsJsonObject(); + jsonObject.addProperty("operational-status", status); + put(url, jsonObject.toString()); + + } + + public void updateLogicLinkStatus(String linkName, String status) throws CorrelationException { + String url = + MicroServiceConfig.getAaiAddr() + getPath(AaiConfig.AaiConsts.AAI_LINK_UPDATE, "linkName", linkName); + String response = getResponse(url); + JsonObject jsonObject = JsonParser.parseString(response).getAsJsonObject(); + jsonObject.addProperty("operational-status", status); + put(url, jsonObject.toString()); + } + + private HttpResponse put(String url, String content) throws CorrelationException { + CloseableHttpClient httpClient = null; + HttpPut httpPut = new HttpPut(url); + try { + httpClient = HttpsUtils.getConditionalHttpsClient(HttpsUtils.DEFUALT_TIMEOUT); + return HttpsUtils.put(httpPut, getHeaders(), new HashMap<>(), new StringEntity(content), httpClient); + } catch (Exception e) { + throw new CorrelationException("Failed to put data in AAI", e); + } finally { + closeHttpClient(httpClient); + } + } + + private void closeHttpClient(CloseableHttpClient httpClient) { + if (httpClient != null) { + try { + httpClient.close(); + } catch (IOException e) { + log.warn("Failed to close http client!"); + } + } + } + +} 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 4879e33..34fa67a 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 @@ -15,7 +15,7 @@ package org.onap.holmes.common.aai.config; public class AaiConfig { - private static final String AAI_API_VERSION = "v16"; + private static final String AAI_API_VERSION = "v19"; public static final String X_TRANSACTION_ID = "9999"; @@ -54,6 +54,14 @@ public class AaiConfig { 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 + "v11/search/nodes-query?search-node-type=vserver&filter="; + + public static final String AAI_SERVICE = AAI_SERVICE_INSTANCES_ADDR_4_CCVPN + "/service-instances/service-instance/{instance-id}"; + + public static final String AAI_PNF = AAI_PREF + AAI_API_VERSION + "/network/pnfs/pnf/{pnfName}"; + + public static final String AAI_PNF_VALUE = AAI_PNF + "?depth=all"; + + public static final String AAI_PNF_ID = AAI_PREF + AAI_API_VERSION + "/network/pnfs?pnf-id={pnfId}"; } diff --git a/holmes-actions/src/test/java/org/onap/holmes/common/aai/AaiQueryMdonsTest.java b/holmes-actions/src/test/java/org/onap/holmes/common/aai/AaiQueryMdonsTest.java new file mode 100644 index 0000000..0fd577c --- /dev/null +++ b/holmes-actions/src/test/java/org/onap/holmes/common/aai/AaiQueryMdonsTest.java @@ -0,0 +1,175 @@ +/** + * Copyright 2020 Fujitsu Limited. + * <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 static org.junit.Assert.assertEquals; +import static org.onap.holmes.common.config.MicroServiceConfig.MSB_ADDR; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +import javax.ws.rs.core.MultivaluedHashMap; +import javax.ws.rs.core.MultivaluedMap; + +import org.apache.http.HttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; +import org.onap.holmes.common.aai.config.AaiConfig; +import org.onap.holmes.common.config.MicroServiceConfig; +import org.onap.holmes.common.utils.HttpsUtils; +import org.powermock.api.easymock.PowerMock; +import org.powermock.core.classloader.annotations.PowerMockIgnore; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; +import org.powermock.reflect.Whitebox; + +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; + +@RunWith(PowerMockRunner.class) +@PowerMockIgnore("javax.net.ssl.*") +@PrepareForTest({AaiQueryMdons.class, HttpsUtils.class, MicroServiceConfig.class, HttpGet.class}) +public class AaiQueryMdonsTest { + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + private static AaiQueryMdons aaiMdonsQuery = AaiQueryMdons.newInstance(); + private static MultivaluedMap<String, Object> headers = new MultivaluedHashMap<>(); + + private static JsonObject data; + + private HttpResponse httpResponse; + private static final String AAI_ADDR = "https://aai.onap:8443/aai/v19/"; + + @BeforeClass + public static void beforeClass() { + System.setProperty(MSB_ADDR, "127.0.0.1:80"); + System.setProperty("ENABLE_ENCRYPT", "true"); + + File file = new File(AaiQueryMdonsTest.class.getClassLoader().getResource("./aai-mdons.json").getFile()); + BufferedReader reader = null; + try { + reader = new BufferedReader(new FileReader(file)); + StringBuilder sb = new StringBuilder(); + reader.lines().forEach(l -> sb.append(l)); + data = JsonParser.parseString(sb.toString()).getAsJsonObject(); + } 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"); + headers.add("Content-Type", "application/json"); + + } + + @Before + public void before() { + httpResponse = PowerMock.createMock(HttpResponse.class); + Whitebox.setInternalState(aaiMdonsQuery, "headers", headers); + + } + + @After + public void after() { + PowerMock.resetAll(); + } + + @Test + public void testProcessPnf() throws Exception { + String pnfUrl = AAI_ADDR + "network/pnfs/pnf/test1?depth=all"; + String domainService = AAI_ADDR + + "business/customers/customer/Orange/service-subscriptions/service-subscription/MDONS_OTN/service-instances/service-instance/789"; + PowerMock.resetAll(); + + aaiMdonsQuery = PowerMock.createPartialMock(AaiQueryMdons.class, "getResponse"); + PowerMock.expectPrivate(aaiMdonsQuery, "getResponse", pnfUrl).andReturn(data.get("pnf-depth-all").toString()); + PowerMock.expectPrivate(aaiMdonsQuery, "getResponse", domainService) + .andReturn(data.get("get-domain-service").toString()); + + PowerMock.replayAll(); + Map<String, String> accessMap = aaiMdonsQuery.processPnf("test1"); + PowerMock.verifyAll(); + Map<String, String> verifyMap = new HashMap<>(); + verifyMap.put("123", "access-service"); + assertEquals(accessMap, verifyMap); + } + + @Test + public void testGetPnfName() throws Exception { + String pnfUrl = AAI_ADDR + "network/pnfs?pnf-id=test1-id"; + + Whitebox.setInternalState(aaiMdonsQuery, "headers", headers); + + aaiMdonsQuery = PowerMock.createPartialMock(AaiQueryMdons.class, "getResponse"); + PowerMock.expectPrivate(aaiMdonsQuery, "getResponse", pnfUrl).andReturn(data.get("get-pnf-by-id").toString()); + PowerMock.replayAll(); + String pnfName = Whitebox.invokeMethod(aaiMdonsQuery, "getPnfNameFromPnfId", "test1-id"); + + PowerMock.verifyAll(); + assertEquals(pnfName, "test1"); + + } + + @Test + public void testUpdatelinksAccess() throws Exception { + Map<String, String> accessMap = new HashMap<>(); + accessMap.put("123", "access-service"); + String accessService = AAI_ADDR + + "business/customers/customer/Orange/service-subscriptions/service-subscription/MDONS_OTN/service-instances/service-instance/123"; + String linkUrl = AAI_ADDR + "network/logical-links/logical-link/link1"; + + String response = + "{\"link-name\":\"link1\",\"in-maint\":false,\"link-type\":\"inter-domain\",\"available-capacity\":\"ODU2\",\"resource-version\":\"1584338211407\",\"operational-status\":\"down\"}"; + + aaiMdonsQuery = PowerMock.createPartialMock(AaiQueryMdons.class, "getResponse", "put"); + + PowerMock.expectPrivate(aaiMdonsQuery, "getResponse", accessService) + .andReturn(data.get("get-access-service").toString()); + PowerMock.expectPrivate(aaiMdonsQuery, "getResponse", linkUrl).andReturn(data.get("get-inter-link").toString()); + PowerMock.expectPrivate(aaiMdonsQuery, "put", linkUrl, response).andReturn(httpResponse); + + PowerMock.replayAll(); + Whitebox.invokeMethod(aaiMdonsQuery, "updateLinksForAccessService", accessMap); + + PowerMock.verifyAll(); + + } + +} diff --git a/holmes-actions/src/test/resources/aai-mdons.json b/holmes-actions/src/test/resources/aai-mdons.json new file mode 100644 index 0000000..517751c --- /dev/null +++ b/holmes-actions/src/test/resources/aai-mdons.json @@ -0,0 +1,191 @@ +{ + "get-pnf-by-id":{ + "pnf":[ + { + "pnf-name":"test1", + "in-maint":false, + "resource-version":"1593337809133", + "operational-status":"down" + } + ] + }, + "get-domain-service":{ + "service-instance-id":"789", + "service-instance-name":"domain-service", + "service-type":"Domain-Service", + "service-rate":"ODU2", + "service-layer":"ODU", + "orchestration-status":"CREATED", + "resource-version":"1592808052617", + "relationship-list":{ + "relationship":[ + { + "related-to":"p-interface", + "relationship-label":"org.onap.relationships.inventory.ComposedOf", + "related-link":"/aai/v19/network/pnfs/pnf/test1/p-interfaces/p-interface/int1", + "relationship-data":[ + { + "relationship-key":"pnf.pnf-name", + "relationship-value":"test1" + }, + { + "relationship-key":"p-interface.interface-name", + "relationship-value":"int1" + } + ] + }, + { + "related-to":"p-interface", + "relationship-label":"org.onap.relationships.inventory.ComposedOf", + "related-link":"/aai/v19/network/pnfs/pnf/test2/p-interfaces/p-interface/int4", + "relationship-data":[ + { + "relationship-key":"pnf.pnf-name", + "relationship-value":"test2" + }, + { + "relationship-key":"p-interface.interface-name", + "relationship-value":"int4" + } + ], + "related-to-property":[ + { + "property-key":"service-instance.service-instance-name", + "property-value":"domain-service" + } + ] + }, + { + "related-to":"service-instance", + "relationship-label":"org.onap.relationships.inventory.ComposedOf", + "related-link":"/aai/v19/business/customers/customer/Orange/service-subscriptions/service-subscription/MDONS_OTN/service-instances/service-instance/123", + "relationship-data":[ + { + "relationship-key":"customer.global-customer-id", + "relationship-value":"Orange" + }, + { + "relationship-key":"service-subscription.service-type", + "relationship-value":"MDONS_OTN" + }, + { + "relationship-key":"service-instance.service-instance-id", + "relationship-value":"123" + } + ], + "related-to-property":[ + { + "property-key":"service-instance.service-instance-name", + "property-value":"access-service" + } + ] + } + ] + } + }, + "get-access-service":{ + "service-instance-id":"123", + "service-instance-name":"access-service", + "service-type":"Access-Service", + "service-rate":"ODU2", + "service-layer":"ODU", + "resource-version":"1593324022557", + "orchestration-status":"CREATED", + "relationship-list":{ + "relationship":[ + { + "related-to":"service-instance", + "relationship-label":"org.onap.relationships.inventory.ComposedOf", + "related-link":"/aai/v19/business/customers/customer/Orange/service-subscriptions/service-subscription/MDONS_OTN/service-instances/service-instance/789", + "relationship-data":[ + { + "relationship-key":"customer.global-customer-id", + "relationship-value":"Orange" + }, + { + "relationship-key":"service-subscription.service-type", + "relationship-value":"MDONS_OTN" + }, + { + "relationship-key":"service-instance.service-instance-id", + "relationship-value":"789" + } + ], + "related-to-property":[ + { + "property-key":"service-instance.service-instance-name", + "property-value":"domain-service" + } + ] + }, + { + "related-to":"logical-link", + "relationship-label":"org.onap.relationships.inventory.ComposedOf", + "related-link":"/aai/v19/network/logical-links/logical-link/link1", + "relationship-data":[ + { + "relationship-key":"logical-link.link-name", + "relationship-value":"link1" + } + ] + } + ] + } + }, + "pnf-depth-all":{ + "pnf-name":"test1", + "resource-version":"1593337809133", + "operational-status":"down", + "p-interfaces":{ + "p-interface":[ + { + "interface-name":"int1", + "in-maint":false, + "resource-version":"159333780998", + "relationship-list":{ + "relationship":[ + { + "related-to":"service-instance", + "relationship-label":"org.onap.relationships.inventory.ComposedOf", + "related-link":"/aai/v19/business/customers/customer/Orange/service-subscriptions/service-subscription/MDONS_OTN/service-instances/service-instance/789", + "relationship-data":[ + { + "relationship-key":"customer.global-customer-id", + "relationship-value":"Orange" + }, + { + "relationship-key":"service-subscription.service-type", + "relationship-value":"MDONS_OTN" + }, + { + "relationship-key":"service-instance.service-instance-id", + "relationship-value":"789" + } + ], + "related-to-property":[ + { + "property-key":"service-instance.service-instance-name", + "property-value":"domain-service" + } + ] + } + ] + } + }, + { + "interface-name":"int2", + "in-maint":false, + "resource-version":"1593337809136" + } + ] + } + }, + "get-inter-link":{ + "link-name":"link1", + "in-maint":false, + "link-type":"inter-domain", + "available-capacity":"ODU2", + "resource-version":"1584338211407", + "operational-status":"up" + } +} |