Changed the object mapper to ignore unknown fields
[so.git] / so-sdn-clients / 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.aaiclient.client.aai.AAIObjectType;
30 import org.onap.aaiclient.client.aai.AAIResourcesClient;
31 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri;
32 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory;
33 import org.onap.so.client.dmaap.DmaapConsumer;
34 import org.onap.so.client.dmaap.DmaapPublisher;
35 import org.onap.so.client.sdno.beans.Body;
36 import org.onap.so.client.sdno.beans.Input;
37 import org.onap.so.client.sdno.beans.RequestHealthDiagnostic;
38 import org.onap.so.client.sdno.beans.SDNO;
39 import org.onap.so.client.sdno.dmaap.SDNOHealthCheckDmaapConsumer;
40 import org.onap.so.client.sdno.dmaap.SDNOHealthCheckDmaapPublisher;
41 import com.fasterxml.jackson.databind.ObjectMapper;
42
43 public class SDNOValidatorImpl implements SDNOValidator {
44
45     private final static String clientName = "MSO";
46     private final static String HEALTH_DIAGNOSTIC_CODE_DEFAULT = "default";
47
48     @Override
49     public boolean healthDiagnostic(String vnfId, UUID uuid, String requestingUserId) throws IOException, Exception {
50
51         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId);
52         AAIResourcesClient client = new AAIResourcesClient();
53         GenericVnf vnf = client.get(GenericVnf.class, uri)
54                 .orElseThrow(() -> new NotFoundException(vnfId + " not found in A&AI"));
55
56         SDNO requestDiagnostic = buildRequestDiagnostic(vnf, uuid, requestingUserId);
57         ObjectMapper mapper = new ObjectMapper();
58         String json = mapper.writeValueAsString(requestDiagnostic);
59         this.submitRequest(json);
60         boolean status = this.pollForResponse(uuid.toString());
61         return status;
62     }
63
64     protected SDNO buildRequestDiagnostic(GenericVnf vnf, UUID uuid, String requestingUserId) {
65
66         Optional<String> nfRole;
67         if (vnf.getNfRole() == null) {
68             nfRole = Optional.empty();
69         } else {
70             nfRole = Optional.of(vnf.getNfRole());
71         }
72         Input input = new Input();
73         SDNO parentRequest = new SDNO();
74         Body body = new Body();
75         parentRequest.setBody(body);
76         parentRequest.setNodeType(nfRole.orElse("NONE").toUpperCase());
77         parentRequest.setOperation("health-diagnostic");
78
79         body.setInput(input);
80
81         RequestHealthDiagnostic request = new RequestHealthDiagnostic();
82
83         request.setRequestClientName(clientName);
84         request.setRequestNodeName(vnf.getVnfName());
85         request.setRequestNodeUuid(vnf.getVnfId());
86         request.setRequestNodeType(nfRole.orElse("NONE").toUpperCase());
87         request.setRequestNodeIp(vnf.getIpv4OamAddress()); // generic-vnf oam ip
88         request.setRequestUserId(requestingUserId); // mech id?
89         request.setRequestId(uuid.toString()); // something to identify this request by for polling
90         request.setHealthDiagnosticCode(HEALTH_DIAGNOSTIC_CODE_DEFAULT);
91
92         input.setRequestHealthDiagnostic(request);
93
94         return parentRequest;
95     }
96
97     protected void submitRequest(String json) throws FileNotFoundException, IOException, InterruptedException {
98
99         DmaapPublisher publisher = new SDNOHealthCheckDmaapPublisher();
100         publisher.send(json);
101     }
102
103     protected boolean pollForResponse(String uuid) throws Exception {
104         DmaapConsumer consumer = this.getConsumer(uuid);
105         return consumer.consume();
106     }
107
108
109
110     protected DmaapConsumer getConsumer(String uuid) throws FileNotFoundException, IOException {
111         return new SDNOHealthCheckDmaapConsumer(uuid);
112     }
113
114
115
116 }