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;
28 import javax.ws.rs.NotFoundException;
30 import org.onap.aai.domain.yang.GenericVnf;
31 import org.onap.so.client.aai.AAIObjectType;
32 import org.onap.so.client.aai.AAIResourcesClient;
33 import org.onap.so.client.aai.AAIVersion;
34 import org.onap.so.client.aai.entities.uri.AAIResourceUri;
35 import org.onap.so.client.aai.entities.uri.AAIUriFactory;
36 import org.onap.so.client.dmaap.DmaapConsumer;
37 import org.onap.so.client.dmaap.DmaapPublisher;
38 import org.onap.so.client.sdno.beans.Body;
39 import org.onap.so.client.sdno.beans.Input;
40 import org.onap.so.client.sdno.beans.RequestHealthDiagnostic;
41 import org.onap.so.client.sdno.beans.SDNO;
42 import org.onap.so.client.sdno.dmaap.SDNOHealthCheckDmaapConsumer;
43 import org.onap.so.client.sdno.dmaap.SDNOHealthCheckDmaapPublisher;
45 import com.fasterxml.jackson.databind.ObjectMapper;
47 public class SDNOValidatorImpl implements SDNOValidator {
49 private final static String clientName = "MSO";
52 public boolean healthDiagnostic(String vnfId, UUID uuid, String requestingUserId) throws IOException, Exception {
54 AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId);
55 AAIResourcesClient client = new AAIResourcesClient(AAIVersion.V10);
56 GenericVnf vnf = client.get(GenericVnf.class, uri).orElseThrow(() -> new NotFoundException(vnfId + " not found in A&AI"));
58 SDNO requestDiagnostic = buildRequestDiagnostic(vnf, uuid, requestingUserId);
59 ObjectMapper mapper = new ObjectMapper();
60 String json = mapper.writeValueAsString(requestDiagnostic);
61 this.submitRequest(json);
62 boolean status = this.pollForResponse(uuid.toString());
67 public boolean healthDiagnostic(GenericVnf genericVnf, UUID uuid, String requestingUserId) 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> vnfType;
80 if (vnf.getVnfType() == null) {
81 vnfType = Optional.empty();
83 vnfType = Optional.of(vnf.getVnfType());
85 Input input = new Input();
86 SDNO parentRequest = new SDNO();
87 Body body = new Body();
88 parentRequest.setBody(body);
89 parentRequest.setNodeType(vnfType.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.setRequestNodeIp(vnf.getIpv4OamAddress()); //generic-vnf oam ip
100 request.setRequestUserId(requestingUserId); //mech id?
101 request.setRequestId(uuid.toString()); //something to identify this request by for polling
103 input.setRequestHealthDiagnostic(request);
105 return parentRequest;
107 protected void submitRequest(String json) throws FileNotFoundException, IOException, InterruptedException {
109 DmaapPublisher publisher = new SDNOHealthCheckDmaapPublisher();
110 publisher.send(json);
112 protected boolean pollForResponse(String uuid) throws Exception {
113 DmaapConsumer consumer = this.getConsumer(uuid);
114 return consumer.consume();
119 protected DmaapConsumer getConsumer(String uuid) throws FileNotFoundException, IOException {
120 return new SDNOHealthCheckDmaapConsumer(uuid);