001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019
020package org.apache.reef.runtime.common.driver.evaluator.pojos;
021
022import org.apache.reef.annotations.audience.DriverSide;
023import org.apache.reef.annotations.audience.Private;
024import org.apache.reef.proto.ReefServiceProtos;
025import org.apache.reef.proto.ReefServiceProtos.ContextStatusProto.ContextMessageProto;
026
027import java.util.ArrayList;
028import java.util.List;
029
030/**
031 * DriverSide representation of ContextStatusProto.
032 */
033@DriverSide
034@Private
035public final class ContextStatusPOJO {
036
037  private final String contextId;
038  private final String parentId;
039  private final byte[] errorBytes;
040  private final ContextState contextState;
041  private final List<ContextMessagePOJO> contextMessages = new ArrayList<>();
042
043
044  public ContextStatusPOJO(final ReefServiceProtos.ContextStatusProto proto, final long sequenceNumber){
045
046    contextId = proto.getContextId();
047    parentId = proto.hasParentId() ? proto.getParentId() : null;
048    errorBytes = proto.hasError() ? proto.getError().toByteArray() : null;
049    contextState = proto.hasContextState()? getContextStateFromProto(proto.getContextState()) : null;
050
051    for (final ContextMessageProto contextMessageProto : proto.getContextMessageList()) {
052      contextMessages.add(new ContextMessagePOJO(contextMessageProto,  sequenceNumber));
053    }
054
055  }
056
057  /**
058   * @return a list of messages sent by a context
059   */
060  public List<ContextMessagePOJO> getContextMessageList() {
061    return contextMessages;
062  }
063
064  /**
065   * @return the ID of a context
066   */
067  public String getContextId() {
068    return contextId;
069  }
070
071  /**
072   * @return the {@link org.apache.reef.runtime.common.driver.evaluator.pojos.ContextState} of a context
073   */
074  public ContextState getContextState() {
075    return contextState;
076  }
077
078  /**
079   * @return true, if a context has thrown an exception and sent it to a driver
080   */
081  public boolean hasError() {
082    return null != errorBytes;
083  }
084
085  /**
086   * @return serialized exception thrown by a context
087   */
088  public byte[] getError() {
089    return  errorBytes;
090  }
091
092  /**
093   * @return true, if a context has the parent context
094   */
095  public boolean hasParentId() {
096    return null != parentId;
097  }
098
099  /**
100   * @return the id of the parent context
101   */
102  public String getParentId(){
103    return parentId;
104  }
105
106  private ContextState getContextStateFromProto(
107          final org.apache.reef.proto.ReefServiceProtos.ContextStatusProto.State protoState) {
108
109    switch (protoState) {
110    case READY:
111      return ContextState.READY;
112    case DONE:
113      return ContextState.DONE;
114    case FAIL:
115      return ContextState.FAIL;
116    default:
117      throw new IllegalStateException("Unknown state " + protoState + " in ContextStatusProto");
118    }
119
120  }
121
122}