Replaced all tabs with spaces in java and pom.xml
[so.git] / common / src / main / java / org / onap / so / client / sdno / SDNOHealthCheckClient.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.ArrayList;
26 import java.util.Collections;
27 import java.util.List;
28 import java.util.Optional;
29 import org.onap.so.client.dmaap.DmaapConsumer;
30 import org.onap.so.client.dmaap.DmaapPublisher;
31 import org.onap.so.client.sdno.beans.AAIParamList;
32 import org.onap.so.client.sdno.beans.Body;
33 import org.onap.so.client.sdno.beans.Input;
34 import org.onap.so.client.sdno.beans.RequestHdCustom;
35 import org.onap.so.client.sdno.beans.SDNO;
36 import org.onap.so.client.sdno.dmaap.SDNOHealthCheckDmaapConsumer;
37 import org.onap.so.client.sdno.dmaap.SDNOHealthCheckDmaapPublisher;
38 import com.fasterxml.jackson.core.JsonProcessingException;
39 import com.fasterxml.jackson.databind.ObjectMapper;
40
41 public class SDNOHealthCheckClient {
42
43     private static final String NODE_TYPE = "VROUTER";
44     private static final String API_OPERATION_TYPE = "health-diagnostic-custom";
45     private static final String MIRRORING_CHECK = "mirroring_check";
46     private static final String CLIENT_NAME = "MSO";
47     private static final String PRE_CHECK_CODE = "VROUTER000003";
48     private static final String POST_CHECK_CODE = "VROUTER000004";
49     private static final String LPORT_MIRRORING_CHECK = "lport_mirroring_check";
50     private static final String CONFIGURATION_ID = "configuration-id";
51
52
53     public boolean lPortMirrorHealthPreCheck(String userId, String requestId, Optional<String> clliCode,
54             String configurationId, String interfaceId) throws Exception {
55         String request = buildLPortMirrorCheckPreRequest(userId, requestId, clliCode, configurationId, interfaceId);
56         return this.execute(requestId, request);
57     }
58
59     public boolean lPortMirrorHealthPostCheck(String userId, String requestId, Optional<String> clliCode,
60             String configurationId, String interfaceId) throws Exception {
61         String request = buildLPortMirrorCheckPostRequest(userId, requestId, clliCode, configurationId, interfaceId);
62         return this.execute(requestId, request);
63     }
64
65     public boolean portMirrorHealthPreCheck(String userId, String requestId, Optional<String> clliCode,
66             String configurationId) throws Exception {
67         final String request = this.buildPortMirrorPreCheckRequest(userId, requestId, clliCode, configurationId);
68         return this.execute(requestId, request);
69     }
70
71     public boolean portMirrorHealthPostCheck(String userId, String requestId, Optional<String> clliCode,
72             String configurationId) throws Exception {
73         final String request = this.buildPortMirrorPostCheckRequest(userId, requestId, clliCode, configurationId);
74         return this.execute(requestId, request);
75     }
76
77     protected String buildLPortMirrorCheckPreRequest(String userId, String requestId, Optional<String> clliCode,
78             String configurationId, String interfaceId) throws JsonProcessingException {
79         return this.buildLPortMirrorCheckRequest(userId, requestId, clliCode, configurationId, interfaceId,
80                 PRE_CHECK_CODE);
81     }
82
83     protected String buildLPortMirrorCheckPostRequest(String userId, String requestId, Optional<String> clliCode,
84             String configurationId, String interfaceId) throws JsonProcessingException {
85         return this.buildLPortMirrorCheckRequest(userId, requestId, clliCode, configurationId, interfaceId,
86                 POST_CHECK_CODE);
87     }
88
89     protected String buildPortMirrorPreCheckRequest(String userId, String requestId, Optional<String> clliCode,
90             String configurationId) throws JsonProcessingException {
91         return this.buildPortMirrorCheckRequest(userId, requestId, clliCode, configurationId, PRE_CHECK_CODE);
92     }
93
94     protected String buildPortMirrorPostCheckRequest(String userId, String requestId, Optional<String> clliCode,
95             String configurationId) throws JsonProcessingException {
96         return this.buildPortMirrorCheckRequest(userId, requestId, clliCode, configurationId, POST_CHECK_CODE);
97     }
98
99     protected String buildPortMirrorCheckRequest(String userId, String requestId, Optional<String> clliCode,
100             String configurationId, String diagnosticCode) throws JsonProcessingException {
101         final AAIParamList list = new AAIParamList();
102         list.setKey(CONFIGURATION_ID);
103         list.setValue(configurationId);
104
105         return this.buildRequest(userId, requestId, clliCode, diagnosticCode, MIRRORING_CHECK,
106                 Collections.singletonList(list));
107     }
108
109     protected String buildLPortMirrorCheckRequest(String userId, String requestId, Optional<String> clliCode,
110             String configurationId, String interfaceId, String diagnosticCode) throws JsonProcessingException {
111
112         final AAIParamList configurationIdParam = new AAIParamList();
113         configurationIdParam.setKey(CONFIGURATION_ID);
114         configurationIdParam.setValue(configurationId);
115         final AAIParamList interfaceIdParam = new AAIParamList();
116         interfaceIdParam.setKey("interface-id");
117         interfaceIdParam.setValue(interfaceId);
118         final List<AAIParamList> list = new ArrayList<>();
119         list.add(configurationIdParam);
120         list.add(interfaceIdParam);
121         return this.buildRequest(userId, requestId, clliCode, diagnosticCode, LPORT_MIRRORING_CHECK, list);
122     }
123
124
125     protected String buildRequest(String userId, String requestId, Optional<String> clliCode, String diagnosticCode,
126             String operationType, List<AAIParamList> paramList) throws JsonProcessingException {
127
128         final RequestHdCustom hdCustom = new RequestHdCustom();
129         hdCustom.withRequestUserId(userId).withRequestId(requestId).withRequestClientName(CLIENT_NAME)
130                 .withHealthDiagnosticCode(diagnosticCode).withOperationType(operationType).withAaiParamList(paramList);
131
132         final Input input = new Input();
133         input.setRequestHdCustom(hdCustom);
134         final Body body = new Body();
135         body.setInput(input);
136         final SDNO request = new SDNO();
137         request.withBody(body).withOperation(API_OPERATION_TYPE).withNodeType(NODE_TYPE);
138         if (clliCode.isPresent()) {
139             request.setNodeLoc(clliCode.get());
140         }
141         return this.getJson(request);
142
143     }
144
145     protected String getJson(SDNO obj) throws JsonProcessingException {
146         final ObjectMapper mapper = new ObjectMapper();
147         return mapper.writeValueAsString(obj);
148     }
149
150     protected DmaapPublisher getPublisher() throws FileNotFoundException, IOException {
151         return new SDNOHealthCheckDmaapPublisher();
152     }
153
154     protected DmaapConsumer getConsumer(String requestId) throws FileNotFoundException, IOException {
155         return new SDNOHealthCheckDmaapConsumer(requestId);
156     }
157
158     protected boolean execute(String requestId, String request) throws Exception {
159         final DmaapPublisher publisher = this.getPublisher();
160         publisher.send(request);
161
162         final DmaapConsumer consumer = this.getConsumer(requestId);
163
164         return consumer.consume();
165     }
166
167 }