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