36cdd1b5e7fa7fb3dee399052b0bbcb5b61f5ff7
[ccsdk/apps.git] / sdnr / wireless-transport / code-Carbon-SR1 / apps / devicemanager / impl / src / main / java / org / opendaylight / mwtn / aaiConnector / impl / AaiWebApiClient.java
1 package org.opendaylight.mwtn.aaiConnector.impl;
2
3 import java.io.IOException;
4 import java.util.List;
5 import java.util.Map;
6
7 import javax.annotation.Nonnull;
8
9 import org.opendaylight.mwtn.base.http.BaseHTTPClient;
10 import org.opendaylight.mwtn.base.http.BaseHTTPResponse;
11 import org.slf4j.Logger;
12 import org.slf4j.LoggerFactory;
13
14 public class AaiWebApiClient extends BaseHTTPClient{
15
16         public static class URLParamEncoder {
17
18             public static String encode(String input) {
19                 StringBuilder resultStr = new StringBuilder();
20                 for (char ch : input.toCharArray()) {
21                     if (isUnsafe(ch)) {
22                         resultStr.append('%');
23                         resultStr.append(toHex(ch / 16));
24                         resultStr.append(toHex(ch % 16));
25                     } else {
26                         resultStr.append(ch);
27                     }
28                 }
29                 return resultStr.toString();
30             }
31
32             private static char toHex(int ch) {
33                 return (char) (ch < 10 ? '0' + ch : 'A' + ch - 10);
34             }
35
36             private static boolean isUnsafe(char ch) {
37                 if (ch > 128 || ch < 0)
38                     return true;
39                 return " %$&+,/:;=?@<>#%".indexOf(ch) >= 0;
40             }
41
42         }
43         private static final String pnfJSON_INTERFACE_TEMPLATE = "        {\n" +
44                         "            \"interface-name\": \"@interface@\",\n" +
45                         "            \"speed-value\": \"300\",\n" +
46                         "            \"speed-units\": \"MBit/s\",\n" +
47                         "            \"port-description\": \"Air Interface (MWPS)\",\n" +
48                         "            \"equipment-identifier\": \"@pnfId@-@interface@\",\n" +
49                         "            \"interface-role\": \"Wireless\",\n" +
50                         "            \"interface-type\": \"Air Interface (MWPS)\",\n" +
51                         "            \"resource-version\": \"@model@\",\n" +
52                         "            \"relationship-list\": [\n" +
53                         "                {\n" +
54                         "                    \"related-to\": \"A keyword provided by A&AI to indicate type of node.\",\n" +
55                         "                    \"related-link\": \"URL to the object in A&AI.\",\n" +
56                         "                    \"relationship-data\": [\n" +
57                         "                        {\n" +
58                         "                            \"relationship-key\": \"A keyword provided by A&AI to indicate an attribute.\",\n" +
59                         "                            \"relationship-value\": \"Value of the attribute\"\n" +
60                         "                        }\n" +
61                         "                    ],\n" +
62                         "                    \"related-to-property\": [\n" +
63                         "                        {\n" +
64                         "                            \"property-key\": \"Key part of a key/value pair\",\n" +
65                         "                            \"property-value\": \"Value part of a key/value pair\"\n" +
66                         "                        }\n" +
67                         "                    ]\n" +
68                         "                }\n" +
69                         "            ]\n" +
70                         "        }\n";
71
72         private static final String pnfJSON_TEMPLATE = "{\n" +
73                         "    \"pnf-name\": \"@pnfId@\",\n" +
74                         "    \"pnf-id\": \"@pnfId@\",\n" +
75                         "    \"equip-type\": \"@type@\",\n" +
76                         "    \"equip-model\": \"@model@\",\n" +
77                         "    \"equip-vendor\": \"@vendor@\",\n" +
78                         "    \"ipaddress-v4-oam\": \"@oamIp@\",\n" +
79                         "    \"in-maint\": true,\n" +
80                         "    \"p-interfaces\": @interface-list@\n"+
81                         "}\n" +
82                         "";
83
84         private static String getPnfTemplateFilled(String pnfId,String type,String model,String vendor,String oamIp,List<String> ifaces)
85         {
86                 return pnfJSON_TEMPLATE.replace("@pnfId@",pnfId).
87                                 replace("@type@", type).
88                                 replace("@model@", model).
89                                 replace("@vendor@",vendor).
90                                 replace("@oamIp@", oamIp).
91                                 replace("@interface-list@", getPnfTemplateInterfaceList(pnfId,ifaces,model));
92         }
93         private static String getPnfTemplateInterfaceList(String pnfId,List<String> ifaces,String model) {
94                 String s="[";
95                 if(ifaces!=null)
96                 {
97                         if(ifaces.size()>0)
98                         {
99                                 s+=pnfJSON_INTERFACE_TEMPLATE.replace("@interface@", ifaces.get(0));
100                         }
101                         for(int i=1;i<ifaces.size();i++)
102                                 s+=","+pnfJSON_INTERFACE_TEMPLATE.replace("@interface@", ifaces.get(i));
103                 }
104                 s+="]";
105
106                 return s.replace("@pnfId@",pnfId).replace("@model@", model);
107         }
108         private static Logger LOG = LoggerFactory.getLogger(AaiWebApiClient.class);
109
110         private final Map<String, String> headers;
111
112         public AaiWebApiClient(String baseUrl, Map<String, String> headers, boolean trustAllCerts) {
113                 this(baseUrl,headers,trustAllCerts,null,null);
114         }
115
116         public AaiWebApiClient(String baseUrl, Map<String, String> headers, boolean trustAllCerts, String certFilename,
117                         String passphrase) {
118                 super(baseUrl, trustAllCerts, certFilename, passphrase, BaseHTTPClient.SSLCERT_PCKS);
119                 this.headers = headers;
120         }
121
122
123         public boolean pnfCreate(String pnfId,String type,String model,String vendor,String oamIp,List<String> ifaces)
124         {
125                 LOG.debug("registering "+pnfId +"(type="+type+", model="+model+", vendor="+vendor+",ip="+oamIp+")");
126                 Map<String,String> headers = this.headers;
127                 headers.put("Content-Type","application/json");
128                 headers.put("Accept","application/json");
129                 BaseHTTPResponse response=null;
130                 try {
131                         String uri="network/pnfs/pnf/"+URLParamEncoder.encode( pnfId);
132                         String message=getPnfTemplateFilled(pnfId, type, model, vendor, oamIp, ifaces);
133                         response=this.sendRequest(uri,"PUT",message,headers);
134                         LOG.debug("finished with responsecode "+response.code);
135                 } catch (IOException e) {
136                         LOG.warn("problem registering "+pnfId+": "+e.getMessage());
137                 }
138                 return response!=null?response.code==200:false;
139         }
140
141         public boolean pnfDelete(String pnfId)
142         {
143                 LOG.debug("unregistering "+pnfId);
144                 Map<String,String> headers = this.headers;
145                 headers.put("Content-Type","application/json");
146                 headers.put("Accept","application/json");
147                 BaseHTTPResponse response=null;
148                 try {
149                         String uri="network/pnfs/pnf/"+URLParamEncoder.encode( pnfId );
150                         response=this.sendRequest(uri,
151                                         "DELETE",
152                                         "",headers);
153                         LOG.debug("finished with responsecode "+response.code);
154                 } catch (IOException e) {
155                         LOG.warn("problem unregistering "+pnfId+": "+e.getMessage());
156                 }
157                 return response!=null?response.code==200:false;
158         }
159         public @Nonnull BaseHTTPResponse pnfCheckIfExists(String pnfId) {
160
161                 BaseHTTPResponse response=null;
162                 LOG.debug("check for "+pnfId);
163                 Map<String,String> headers = this.headers;
164                 headers.put("Content-Type","application/json");
165                 headers.put("Accept","application/json");
166                 try {
167                         String uri="network/pnfs/pnf/"+URLParamEncoder.encode( pnfId );
168                         response=this.sendRequest(uri,
169                                         "GET",
170                                         "",headers);
171                         LOG.debug("finished with responsecode "+response.code);
172                 } catch (IOException e) {
173                         LOG.warn("problem checking "+pnfId+": "+e.getMessage());
174                         response = BaseHTTPResponse.UNKNOWN;
175                 }
176
177                 return response;
178         }
179
180
181
182 }