public ManagedElement getLocalElement(String managed_element_uuid) {
   ManagedElement me = agent.getRuntimeModel().getManagedElement(managed_element_uuid);
   if (me != null) return me;
   else {
     for (ManagedElement unme : agent.getUnmanagedElements()) {
       if (unme.getUUID().equalsIgnoreCase(managed_element_uuid)) {
         return unme;
       }
     }
   }
   return null;
 }
  public boolean addProperty(String managed_element_uuid, String name, String value)
      throws PropertyExistException, InvalidNameException {
    ManagedElement me1 = getLocalElement(managed_element_uuid);
    if (me1 != null) {
      return me1.addProperty(name, value);
    } else {
      String auri = agent.getExternalAgentUri(managed_element_uuid);
      if (auri != null) {
        CMessage msg = new CMessage();
        msg.setTo(auri);
        msg.setObject("runtimemodel");
        msg.setBody("addProperty");
        msg.addHeader("uuid", managed_element_uuid);
        msg.addHeader("name", name);
        msg.addHeader("value", value);
        try {
          CMessage resultmsg = sendAndWait(msg);
          if (resultmsg != null) {
            if (resultmsg.getBody() != null) {
              if (resultmsg.getBody().toString().equalsIgnoreCase("true")) {
                return true;
              }
            }
          }
        } catch (TimeOutException e) {
          e.printStackTrace();
        }
      }
    }

    return false; // To change body of implemented methods use File | Settings | File Templates.
  }
 public String getPropertyValue(String managed_element_uuid, String name) {
   ManagedElement me1 = getLocalElement(managed_element_uuid);
   if (me1 != null) {
     // System.out.println(".... get property value (local) of element: " + managed_element_uuid);
     return me1.getProperty(name);
   } else {
     // System.out.println(".... get property value (remote) of element: " + managed_element_uuid);
     String auri = agent.getExternalAgentUri(managed_element_uuid);
     if (auri != null) {
       CMessage msg = new CMessage();
       msg.setTo(auri);
       msg.setObject("runtimemodel");
       msg.setBody("getPropertyValue");
       msg.addHeader("uuid", managed_element_uuid);
       msg.addHeader("name", name);
       try {
         CMessage resultmsg = sendAndWait(msg);
         if (resultmsg != null) {
           if (resultmsg.getBody() != null) {
             return resultmsg.getBody().toString();
           }
         }
       } catch (TimeOutException e) {
         e.printStackTrace();
       }
     } else {
       System.out.println(
           ".... get property value: impossible to find agent uri of instance "
               + managed_element_uuid);
     }
   }
   return null;
 }
 public boolean hasReferencedElement(
     String managed_element_uuid, String reference_name, String referenced_element_uuri) {
   ManagedElement me1 = getLocalElement(managed_element_uuid);
   if (me1 != null) {
     Reference r = me1.getReference(reference_name);
     if (r != null) {
       return r.hasReferencedElement(referenced_element_uuri);
     }
   } else {
     String auri = agent.getExternalAgentUri(managed_element_uuid);
     if (auri != null) {
       CMessage msg = new CMessage();
       msg.setTo(auri);
       msg.setObject("runtimemodel");
       msg.setBody("hasReferencedElement");
       msg.addHeader("uuid", managed_element_uuid);
       msg.addHeader("name", reference_name);
       msg.addHeader("refuuid", referenced_element_uuri);
       try {
         CMessage resultmsg = sendAndWait(msg);
         if (resultmsg != null) {
           if (resultmsg.getBody() != null) {
             if (resultmsg.getBody().toString().equalsIgnoreCase("true")) {
               return true;
             }
           }
         }
       } catch (TimeOutException e) {
         e.printStackTrace();
       }
     }
   }
   return false;
 }
 public ManagedElement getElement(String managed_element_uuid) {
   String auri = agent.getExternalAgentUri(managed_element_uuid);
   if (auri != null) {
     return getRemoteElement(managed_element_uuid);
   } else {
     return getLocalElement(managed_element_uuid);
   }
   // return null;
 }
 public boolean addReferencedElement(
     String managed_element_uuid,
     String reference_name,
     boolean onlyone,
     String referenced_element_uuid)
     throws InvalidNameException {
   ManagedElement me1 = getLocalElement(managed_element_uuid);
   if (me1 != null) {
     // System.out.println("add Local Reference Element ...");
     Reference r = me1.getReference(reference_name);
     if (r == null) {
       // System.out.println("Reference '"+reference_name+"' does not exists! we will create it!");
       r = me1.addReference(reference_name, onlyone);
     }
     if (r != null) {
       r.addReferencedElement(referenced_element_uuid);
       return true;
     }
   } else {
     // System.out.println("add Remote Reference Element ...");
     String auri = agent.getExternalAgentUri(managed_element_uuid);
     if (auri != null) {
       CMessage msg = new CMessage();
       msg.setTo(auri);
       msg.setObject("runtimemodel");
       msg.setBody("addReferencedElement");
       msg.addHeader("uuid", managed_element_uuid);
       msg.addHeader("name", reference_name);
       msg.addHeader("onlyone", onlyone == true ? "true" : "false");
       msg.addHeader("refuuid", referenced_element_uuid);
       try {
         CMessage resultmsg = sendAndWait(msg);
         if (resultmsg != null) {
           if (resultmsg.getBody() != null) {
             if (resultmsg.getBody().toString().equalsIgnoreCase("true")) {
               return true;
             }
           }
         }
       } catch (TimeOutException e) {
         e.printStackTrace();
       }
     } else {
       System.out.println("External agent of element '" + managed_element_uuid + "' not found!");
     }
   }
   return false;
 }
  public List<String> getReferencedElements(String managed_element_uuid, String reference_name) {

    List<String> result = new ArrayList<String>();
    ManagedElement me1 = getLocalElement(managed_element_uuid);
    if (me1 != null) {
      Reference r = me1.getReference(reference_name);
      if (r != null) {
        return r.getReferencedElements();
      } else {
        // System.out.println("CONTROLLER: no reference with name: " + reference_name);
      }
    } else {
      String auri = agent.getExternalAgentUri(managed_element_uuid);
      if (auri != null) {
        CMessage msg = new CMessage();
        msg.setTo(auri);
        msg.setObject("runtimemodel");
        msg.setBody("getReferencedElements");
        msg.addHeader("uuid", managed_element_uuid);
        msg.addHeader("name", reference_name);
        try {
          CMessage resultmsg = sendAndWait(msg);
          if (resultmsg != null) {
            if (resultmsg.getBody() != null) {
              String[] tmp = resultmsg.getBody().toString().split(",");
              for (int i = 0; i < tmp.length; i++) {
                if (tmp[i] != null && tmp[i].length() > 0) {
                  String[] tmp2 = tmp[i].split("###");
                  String agenturi = tmp2[0];
                  String elementuuid = tmp2[1];
                  if (!agenturi.equalsIgnoreCase(getCubeAgent().getUri())) {
                    this.agent.addExternalElement(elementuuid, agenturi);
                  }
                  result.add(elementuuid);
                }
              }
            }
          }
        } catch (TimeOutException e) {
          e.printStackTrace();
        }
      }
    }
    return result;
  }
 public ManagedElement getRemoteElement(String managed_element_uuid) {
   String auri = agent.getExternalAgentUri(managed_element_uuid);
   if (auri != null) {
     CMessage msg = new CMessage();
     msg.setTo(auri);
     msg.setObject("runtimemodel");
     msg.setBody("getRemoteElement");
     msg.addHeader("uuid", managed_element_uuid);
     try {
       CMessage resultmsg = sendAndWait(msg);
       if (resultmsg != null) {
         return resultmsg.getAttachement();
       }
     } catch (TimeOutException e) {
       e.printStackTrace();
     }
   }
   return null;
 }