1 package org.opendaylight.mwtn.aaiConnector.impl;
 
   3 import java.io.IOException;
 
   7 import javax.annotation.Nonnull;
 
   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;
 
  14 public class AaiWebApiClient extends BaseHTTPClient{
 
  16         public static class URLParamEncoder {
 
  18             public static String encode(String input) {
 
  19                 StringBuilder resultStr = new StringBuilder();
 
  20                 for (char ch : input.toCharArray()) {
 
  22                         resultStr.append('%');
 
  23                         resultStr.append(toHex(ch / 16));
 
  24                         resultStr.append(toHex(ch % 16));
 
  29                 return resultStr.toString();
 
  32             private static char toHex(int ch) {
 
  33                 return (char) (ch < 10 ? '0' + ch : 'A' + ch - 10);
 
  36             private static boolean isUnsafe(char ch) {
 
  37                 if (ch > 128 || ch < 0)
 
  39                 return " %$&+,/:;=?@<>#%".indexOf(ch) >= 0;
 
  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" +
 
  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" +
 
  58                         "                            \"relationship-key\": \"A keyword provided by A&AI to indicate an attribute.\",\n" +
 
  59                         "                            \"relationship-value\": \"Value of the attribute\"\n" +
 
  62                         "                    \"related-to-property\": [\n" +
 
  64                         "                            \"property-key\": \"Key part of a key/value pair\",\n" +
 
  65                         "                            \"property-value\": \"Value part of a key/value pair\"\n" +
 
  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"+
 
  84         private static String getPnfTemplateFilled(String pnfId,String type,String model,String vendor,String oamIp,List<String> ifaces)
 
  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));
 
  93         private static String getPnfTemplateInterfaceList(String pnfId,List<String> ifaces,String model) {
 
  99                                 s+=pnfJSON_INTERFACE_TEMPLATE.replace("@interface@", ifaces.get(0));
 
 101                         for(int i=1;i<ifaces.size();i++)
 
 102                                 s+=","+pnfJSON_INTERFACE_TEMPLATE.replace("@interface@", ifaces.get(i));
 
 106                 return s.replace("@pnfId@",pnfId).replace("@model@", model);
 
 108         private static Logger LOG = LoggerFactory.getLogger(AaiWebApiClient.class);
 
 110         private final Map<String, String> headers;
 
 112         public AaiWebApiClient(String baseUrl, Map<String, String> headers, boolean trustAllCerts) {
 
 113                 this(baseUrl,headers,trustAllCerts,null,null);
 
 116         public AaiWebApiClient(String baseUrl, Map<String, String> headers, boolean trustAllCerts, String certFilename,
 
 118                 super(baseUrl, trustAllCerts, certFilename, passphrase, BaseHTTPClient.SSLCERT_PCKS);
 
 119                 this.headers = headers;
 
 123         public boolean pnfCreate(String pnfId,String type,String model,String vendor,String oamIp,List<String> ifaces)
 
 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;
 
 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());
 
 138                 return response!=null?response.code==200:false;
 
 141         public boolean pnfDelete(String pnfId)
 
 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;
 
 149                         String uri="network/pnfs/pnf/"+URLParamEncoder.encode( pnfId );
 
 150                         response=this.sendRequest(uri,
 
 153                         LOG.debug("finished with responsecode "+response.code);
 
 154                 } catch (IOException e) {
 
 155                         LOG.warn("problem unregistering "+pnfId+": "+e.getMessage());
 
 157                 return response!=null?response.code==200:false;
 
 159         public @Nonnull BaseHTTPResponse pnfCheckIfExists(String pnfId) {
 
 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");
 
 167                         String uri="network/pnfs/pnf/"+URLParamEncoder.encode( pnfId );
 
 168                         response=this.sendRequest(uri,
 
 171                         LOG.debug("finished with responsecode "+response.code);
 
 172                 } catch (IOException e) {
 
 173                         LOG.warn("problem checking "+pnfId+": "+e.getMessage());
 
 174                         response = BaseHTTPResponse.UNKNOWN;