6ce4cff670400caacd25eec0e7a43bbc060ff6ff
[ccsdk/features.git] /
1 /*******************************************************************************
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
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 package org.onap.ccsdk.features.sdnr.wt.devicemanager.aaiconnector.impl;
19
20 import java.io.IOException;
21 import java.util.HashMap;
22 import java.util.List;
23 import java.util.Map;
24 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.http.BaseHTTPClient;
25 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.http.BaseHTTPResponse;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 public class AaiWebApiClient extends BaseHTTPClient {
30
31     private static Logger LOG = LoggerFactory.getLogger(AaiWebApiClient.class);
32     private static final String PNF_JSON_INTERFACE_TEMPLATE = "        {\n"
33             + "            \"interface-name\": \"@interface@\",\n" + "            \"speed-value\": \"300\",\n"
34             + "            \"speed-units\": \"MBit/s\",\n"
35             + "            \"port-description\": \"Air Interface (MWPS)\",\n"
36             + "            \"equipment-identifier\": \"@pnfId@-@interface@\",\n"
37             + "            \"interface-role\": \"Wireless\",\n"
38             + "            \"interface-type\": \"Air Interface (MWPS)\",\n"
39             + "            \"resource-version\": \"@model@\",\n" + "            \"relationship-list\": [\n"
40             + "                {\n"
41             + "                    \"related-to\": \"A keyword provided by A&AI to indicate type of node.\",\n"
42             + "                    \"related-link\": \"URL to the object in A&AI.\",\n"
43             + "                    \"relationship-data\": [\n" + "                        {\n"
44             + "                            \"relationship-key\": \"A keyword provided by A&AI to indicate an attribute.\",\n"
45             + "                            \"relationship-value\": \"Value of the attribute\"\n"
46             + "                        }\n" + "                    ],\n"
47             + "                    \"related-to-property\": [\n" + "                        {\n"
48             + "                            \"property-key\": \"Key part of a key/value pair\",\n"
49             + "                            \"property-value\": \"Value part of a key/value pair\"\n"
50             + "                        }\n" + "                    ]\n" + "                }\n" + "            ]\n"
51             + "        }\n";
52     private static final String PNF_JSON_TEMPLATE = "{\n" + "    \"pnf-name\": \"@pnfId@\",\n"
53             + "    \"pnf-id\": \"@pnfId@\",\n" + "    \"equip-type\": \"@type@\",\n"
54             + "    \"equip-model\": \"@model@\",\n" + "    \"equip-vendor\": \"@vendor@\",\n"
55             + "    \"ipaddress-v4-oam\": \"@oamIp@\",\n" + "    \"in-maint\": false,\n"
56             + "    \"prov-status\":\"PROV\",\n" + "    \"p-interfaces\": @interface-list@\n" + "}\n" + "";
57     private static final String PNF_URI = "network/pnfs/pnf/";
58     private static final String EMPTY_MESSAGE = "";
59
60     private final Map<String, String> headerMap;
61
62
63     public AaiWebApiClient(String baseUrl, Map<String, String> headers, boolean trustAllCerts) {
64         this(baseUrl, headers, trustAllCerts, null, null);
65     }
66
67     public AaiWebApiClient(String baseUrl, Map<String, String> headers, boolean trustAllCerts, String certFilename,
68             String passphrase) {
69         super(baseUrl, trustAllCerts, certFilename, passphrase, BaseHTTPClient.getSslCertPcks());
70
71         this.headerMap = new HashMap<>();
72         this.headerMap.putAll(headers);
73         this.headerMap.put("Content-Type", "application/json");
74         this.headerMap.put("Accept", "application/json");
75     }
76
77     /**
78      * Create and specify defition parametrs of pnf
79      * @param pnfId name
80      * @param type type
81      * @param model model
82      * @param vendor vendor
83      * @param oamIp ip
84      * @param ifaces interfaces
85      * @return true if http response code was 200 or false if not.
86      */
87     public boolean pnfCreate(String pnfId, String type, String model, String vendor, String oamIp,
88             List<String> ifaces) {
89         LOG.debug("registering {} (type={}, model={}, vendor={},ip={})", pnfId, type, model, vendor, oamIp);
90         String message = getPnfTemplateFilled(pnfId, type, model, vendor, oamIp, ifaces);
91         return pnfRequest(pnfId, "PUT", message) == 200;
92     }
93
94     /**
95      * Unregister
96      * @param pnfId name
97      * @return true if http response code was 200 or false if not.
98      */
99     public boolean pnfDelete(String pnfId) {
100         LOG.debug("unregistering {}", pnfId);
101         return pnfRequest(pnfId, "DELETE", EMPTY_MESSAGE) == 200;
102     }
103
104     /**
105      * Send registration request
106      * @param pnfId name
107      * @return error accoring to http response code or -1
108      */
109     public int pnfCheckIfExists(String pnfId) {
110         LOG.debug("check for {}", pnfId);
111         return pnfRequest(pnfId, "GET", EMPTY_MESSAGE);
112     }
113
114     /*
115      * Private classes
116      */
117
118     private int pnfRequest(String pnfId, String method, String message) {
119         BaseHTTPResponse response;
120         try {
121             String uri = PNF_URI + URLParamEncoder.encode(pnfId);
122             response = this.sendRequest(uri, method, message, headerMap);
123             LOG.debug("finished with responsecode {}", response.code);
124             return response.code;
125         } catch (IOException e) {
126             LOG.warn("problem registering {} : {}", pnfId, e.getMessage());
127             return -1;
128         }
129     }
130
131
132     private static String getPnfTemplateFilled(String pnfId, String type, String model, String vendor, String oamIp,
133             List<String> ifaces) {
134         return PNF_JSON_TEMPLATE.replace("@pnfId@", pnfId).replace("@type@", type).replace("@model@", model)
135                 .replace("@vendor@", vendor).replace("@oamIp@", oamIp)
136                 .replace("@interface-list@", getPnfTemplateInterfaceList(pnfId, ifaces, model));
137     }
138
139     private static String getPnfTemplateInterfaceList(String pnfId, List<String> ifaces, String model) {
140         StringBuffer s = new StringBuffer();
141         s.append("[");
142         if (ifaces != null) {
143             for (int i = 0; i < ifaces.size(); i++) {
144                 if (i > 0) {
145                     s.append(",");
146                 }
147                 s.append(PNF_JSON_INTERFACE_TEMPLATE.replace("@interface@", ifaces.get(i)));
148             }
149         }
150         s.append("]");
151
152         return s.toString().replace("@pnfId@", pnfId).replace("@model@", model);
153     }
154
155
156
157 }