5a366a83910d44234c746ced04a19fa94beaf817
[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.common.http.BaseHTTPClient;
25 import org.onap.ccsdk.features.sdnr.wt.common.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     // @formatter:off
33     private static final String PNF_JSON_INTERFACE_TEMPLATE =
34               "        {\n"
35             + "            \"interface-name\": \"@interface@\",\n"
36             + "            \"speed-value\": \"300\",\n"
37             + "            \"speed-units\": \"MBit/s\",\n"
38             + "            \"port-description\": \"Air Interface (MWPS)\",\n"
39             + "            \"equipment-identifier\": \"@pnfId@-@interface@\",\n"
40             + "            \"interface-role\": \"Wireless\",\n"
41             + "            \"interface-type\": \"Air Interface (MWPS)\"\n"
42             + "        }\n";
43     private static final String PNF_JSON_TEMPLATE = "{\n"
44             + "    \"pnf-name\": \"@pnfId@\",\n"
45             + "    \"pnf-id\": \"@pnfId@\",\n"
46             + "    \"equip-type\": \"@type@\",\n"
47             + "    \"equip-model\": \"@model@\",\n"
48             + "    \"equip-vendor\": \"@vendor@\",\n"
49             + "    \"ipaddress-v4-oam\": \"@oamIp@\",\n"
50             + "    \"in-maint\": false,\n"
51             + "    \"prov-status\":\"PROV\",\n"
52             + "    \"p-interfaces\": @interface-list@\n"
53             + "}\n" + "";
54     // @formatter:on
55     private static final String PNF_URI = "network/pnfs/pnf/";
56     private static final String EMPTY_MESSAGE = "";
57
58     private final Map<String, String> headerMap;
59
60
61     public AaiWebApiClient(String baseUrl, Map<String, String> headers, boolean trustAllCerts) {
62         this(baseUrl, headers, trustAllCerts, null, null);
63     }
64
65     public AaiWebApiClient(String baseUrl, Map<String, String> headers, boolean trustAllCerts, String certFilename,
66             String passphrase) {
67         super(baseUrl, trustAllCerts, certFilename, passphrase, BaseHTTPClient.getSslCertPcks());
68
69         this.headerMap = new HashMap<>();
70         this.headerMap.putAll(headers);
71         this.headerMap.put("Content-Type", "application/json");
72         this.headerMap.put("Accept", "application/json");
73     }
74
75     /**
76      * Create and specify defition parametrs of pnf
77      *
78      * @param pnfId name
79      * @param type type
80      * @param model model
81      * @param vendor vendor
82      * @param oamIp ip
83      * @param ifaces interfaces
84      * @return true if http response code was 200 or false if not.
85      */
86     public boolean pnfCreate(String pnfId, String type, String model, String vendor, String oamIp,
87             List<String> ifaces) {
88         LOG.debug("registering {} (type={}, model={}, vendor={},ip={})", pnfId, type, model, vendor, oamIp);
89         String message = getPnfTemplateFilled(pnfId, type, model, vendor, oamIp, ifaces);
90         return pnfRequest(pnfId, "PUT", message) == 200;
91     }
92
93     /**
94      * Unregister
95      *
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      *
107      * @param pnfId name
108      * @return error accoring to http response code or -1
109      */
110     public int pnfCheckIfExists(String pnfId) {
111         LOG.debug("check for {}", pnfId);
112         return pnfRequest(pnfId, "GET", EMPTY_MESSAGE);
113     }
114
115     /*
116      * Private classes
117      */
118
119     private int pnfRequest(String pnfId, String method, String message) {
120         BaseHTTPResponse response;
121         try {
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());
128             return -1;
129         }
130     }
131
132
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));
138     }
139
140     private static String getPnfTemplateInterfaceList(String pnfId, List<String> ifaces, String model) {
141         StringBuffer s = new StringBuffer();
142         s.append("[");
143         if (ifaces != null) {
144             for (int i = 0; i < ifaces.size(); i++) {
145                 if (i > 0) {
146                     s.append(",");
147                 }
148                 s.append(PNF_JSON_INTERFACE_TEMPLATE.replace("@interface@", ifaces.get(i)));
149             }
150         }
151         s.append("]");
152
153         return s.toString().replace("@pnfId@", pnfId).replace("@model@", model);
154     }
155
156
157
158 }