1d70077b91b64937446aca0c4c0b62f6d5906ab6
[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  * =================================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software distributed under the License
13  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14  * or implied. See the License for the specific language governing permissions and limitations under
15  * the License.
16  * ============LICENSE_END==========================================================================
17  */
18
19 package org.onap.ccsdk.features.sdnr.wt.mountpointregistrar.impl;
20
21 import com.fasterxml.jackson.databind.JsonNode;
22 import com.fasterxml.jackson.databind.ObjectMapper;
23 import java.io.IOException;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 public class DMaaPPNFRegVESMsgConsumer extends DMaaPVESMsgConsumerImpl {
29
30     private static final Logger LOG = LoggerFactory.getLogger(DMaaPPNFRegVESMsgConsumer.class);
31     private static final String DEFAULT_PROTOCOL = "SSH";
32     private static final String DEFAULT_PORT = "17830";
33     private static final String DEFAULT_USERNAME = "netconf";
34     private static final String DEFAULT_PASSWORD = "netconf";
35     private static final String DEFAULT_SDNRUSER = "admin";
36     private static final String DEFAULT_SDNRPASSWD = "admin";
37
38     private final GeneralConfig generalConfig;
39
40     public DMaaPPNFRegVESMsgConsumer(GeneralConfig generalConfig) {
41         this.generalConfig = generalConfig;
42     }
43
44     @Override
45     public void processMsg(String msg) {
46         LOG.debug("Message from DMaaP topic is - {} ", msg);
47         @Nullable
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         ObjectMapper oMapper = new ObjectMapper();
61         JsonNode dmaapMessageRootNode;
62         try {
63             dmaapMessageRootNode = oMapper.readTree(msg);
64             pnfId = dmaapMessageRootNode.at("/event/commonEventHeader/sourceName").textValue();
65             pnfIPAddress = getPNFIPAddress(dmaapMessageRootNode);
66             pnfCommProtocol =
67                     dmaapMessageRootNode.at("/event/pnfRegistrationFields/additionalFields/protocol").textValue();
68             pnfCommPort = dmaapMessageRootNode.at("/event/pnfRegistrationFields/additionalFields/oamPort").textValue();
69             if (pnfCommProtocol != null) {
70                 if (pnfCommProtocol.equalsIgnoreCase("TLS")) {
71                     // Read username and keyId
72                     pnfKeyId =
73                             dmaapMessageRootNode.at("/event/pnfRegistrationFields/additionalFields/keyId").textValue();
74                     pnfUsername = dmaapMessageRootNode.at("/event/pnfRegistrationFields/additionalFields/username")
75                             .textValue();
76                 } else if (pnfCommProtocol.equalsIgnoreCase("SSH")) {
77                     // Read username and password
78                     pnfUsername = dmaapMessageRootNode.at("/event/pnfRegistrationFields/additionalFields/username")
79                             .textValue();
80                     pnfPasswd = dmaapMessageRootNode.at("/event/pnfRegistrationFields/additionalFields/password")
81                             .textValue();
82                 } else {
83                     // log warning - Unknown protocol
84                     LOG.warn("Only SSH and TLS protocols supported. Protocol specified in VES message is - {}",
85                             pnfCommProtocol, ". Defaulting to SSH");
86                     pnfCommProtocol = DEFAULT_PROTOCOL;
87                     pnfCommPort = DEFAULT_PORT;
88                     pnfUsername = DEFAULT_USERNAME;
89                     pnfPasswd = DEFAULT_PASSWORD;
90                 }
91             } else {
92                 LOG.warn("Protocol not specified in VES message, Defaulting to SSH");
93                 pnfCommProtocol = DEFAULT_PROTOCOL;
94                 pnfCommPort = DEFAULT_PORT;
95                 pnfUsername = DEFAULT_USERNAME;
96                 pnfPasswd = DEFAULT_PASSWORD;
97             }
98
99             LOG.debug(
100                     "PNF Fields - ID - {} : IP Address - {} : Protocol - {} : TLS Key ID - {} : User - {} : Port - {}",
101                     pnfId, pnfIPAddress, pnfCommProtocol, pnfKeyId, pnfUsername, pnfCommPort);
102
103             String baseUrl = getBaseUrl();
104             String sdnrUser = getSDNRUser();
105             String sdnrPasswd = getSDNRPasswd();
106
107             PNFMountPointClient mountpointClient = getPNFMountPointClient(baseUrl);
108             LOG.debug("Setting RESTConf Authorization values - {} : {}", sdnrUser, sdnrPasswd);
109             mountpointClient.setAuthorization(sdnrUser, sdnrPasswd);
110
111             if ((null != pnfId) && null != pnfIPAddress && (null != pnfCommProtocol) && (null != pnfUsername)
112                     && (null != pnfCommPort)) {
113                 mountpointClient.pnfMountPointCreate(pnfId, pnfIPAddress, pnfCommProtocol, pnfKeyId, pnfUsername,
114                         pnfPasswd, pnfCommPort);
115             } else {
116                 LOG.warn(
117                         "One of the mandatory fields has a null value - pnfId = {} : pnfIPAddress = {} : pnfCommProtocol = {} : pnfUsername {} : "
118                                 + "pnfCommPort {}",
119                         pnfId, pnfIPAddress, pnfCommProtocol, pnfUsername, pnfCommPort,
120                         "- not invoking mountpoint creation");
121             }
122         } catch (IOException e) {
123             LOG.info("Cannot parse json object, ignoring the received PNF Registration VES Message. Reason: {}",
124                     e.getMessage());
125         }
126     }
127
128     private String getPNFIPAddress(JsonNode dmaapMessageRootNode) {
129         String ipAddress = dmaapMessageRootNode.at("/event/pnfRegistrationFields/oamV6IpAddress").textValue();
130         if (ipAddress != null && ipAddress != "")
131             return ipAddress;
132
133         ipAddress = dmaapMessageRootNode.at("/event/pnfRegistrationFields/oamV4IpAddress").textValue();
134         if (ipAddress != null && ipAddress != "")
135             return ipAddress;
136
137         return null;
138     }
139
140     public String getBaseUrl() {
141         return generalConfig.getBaseUrl();
142     }
143
144     public String getSDNRUser() {
145         return generalConfig.getSDNRUser() != null ? generalConfig.getSDNRUser() : DEFAULT_SDNRUSER;
146     }
147
148     public String getSDNRPasswd() {
149         return generalConfig.getSDNRPasswd() != null ? generalConfig.getSDNRPasswd() : DEFAULT_SDNRPASSWD;
150     }
151
152     private PNFMountPointClient getPNFMountPointClient(String baseUrl) {
153         return new PNFMountPointClient(baseUrl);
154     }
155 }