Containerization feature of SO
[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
51         @Override
52         public boolean healthDiagnostic(String vnfId, UUID uuid, String requestingUserId) throws IOException, Exception {
53                 
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"));
57                 
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());
63                 return status;          
64         }
65         
66         @Override
67         public boolean healthDiagnostic(GenericVnf genericVnf, UUID uuid, String requestingUserId) 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> vnfType;
80                 if (vnf.getVnfType() == null) {
81                         vnfType = Optional.empty();
82                 } else {
83                         vnfType = Optional.of(vnf.getVnfType());
84                 }
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");
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.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
102                 
103                 input.setRequestHealthDiagnostic(request);
104                 
105                 return parentRequest;
106         }
107         protected void submitRequest(String json) throws FileNotFoundException, IOException, InterruptedException {
108                 
109                 DmaapPublisher publisher = new SDNOHealthCheckDmaapPublisher();
110                 publisher.send(json);
111         }
112         protected boolean pollForResponse(String uuid) throws Exception {
113                 DmaapConsumer consumer = this.getConsumer(uuid);
114                 return consumer.consume();
115         }
116         
117
118         
119         protected DmaapConsumer getConsumer(String uuid) throws FileNotFoundException, IOException {
120                 return new SDNOHealthCheckDmaapConsumer(uuid);
121         }
122         
123
124         
125 }