Merge "fix oauth code"
[ccsdk/features.git] / sdnr / wt / mountpoint-registrar / provider / src / main / java / org / onap / ccsdk / features / sdnr / wt / mountpointregistrar / vesdomain / pnfreg / StrimziKafkaPNFRegVESMsgConsumer.java
1 /*
2  * ============LICENSE_START========================================================================
3  * ONAP : ccsdk feature sdnr wt mountpoint-registrar
4  * =================================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6  * Copyright (C) 2021 Samsung Electronics Intellectual Property. All rights reserved.
7  * =================================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
9  * in compliance with the License. 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 distributed under the License
14  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
15  * or implied. See the License for the specific language governing permissions and limitations under
16  * the License.
17  * ============LICENSE_END==========================================================================
18  */
19
20 package org.onap.ccsdk.features.sdnr.wt.mountpointregistrar.vesdomain.pnfreg;
21
22 import com.fasterxml.jackson.databind.JsonNode;
23 import com.fasterxml.jackson.databind.ObjectMapper;
24 import java.io.IOException;
25 import java.util.Map;
26 import org.apache.kafka.clients.admin.Admin;
27 import org.eclipse.jdt.annotation.Nullable;
28 import org.onap.ccsdk.features.sdnr.wt.mountpointregistrar.config.GeneralConfig;
29 import org.onap.ccsdk.features.sdnr.wt.mountpointregistrar.impl.StrimziKafkaVESMsgConsumerImpl;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 public class StrimziKafkaPNFRegVESMsgConsumer extends StrimziKafkaVESMsgConsumerImpl {
34
35     private static final Logger LOG = LoggerFactory.getLogger(StrimziKafkaPNFRegVESMsgConsumer.class);
36     private static final String DEFAULT_PROTOCOL = "SSH";
37     private static final String DEFAULT_PORT = "17830";
38     private static final String DEFAULT_USERNAME = "netconf";
39     private static final String DEFAULT_PASSWORD = "netconf";
40
41
42     public StrimziKafkaPNFRegVESMsgConsumer(GeneralConfig generalConfig, Admin kafkaAdminClient) {
43         super(generalConfig, kafkaAdminClient);
44     }
45
46     @Override
47     public void processMsg(String msg) {
48         LOG.debug("Message from Kafka topic is - {} ", msg);
49         String pnfId;
50         String pnfIPAddress;
51         @Nullable
52         String pnfCommProtocol;
53         @Nullable
54         String pnfCommPort;
55         @Nullable
56         String pnfKeyId = null;
57         @Nullable
58         String pnfUsername;
59         @Nullable
60         String pnfPasswd = null;
61         String reportingEntityName;
62         ObjectMapper oMapper = new ObjectMapper();
63         JsonNode sKafkaMessageRootNode;
64         try {
65             sKafkaMessageRootNode = oMapper.readTree(msg);
66             reportingEntityName = sKafkaMessageRootNode.at("/event/commonEventHeader/reportingEntityName").textValue();
67             if (reportingEntityName.equals("ONAP SDN-R")) {
68                 LOG.info(
69                         "VES PNF Registration message generated by SDNR, hence no need to process any further; Ignoring the received message");
70                 return;
71             }
72
73             pnfId = sKafkaMessageRootNode.at("/event/commonEventHeader/sourceName").textValue();
74             pnfIPAddress = getPNFIPAddress(sKafkaMessageRootNode);
75             pnfCommProtocol =
76                     sKafkaMessageRootNode.at("/event/pnfRegistrationFields/additionalFields/protocol").textValue();
77             pnfCommPort = sKafkaMessageRootNode.at("/event/pnfRegistrationFields/additionalFields/oamPort").textValue();
78             if (pnfCommProtocol != null) {
79                 if (pnfCommProtocol.equalsIgnoreCase("TLS")) {
80                     // Read username and keyId
81                     pnfKeyId =
82                             sKafkaMessageRootNode.at("/event/pnfRegistrationFields/additionalFields/keyId").textValue();
83                     pnfUsername = sKafkaMessageRootNode.at("/event/pnfRegistrationFields/additionalFields/username")
84                             .textValue();
85                 } else if (pnfCommProtocol.equalsIgnoreCase("SSH")) {
86                     // Read username and password
87                     pnfUsername = sKafkaMessageRootNode.at("/event/pnfRegistrationFields/additionalFields/username")
88                             .textValue();
89                     pnfPasswd = sKafkaMessageRootNode.at("/event/pnfRegistrationFields/additionalFields/password")
90                             .textValue();
91                 } else {
92                     // log warning - Unknown protocol
93                     LOG.warn("Only SSH and TLS protocols supported. Protocol specified in VES message is - {}",
94                             pnfCommProtocol, ". Defaulting to SSH");
95                     pnfCommProtocol = DEFAULT_PROTOCOL;
96                     pnfCommPort = DEFAULT_PORT;
97                     pnfUsername = DEFAULT_USERNAME;
98                     pnfPasswd = DEFAULT_PASSWORD;
99                 }
100             } else {
101                 LOG.warn("Protocol not specified in VES message, Defaulting to SSH");
102                 pnfCommProtocol = DEFAULT_PROTOCOL;
103                 pnfCommPort = DEFAULT_PORT;
104                 pnfUsername = DEFAULT_USERNAME;
105                 pnfPasswd = DEFAULT_PASSWORD;
106             }
107
108             LOG.debug(
109                     "PNF Fields - ID - {} : IP Address - {} : Protocol - {} : TLS Key ID - {} : User - {} : Port - {}",
110                     pnfId, pnfIPAddress, pnfCommProtocol, pnfKeyId, pnfUsername, pnfCommPort);
111
112             String baseUrl = getBaseUrl();
113             String sdnrUser = getSDNRUser();
114             String sdnrPasswd = getSDNRPasswd();
115
116             if (hasNullInRequiredField(pnfId, pnfIPAddress, pnfCommPort, pnfCommProtocol, pnfUsername)) {
117                 LOG.warn("One of the mandatory fields has a null value - pnfId = {} : pnfIPAddress = {} : " +
118                          "pnfCommProtocol = {} : pnfUsername {} : pnfCommPort {} - not invoking mountpoint creation",
119                           pnfId, pnfIPAddress, pnfCommProtocol, pnfUsername, pnfCommPort);
120                 return;
121             }
122
123             Map<String, String> payloadMap = PNFMountPointClient.createPNFNotificationPayloadMap(pnfId, pnfIPAddress,
124                     pnfCommPort, pnfCommProtocol, pnfUsername, pnfPasswd, pnfKeyId);
125
126             PNFMountPointClient mountPointClient = new PNFMountPointClient(baseUrl);
127             LOG.debug("Setting RESTConf Authorization values - {} : {}", sdnrUser, sdnrPasswd);
128             mountPointClient.setAuthorization(sdnrUser, sdnrPasswd);
129             String message = mountPointClient.prepareMessageFromPayloadMap(payloadMap);
130             mountPointClient.sendNotification(message);
131
132         } catch (IOException e) {
133             LOG.info("Cannot parse json object, ignoring the received PNF Registration VES Message. Reason: {}",
134                     e.getMessage());
135         }
136     }
137
138     private boolean hasNullInRequiredField(String pnfId, String pnfIPAddress, String pnfCommPort,
139                                            String pnfCommProtocol, String pnfUsername) {
140
141         return pnfId == null || pnfIPAddress == null || pnfCommProtocol == null ||
142                 pnfCommPort == null || pnfUsername == null;
143     }
144
145     private String getPNFIPAddress(JsonNode sKafkaMessageRootNode) {
146         String ipAddress = sKafkaMessageRootNode.at("/event/pnfRegistrationFields/oamV6IpAddress").textValue();
147         if (ipAddress != null && ipAddress != "")
148             return ipAddress;
149
150         ipAddress = sKafkaMessageRootNode.at("/event/pnfRegistrationFields/oamV4IpAddress").textValue();
151         if (ipAddress != null && ipAddress != "")
152             return ipAddress;
153
154         return null;
155     }
156
157 }