Esempio n. 1
0
  public Applications toApplications() {
    Map<String, Application> appsByName = new HashMap<>();
    Iterator<InstanceInfo> it = serviceIterator();
    while (it.hasNext()) {
      InstanceInfo instanceInfo = it.next();
      Application instanceApp = appsByName.get(instanceInfo.getAppName());
      if (instanceApp == null) {
        instanceApp = new Application(instanceInfo.getAppName());
        appsByName.put(instanceInfo.getAppName(), instanceApp);
      }
      instanceApp.addInstance(instanceInfo);
    }

    // Do not pass application list to the constructor, as it does not initialize properly
    // Applications
    // data structure.
    Applications applications = new Applications();
    for (Application app : appsByName.values()) {
      applications.addApplication(app);
    }

    applications.setAppsHashCode(applications.getReconcileHashCode());

    return applications;
  }
Esempio n. 2
0
 public static ReplicationInstance newReplicationInstanceOf(Action action, InstanceInfo instance) {
   switch (action) {
     case Register:
       return new ReplicationInstance(
           instance.getAppName(),
           instance.getId(),
           System.currentTimeMillis(),
           null,
           instance.getStatus().name(),
           instance,
           action);
     case Cancel:
       return new ReplicationInstance(
           instance.getAppName(),
           instance.getId(),
           System.currentTimeMillis(),
           null,
           null,
           null,
           action);
     case Heartbeat:
       return new ReplicationInstance(
           instance.getAppName(),
           instance.getId(),
           System.currentTimeMillis(),
           InstanceStatus.OUT_OF_SERVICE.name(),
           instance.getStatus().name(),
           instance,
           action);
     case StatusUpdate:
       return new ReplicationInstance(
           instance.getAppName(),
           instance.getId(),
           System.currentTimeMillis(),
           InstanceStatus.OUT_OF_SERVICE.name(),
           null,
           null,
           action);
     case DeleteStatusOverride:
       return new ReplicationInstance(
           instance.getAppName(),
           instance.getId(),
           System.currentTimeMillis(),
           InstanceStatus.OUT_OF_SERVICE.name(),
           null,
           null,
           action);
   }
   throw new IllegalStateException("Unexpected action " + action);
 }
  @Test
  public void testBadRegistration() throws Exception {
    InstanceInfo instanceInfo = spy(InstanceInfoGenerator.takeOne());
    when(instanceInfo.getId()).thenReturn(null);
    Response response = applicationResource.addInstance(instanceInfo, false + "");
    assertThat(response.getStatus(), is(400));

    instanceInfo = spy(InstanceInfoGenerator.takeOne());
    when(instanceInfo.getHostName()).thenReturn(null);
    response = applicationResource.addInstance(instanceInfo, false + "");
    assertThat(response.getStatus(), is(400));

    instanceInfo = spy(InstanceInfoGenerator.takeOne());
    when(instanceInfo.getAppName()).thenReturn("");
    response = applicationResource.addInstance(instanceInfo, false + "");
    assertThat(response.getStatus(), is(400));

    instanceInfo = spy(InstanceInfoGenerator.takeOne());
    when(instanceInfo.getAppName()).thenReturn(applicationResource.getName() + "extraExtra");
    response = applicationResource.addInstance(instanceInfo, false + "");
    assertThat(response.getStatus(), is(400));

    instanceInfo = spy(InstanceInfoGenerator.takeOne());
    when(instanceInfo.getDataCenterInfo()).thenReturn(null);
    response = applicationResource.addInstance(instanceInfo, false + "");
    assertThat(response.getStatus(), is(400));

    instanceInfo = spy(InstanceInfoGenerator.takeOne());
    when(instanceInfo.getDataCenterInfo())
        .thenReturn(
            new DataCenterInfo() {
              @Override
              public Name getName() {
                return null;
              }
            });
    response = applicationResource.addInstance(instanceInfo, false + "");
    assertThat(response.getStatus(), is(400));
  }
Esempio n. 4
0
    /*
     * (non-Javadoc)
     *
     * @see
     * com.thoughtworks.xstream.converters.Converter#marshal(java.lang.Object
     * , com.thoughtworks.xstream.io.HierarchicalStreamWriter,
     * com.thoughtworks.xstream.converters.MarshallingContext)
     */
    @Override
    public void marshal(
        Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
      InstanceInfo info = (InstanceInfo) source;

      if (info.getInstanceId() != null) {
        writer.startNode(ELEM_INSTANCE_ID);
        writer.setValue(info.getInstanceId());
        writer.endNode();
      }

      writer.startNode(ELEM_HOST);
      writer.setValue(info.getHostName());
      writer.endNode();

      writer.startNode(ELEM_APP);
      writer.setValue(info.getAppName());
      writer.endNode();

      writer.startNode(ELEM_IP);
      writer.setValue(info.getIPAddr());
      writer.endNode();

      if (!("unknown".equals(info.getSID()) || "na".equals(info.getSID()))) {
        writer.startNode(ELEM_SID);
        writer.setValue(info.getSID());
        writer.endNode();
      }

      writer.startNode(ELEM_STATUS);
      writer.setValue(getStatus(info));
      writer.endNode();

      writer.startNode(ELEM_OVERRIDDEN_STATUS);
      writer.setValue(info.getOverriddenStatus().name());
      writer.endNode();

      writer.startNode(ELEM_PORT);
      writer.addAttribute(ATTR_ENABLED, String.valueOf(info.isPortEnabled(PortType.UNSECURE)));
      writer.setValue(String.valueOf(info.getPort()));
      writer.endNode();

      writer.startNode(ELEM_SECURE_PORT);
      writer.addAttribute(ATTR_ENABLED, String.valueOf(info.isPortEnabled(PortType.SECURE)));
      writer.setValue(String.valueOf(info.getSecurePort()));
      writer.endNode();

      writer.startNode(ELEM_COUNTRY_ID);
      writer.setValue(String.valueOf(info.getCountryId()));
      writer.endNode();

      if (info.getDataCenterInfo() != null) {
        writer.startNode(NODE_DATACENTER);
        // This is needed for backward compat. for now.
        if (info.getDataCenterInfo().getName() == Name.Amazon) {
          writer.addAttribute("class", "com.netflix.appinfo.AmazonInfo");
        } else {
          writer.addAttribute("class", "com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo");
        }
        context.convertAnother(info.getDataCenterInfo());
        writer.endNode();
      }

      if (info.getLeaseInfo() != null) {
        writer.startNode(NODE_LEASE);
        context.convertAnother(info.getLeaseInfo());
        writer.endNode();
      }

      if (info.getMetadata() != null) {
        writer.startNode(NODE_METADATA);
        // for backward compat. for now
        if (info.getMetadata().size() == 0) {
          writer.addAttribute("class", "java.util.Collections$EmptyMap");
        }
        context.convertAnother(info.getMetadata());
        writer.endNode();
      }
      autoMarshalEligible(source, writer);
    }