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.eclipse.jdt.annotation.Nullable;
27 import org.onap.ccsdk.features.sdnr.wt.mountpointregistrar.config.GeneralConfig;
28 import org.onap.ccsdk.features.sdnr.wt.mountpointregistrar.impl.StrimziKafkaVESMsgConsumerImpl;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 public class StrimziKafkaPNFRegVESMsgConsumer extends StrimziKafkaVESMsgConsumerImpl {
33
34     private static final Logger LOG = LoggerFactory.getLogger(StrimziKafkaPNFRegVESMsgConsumer.class);
35     private static final String DEFAULT_PROTOCOL = "SSH";
36     private static final String DEFAULT_PORT = "17830";
37     private static final String DEFAULT_USERNAME = "netconf";
38     private static final String DEFAULT_PASSWORD = "netconf";
39
40
41     public StrimziKafkaPNFRegVESMsgConsumer(GeneralConfig generalConfig) {
42         super(generalConfig);
43     }
44
45     @Override
46     public void processMsg(String msg) {
47         LOG.debug("Message from Kafka topic is - {} ", msg);
48         String pnfId;
49         String pnfIPAddress;
50         @Nullable
51         String pnfCommProtocol;
52         @Nullable
53         String pnfCommPort;
54         @Nullable
55         String pnfKeyId = null;
56         @Nullable
57         String pnfUsername;
58         @Nullable
59         String pnfPasswd = null;
60         String reportingEntityName;
61         ObjectMapper oMapper = new ObjectMapper();
62         JsonNode sKafkaMessageRootNode;
63         try {
64             sKafkaMessageRootNode = oMapper.readTree(msg);
65             reportingEntityName = sKafkaMessageRootNode.at("/event/commonEventHeader/reportingEntityName").textValue();
66             if (reportingEntityName.equals("ONAP SDN-R")) {
67                 LOG.info(
68                         "VES PNF Registration message generated by SDNR, hence no need to process any further; Ignoring the received message");
69                 return;
70             }
71
72             pnfId = sKafkaMessageRootNode.at("/event/commonEventHeader/sourceName").textValue();
73             pnfIPAddress = getPNFIPAddress(sKafkaMessageRootNode);
74             pnfCommProtocol =
75                     sKafkaMessageRootNode.at("/event/pnfRegistrationFields/additionalFields/protocol").textValue();
76             pnfCommPort = sKafkaMessageRootNode.at("/event/pnfRegistrationFields/additionalFields/oamPort").textValue();
77             if (pnfCommProtocol != null) {
78                 if (pnfCommProtocol.equalsIgnoreCase("TLS")) {
79                     // Read username and keyId
80                     pnfKeyId =
81                             sKafkaMessageRootNode.at("/event/pnfRegistrationFields/additionalFields/keyId").textValue();
82                     pnfUsername = sKafkaMessageRootNode.at("/event/pnfRegistrationFields/additionalFields/username")
83                             .textValue();
84                 } else if (pnfCommProtocol.equalsIgnoreCase("SSH")) {
85                     // Read username and password
86                     pnfUsername = sKafkaMessageRootNode.at("/event/pnfRegistrationFields/additionalFields/username")
87                             .textValue();
88                     pnfPasswd = sKafkaMessageRootNode.at("/event/pnfRegistrationFields/additionalFields/password")
89                             .textValue();
90                 } else {
91                     // log warning - Unknown protocol
92                     LOG.warn("Only SSH and TLS protocols supported. Protocol specified in VES message is - {}",
93                             pnfCommProtocol, ". Defaulting to SSH");
94                     pnfCommProtocol = DEFAULT_PROTOCOL;
95                     pnfCommPort = DEFAULT_PORT;
96                     pnfUsername = DEFAULT_USERNAME;
97                     pnfPasswd = DEFAULT_PASSWORD;
98                 }
99             } else {
100                 LOG.warn("Protocol not specified in VES message, Defaulting to SSH");
101                 pnfCommProtocol = DEFAULT_PROTOCOL;
102                 pnfCommPort = DEFAULT_PORT;
103                 pnfUsername = DEFAULT_USERNAME;
104                 pnfPasswd = DEFAULT_PASSWORD;
105             }
106
107             LOG.debug(
108                     "PNF Fields - ID - {} : IP Address - {} : Protocol - {} : TLS Key ID - {} : User - {} : Port - {}",
109                     pnfId, pnfIPAddress, pnfCommProtocol, pnfKeyId, pnfUsername, pnfCommPort);
110
111             String baseUrl = getBaseUrl();
112             String sdnrUser = getSDNRUser();
113             String sdnrPasswd = getSDNRPasswd();
114
115             if (hasNullInRequiredField(pnfId, pnfIPAddress, pnfCommPort, pnfCommProtocol, pnfUsername)) {
116                 LOG.warn("One of the mandatory fields has a null value - pnfId = {} : pnfIPAddress = {} : " +
117                          "pnfCommProtocol = {} : pnfUsername {} : pnfCommPort {} - not invoking mountpoint creation",
118                           pnfId, pnfIPAddress, pnfCommProtocol, pnfUsername, pnfCommPort);
119                 return;
120             }
121
122             Map<String, String> payloadMap = PNFMountPointClient.createPNFNotificationPayloadMap(pnfId, pnfIPAddress,
123                     pnfCommPort, pnfCommProtocol, pnfUsername, pnfPasswd, pnfKeyId);
124
125             PNFMountPointClient mountPointClient = new PNFMountPointClient(baseUrl);
126             LOG.debug("Setting RESTConf Authorization values - {} : {}", sdnrUser, sdnrPasswd);
127             mountPointClient.setAuthorization(sdnrUser, sdnrPasswd);
128             String message = mountPointClient.prepareMessageFromPayloadMap(payloadMap);
129             mountPointClient.sendNotification(message);
130
131         } catch (IOException e) {
132             LOG.info("Cannot parse json object, ignoring the received PNF Registration VES Message. Reason: {}",
133                     e.getMessage());
134         }
135     }
136
137     private boolean hasNullInRequiredField(String pnfId, String pnfIPAddress, String pnfCommPort,
138                                            String pnfCommProtocol, String pnfUsername) {
139
140         return pnfId == null || pnfIPAddress == null || pnfCommProtocol == null ||
141                 pnfCommPort == null || pnfUsername == null;
142     }
143
144     private String getPNFIPAddress(JsonNode sKafkaMessageRootNode) {
145         String ipAddress = sKafkaMessageRootNode.at("/event/pnfRegistrationFields/oamV6IpAddress").textValue();
146         if (ipAddress != null && ipAddress != "")
147             return ipAddress;
148
149         ipAddress = sKafkaMessageRootNode.at("/event/pnfRegistrationFields/oamV4IpAddress").textValue();
150         if (ipAddress != null && ipAddress != "")
151             return ipAddress;
152
153         return null;
154     }
155
156 }