Replaced all tabs with spaces in java and pom.xml
[so.git] / common / src / main / java / org / onap / so / client / sdno / SDNOValidatorImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.so.client.sdno;
22
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;
43
44 public class SDNOValidatorImpl implements SDNOValidator {
45
46     private final static String clientName = "MSO";
47     private final static String HEALTH_DIAGNOSTIC_CODE_DEFAULT = "default";
48
49     @Override
50     public boolean healthDiagnostic(String vnfId, UUID uuid, String requestingUserId) throws IOException, Exception {
51
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"));
56
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());
62         return status;
63     }
64
65     @Override
66     public boolean healthDiagnostic(GenericVnf genericVnf, UUID uuid, String requestingUserId)
67             throws IOException, Exception {
68
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());
74         return status;
75     }
76
77     protected SDNO buildRequestDiagnostic(GenericVnf vnf, UUID uuid, String requestingUserId) {
78
79         Optional<String> nfRole;
80         if (vnf.getNfRole() == null) {
81             nfRole = Optional.empty();
82         } else {
83             nfRole = Optional.of(vnf.getNfRole());
84         }
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");
91
92         body.setInput(input);
93
94         RequestHealthDiagnostic request = new RequestHealthDiagnostic();
95
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);
104
105         input.setRequestHealthDiagnostic(request);
106
107         return parentRequest;
108     }
109
110     protected void submitRequest(String json) throws FileNotFoundException, IOException, InterruptedException {
111
112         DmaapPublisher publisher = new SDNOHealthCheckDmaapPublisher();
113         publisher.send(json);
114     }
115
116     protected boolean pollForResponse(String uuid) throws Exception {
117         DmaapConsumer consumer = this.getConsumer(uuid);
118         return consumer.consume();
119     }
120
121
122
123     protected DmaapConsumer getConsumer(String uuid) throws FileNotFoundException, IOException {
124         return new SDNOHealthCheckDmaapConsumer(uuid);
125     }
126
127
128
129 }