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