2 * ============LICENSE_START========================================================================
3 * ONAP : ccsdk feature sdnr wt
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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
16 * ============LICENSE_END==========================================================================
18 package org.onap.ccsdk.features.sdnr.wt.devicemanager.aaiconnector.impl;
20 import java.io.IOException;
21 import java.util.HashMap;
22 import java.util.List;
25 import org.onap.ccsdk.features.sdnr.wt.common.http.BaseHTTPClient;
26 import org.onap.ccsdk.features.sdnr.wt.common.http.BaseHTTPResponse;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 public class AaiWebApiClient extends BaseHTTPClient {
32 private static Logger LOG = LoggerFactory.getLogger(AaiWebApiClient.class);
33 private static final String PNF_JSON_INTERFACE_TEMPLATE = " {\n"
34 + " \"interface-name\": \"@interface@\",\n" + " \"speed-value\": \"300\",\n"
35 + " \"speed-units\": \"MBit/s\",\n"
36 + " \"port-description\": \"Air Interface (MWPS)\",\n"
37 + " \"equipment-identifier\": \"@pnfId@-@interface@\",\n"
38 + " \"interface-role\": \"Wireless\",\n"
39 + " \"interface-type\": \"Air Interface (MWPS)\",\n"
40 + " \"resource-version\": \"@model@\",\n" + " \"relationship-list\": [\n"
42 + " \"related-to\": \"A keyword provided by A&AI to indicate type of node.\",\n"
43 + " \"related-link\": \"URL to the object in A&AI.\",\n"
44 + " \"relationship-data\": [\n" + " {\n"
45 + " \"relationship-key\": \"A keyword provided by A&AI to indicate an attribute.\",\n"
46 + " \"relationship-value\": \"Value of the attribute\"\n"
48 + " \"related-to-property\": [\n" + " {\n"
49 + " \"property-key\": \"Key part of a key/value pair\",\n"
50 + " \"property-value\": \"Value part of a key/value pair\"\n"
51 + " }\n" + " ]\n" + " }\n" + " ]\n"
53 private static final String PNF_JSON_TEMPLATE = "{\n" + " \"pnf-name\": \"@pnfId@\",\n"
54 + " \"pnf-id\": \"@pnfId@\",\n" + " \"equip-type\": \"@type@\",\n"
55 + " \"equip-model\": \"@model@\",\n" + " \"equip-vendor\": \"@vendor@\",\n"
56 + " \"ipaddress-v4-oam\": \"@oamIp@\",\n" + " \"in-maint\": false,\n"
57 + " \"prov-status\":\"PROV\",\n" + " \"p-interfaces\": @interface-list@\n" + "}\n" + "";
58 private static final String PNF_URI = "network/pnfs/pnf/";
59 private static final String EMPTY_MESSAGE = "";
61 private final Map<String, String> headerMap;
64 public AaiWebApiClient(String baseUrl, Map<String, String> headers, boolean trustAllCerts) {
65 this(baseUrl, headers, trustAllCerts, null, null);
68 public AaiWebApiClient(String baseUrl, Map<String, String> headers, boolean trustAllCerts, String certFilename,
70 super(baseUrl, trustAllCerts, certFilename, passphrase, BaseHTTPClient.getSslCertPcks());
72 this.headerMap = new HashMap<>();
73 this.headerMap.putAll(headers);
74 this.headerMap.put("Content-Type", "application/json");
75 this.headerMap.put("Accept", "application/json");
79 * Create and specify defition parametrs of pnf
83 * @param vendor vendor
85 * @param ifaces interfaces
86 * @return true if http response code was 200 or false if not.
88 public boolean pnfCreate(String pnfId, String type, String model, String vendor, String oamIp,
89 List<String> ifaces) {
90 LOG.debug("registering {} (type={}, model={}, vendor={},ip={})", pnfId, type, model, vendor, oamIp);
91 String message = getPnfTemplateFilled(pnfId, type, model, vendor, oamIp, ifaces);
92 return pnfRequest(pnfId, "PUT", message) == 200;
98 * @return true if http response code was 200 or false if not.
100 public boolean pnfDelete(String pnfId) {
101 LOG.debug("unregistering {}", pnfId);
102 return pnfRequest(pnfId, "DELETE", EMPTY_MESSAGE) == 200;
106 * Send registration request
108 * @return error accoring to http response code or -1
110 public int pnfCheckIfExists(String pnfId) {
111 LOG.debug("check for {}", pnfId);
112 return pnfRequest(pnfId, "GET", EMPTY_MESSAGE);
119 private int pnfRequest(String pnfId, String method, String message) {
120 BaseHTTPResponse response;
122 String uri = PNF_URI + URLParamEncoder.encode(pnfId);
123 response = this.sendRequest(uri, method, message, headerMap);
124 LOG.debug("finished with responsecode {}", response.code);
125 return response.code;
126 } catch (IOException e) {
127 LOG.warn("problem registering {} : {}", pnfId, e.getMessage());
133 private static String getPnfTemplateFilled(String pnfId, String type, String model, String vendor, String oamIp,
134 List<String> ifaces) {
135 return PNF_JSON_TEMPLATE.replace("@pnfId@", pnfId).replace("@type@", type).replace("@model@", model)
136 .replace("@vendor@", vendor).replace("@oamIp@", oamIp)
137 .replace("@interface-list@", getPnfTemplateInterfaceList(pnfId, ifaces, model));
140 private static String getPnfTemplateInterfaceList(String pnfId, List<String> ifaces, String model) {
141 StringBuffer s = new StringBuffer();
143 if (ifaces != null) {
144 for (int i = 0; i < ifaces.size(); i++) {
148 s.append(PNF_JSON_INTERFACE_TEMPLATE.replace("@interface@", ifaces.get(i)));
153 return s.toString().replace("@pnfId@", pnfId).replace("@model@", model);