Merge "updated logging to JaxRsFilterLogging"
[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
28 import javax.ws.rs.NotFoundException;
29
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;
44
45 import com.fasterxml.jackson.databind.ObjectMapper;
46
47 public class SDNOValidatorImpl implements SDNOValidator {
48
49         private final static String clientName = "MSO";
50         private final static String HEALTH_DIAGNOSTIC_CODE_DEFAULT = "default";
51
52         @Override
53         public boolean healthDiagnostic(String vnfId, UUID uuid, String requestingUserId) throws IOException, Exception {
54                 
55                 AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId);
56                 AAIResourcesClient client = new AAIResourcesClient();
57                 GenericVnf vnf = client.get(GenericVnf.class, uri).orElseThrow(() -> new NotFoundException(vnfId + " not found in A&AI"));
58                 
59                 SDNO requestDiagnostic = buildRequestDiagnostic(vnf, uuid, requestingUserId);
60                 ObjectMapper mapper = new ObjectMapper();
61                 String json = mapper.writeValueAsString(requestDiagnostic);
62                 this.submitRequest(json);
63                 boolean status = this.pollForResponse(uuid.toString());
64                 return status;          
65         }
66         
67         @Override
68         public boolean healthDiagnostic(GenericVnf genericVnf, UUID uuid, String requestingUserId) throws IOException, Exception {
69                 
70                 SDNO requestDiagnostic = buildRequestDiagnostic(genericVnf, uuid, requestingUserId);
71                 ObjectMapper mapper = new ObjectMapper();
72                 String json = mapper.writeValueAsString(requestDiagnostic);
73                 this.submitRequest(json);
74                 boolean status = this.pollForResponse(uuid.toString());
75                 return status;          
76         }
77
78         protected SDNO buildRequestDiagnostic(GenericVnf vnf, UUID uuid, String requestingUserId) {
79                 
80                 Optional<String> vnfType;
81                 if (vnf.getVnfType() == null) {
82                         vnfType = Optional.empty();
83                 } else {
84                         vnfType = Optional.of(vnf.getVnfType());
85                 }
86                 Input input = new Input();
87                 SDNO parentRequest = new SDNO();
88                 Body body = new Body();
89                 parentRequest.setBody(body);
90                 parentRequest.setNodeType(vnfType.orElse("NONE").toUpperCase());
91                 parentRequest.setOperation("health-diagnostic");
92                 
93                 body.setInput(input);
94                 
95                 RequestHealthDiagnostic request = new RequestHealthDiagnostic();
96
97                 request.setRequestClientName(clientName);
98                 request.setRequestNodeName(vnf.getVnfName());
99                 request.setRequestNodeUuid(vnf.getVnfId());
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         protected void submitRequest(String json) throws FileNotFoundException, IOException, InterruptedException {
110                 
111                 DmaapPublisher publisher = new SDNOHealthCheckDmaapPublisher();
112                 publisher.send(json);
113         }
114         protected boolean pollForResponse(String uuid) throws Exception {
115                 DmaapConsumer consumer = this.getConsumer(uuid);
116                 return consumer.consume();
117         }
118         
119
120         
121         protected DmaapConsumer getConsumer(String uuid) throws FileNotFoundException, IOException {
122                 return new SDNOHealthCheckDmaapConsumer(uuid);
123         }
124         
125
126         
127 }