2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.so.client.sdno;
23 import java.io.FileNotFoundException;
24 import java.io.IOException;
25 import java.util.Optional;
26 import java.util.UUID;
27 import javax.ws.rs.NotFoundException;
28 import org.onap.aai.domain.yang.GenericVnf;
29 import org.onap.so.client.aai.AAIObjectType;
30 import org.onap.so.client.aai.AAIResourcesClient;
31 import org.onap.so.client.aai.AAIVersion;
32 import org.onap.so.client.aai.entities.uri.AAIResourceUri;
33 import org.onap.so.client.aai.entities.uri.AAIUriFactory;
34 import org.onap.so.client.dmaap.DmaapConsumer;
35 import org.onap.so.client.dmaap.DmaapPublisher;
36 import org.onap.so.client.sdno.beans.Body;
37 import org.onap.so.client.sdno.beans.Input;
38 import org.onap.so.client.sdno.beans.RequestHealthDiagnostic;
39 import org.onap.so.client.sdno.beans.SDNO;
40 import org.onap.so.client.sdno.dmaap.SDNOHealthCheckDmaapConsumer;
41 import org.onap.so.client.sdno.dmaap.SDNOHealthCheckDmaapPublisher;
42 import com.fasterxml.jackson.databind.ObjectMapper;
44 public class SDNOValidatorImpl implements SDNOValidator {
46 private final static String clientName = "MSO";
47 private final static String HEALTH_DIAGNOSTIC_CODE_DEFAULT = "default";
50 public boolean healthDiagnostic(String vnfId, UUID uuid, String requestingUserId) throws IOException, Exception {
52 AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId);
53 AAIResourcesClient client = new AAIResourcesClient();
54 GenericVnf vnf = client.get(GenericVnf.class, uri)
55 .orElseThrow(() -> new NotFoundException(vnfId + " not found in A&AI"));
57 SDNO requestDiagnostic = buildRequestDiagnostic(vnf, uuid, requestingUserId);
58 ObjectMapper mapper = new ObjectMapper();
59 String json = mapper.writeValueAsString(requestDiagnostic);
60 this.submitRequest(json);
61 boolean status = this.pollForResponse(uuid.toString());
66 public boolean healthDiagnostic(GenericVnf genericVnf, UUID uuid, String requestingUserId)
67 throws IOException, Exception {
69 SDNO requestDiagnostic = buildRequestDiagnostic(genericVnf, uuid, requestingUserId);
70 ObjectMapper mapper = new ObjectMapper();
71 String json = mapper.writeValueAsString(requestDiagnostic);
72 this.submitRequest(json);
73 boolean status = this.pollForResponse(uuid.toString());
77 protected SDNO buildRequestDiagnostic(GenericVnf vnf, UUID uuid, String requestingUserId) {
79 Optional<String> nfRole;
80 if (vnf.getNfRole() == null) {
81 nfRole = Optional.empty();
83 nfRole = Optional.of(vnf.getNfRole());
85 Input input = new Input();
86 SDNO parentRequest = new SDNO();
87 Body body = new Body();
88 parentRequest.setBody(body);
89 parentRequest.setNodeType(nfRole.orElse("NONE").toUpperCase());
90 parentRequest.setOperation("health-diagnostic");
94 RequestHealthDiagnostic request = new RequestHealthDiagnostic();
96 request.setRequestClientName(clientName);
97 request.setRequestNodeName(vnf.getVnfName());
98 request.setRequestNodeUuid(vnf.getVnfId());
99 request.setRequestNodeType(nfRole.orElse("NONE").toUpperCase());
100 request.setRequestNodeIp(vnf.getIpv4OamAddress()); // generic-vnf oam ip
101 request.setRequestUserId(requestingUserId); // mech id?
102 request.setRequestId(uuid.toString()); // something to identify this request by for polling
103 request.setHealthDiagnosticCode(HEALTH_DIAGNOSTIC_CODE_DEFAULT);
105 input.setRequestHealthDiagnostic(request);
107 return parentRequest;
110 protected void submitRequest(String json) throws FileNotFoundException, IOException, InterruptedException {
112 DmaapPublisher publisher = new SDNOHealthCheckDmaapPublisher();
113 publisher.send(json);
116 protected boolean pollForResponse(String uuid) throws Exception {
117 DmaapConsumer consumer = this.getConsumer(uuid);
118 return consumer.consume();
123 protected DmaapConsumer getConsumer(String uuid) throws FileNotFoundException, IOException {
124 return new SDNOHealthCheckDmaapConsumer(uuid);