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