Commit e40d126d authored by garciay's avatar garciay
Browse files

Merge STF455 staff

parent e8ef5e9e
package org.etsi.its.tool.elvior;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.elvior.ttcn.tritci.TciProvider;
import org.elvior.ttcn.tritci.TriMessageEx;
import org.elvior.ttcn.tritci.TriProvider;
import org.etsi.common.ByteHelper;
import org.etsi.ttcn.tci.RecordValue;
import org.etsi.ttcn.tci.TciCDProvided;
import org.etsi.ttcn.tci.Type;
import org.etsi.ttcn.tci.Value;
import org.etsi.ttcn.tri.TriMessage;
/**
*
* Note that "Enable Internal Codec" field shall be set to true
*
*/
public class BuiltInCodec implements TciCDProvided {
/**
* Logger instance
*/
protected final static Logger _logger = Logger.getLogger("org.etsi.its");
private String _encodingName;
/**
* Constructor
*/
public BuiltInCodec(final String encodingName) {
_logger.entering("BuiltInCodec", "BuiltInCodec", encodingName);
if (encodingName.equals("LibItsDenm_asn1")) {
_encodingName = "DENM_PDU_Descriptions";
} else if (encodingName.equals("LibItsCam_asn1")) {
_encodingName = "CAM_PDU_Descriptions";
} else if (encodingName.equals("LibItsCALMmsap_asn1")) {
_encodingName = "CALMmsap";
} else if (encodingName.equals("LibItsCALMllsap_asn1")) {
_encodingName = "CALMllsap";
} else if (encodingName.equals("LibItsFntp_asn1")) {
_encodingName = "CALMfntp";
} else if (encodingName.equals("LibItsFsap_asn1")) {
_encodingName = "CALMfsap";
} else if (encodingName.equals("LibItsIicp_asn1")) {
_encodingName = "CALMiitsscu";
} else {
_encodingName = "";
}
}
/**
* This operation decodes message according to the encoding rules and returns a TTCN-3 value.
* The decodingHypothesis shall be used to determine whether the encoded value can be decoded.
* If an encoding rule is not self-sufficient, i.e. if the encoded message does not inherently
* contain its type decodingHypothesis shall be used. If the encoded value can be decoded without
* the decoding hypothesis, the distinct null value shall be returned if the type determined from
* the encoded message is not compatible with the decoding hypothesis
*
* @param message The encoded message to be decoded
* @param decodingHypothesis The hypothesis the decoding can be based on
* @return Returns the decoded value, if the value is of a compatible type as the decodingHypothesis, else the distinct value null
*
* See ETSI ES 201 873-6 V4.2.1 - 7.3.2.2.1 decode
*/
@Override
public Value decode(final TriMessage message, final Type decodingHypothesis) {
_logger.entering("BuiltInCodec", "decode", decodingHypothesis.getName());
TriMessageEx msg = TriProvider.getInstance().getTriFactory().createMessageEx();
msg.setEncodedMessage(message.getEncodedMessage());
if (_logger.isLoggable(Level.ALL)) ByteHelper.dump("BuiltInCodec.decode: ", msg.getEncodedMessage());
String type = String.format("%s.%s", _encodingName, decodingHypothesis.getName());
_logger.info("Type: " + type);
Type asnOriginalType = getTypeForName(type);
// Check which type class the decoding hypothesis is of
Value value = TciProvider.getInstance().getSystemInterface().internalDecode(msg, asnOriginalType);
_logger.exiting("BuiltInCodec", "decode", value.toString());
return value;
}
@Override
public TriMessage encode(final Value template) {
_logger.entering("BuiltInCodec", "encode", template.getType().getName());
RecordValue asn1Value = (RecordValue)template;
String type = String.format("%s.%s", _encodingName, template.getType().getName());
_logger.info("Type: " + type);
Type asnOriginalType = getTypeForName(type);
if (asnOriginalType != null) {
RecordValue internalASNEncodecValue = (RecordValue)asnOriginalType.newInstance();
String[] fields = internalASNEncodecValue.getFieldNames();
for(String field: fields) {
_logger.info(String.format("Process field %s", field));
Value fieldValue = asn1Value.getField(field);
if(fieldValue.notPresent()) {
_logger.info(String.format("Field %s was omitted", field));
internalASNEncodecValue.setFieldOmitted(field);
} else {
_logger.info(String.format("Field %s was added", field));
internalASNEncodecValue.setField(field, fieldValue);
}
} // End of 'for' statement
_logger.info("Call internal codec");
TriMessage msg = TciProvider.getInstance().getSystemInterface().internalEncode(internalASNEncodecValue);
ByteHelper.dump("BuiltInCodec.encode: ", msg.getEncodedMessage());
_logger.exiting("BuiltInCodec", "encode");
return msg;
}
_logger.exiting("BuiltInCodec", "encode", "null");
return null;
}
private Type getTypeForName(final String type) {
_logger.entering("BuiltInCodec", "getTypeForName", type);
Type asnOriginalType = TciProvider.getInstance().getTciCDRequired().getTypeForName(type);
if (asnOriginalType == null) { // Failed with default mechanism, try with External Attributes mechanism
// type = decodingHypothesis.getName(); //FIXME External Attributes mechanism does not work
String type_ = "";
if (type.endsWith("nfSapPrimitivesUp")) {
type_ = String.format("%s.%s", _encodingName, "NFsapPrimitivesUp");
} else if (type.endsWith("faSapPrimitivesUp")) {
type_ = String.format("%s.%s", _encodingName, "FAsapPrimitivesUp");
} else if (type.endsWith("mnRequestRequest")) {
type_ = String.format("%s.%s", _encodingName, "MN_Request_request");
} else if (type.endsWith("mfCommandRequest")) {
type_ = String.format("%s.%s", _encodingName, "MF_Command_request");
} else if (type.endsWith("miCommandRequest")) {
type_ = String.format("%s.%s", _encodingName, "MI_Command_request");
} else if (type.endsWith("miCommandConfirm")) {
type_ = String.format("%s.%s", _encodingName, "MI_Command_confirm");
} else if (type.endsWith("miRequestRequest")) {
type_ = String.format("%s.%s", _encodingName, "MI_Request_request");
} else if (type.endsWith("miRequestConfirm")) {
type_ = String.format("%s.%s", _encodingName, "MI_Request_confirm");
} else if (type.endsWith("miSetRequest")) {
type_ = String.format("%s.%s", _encodingName, "MI_Set_request");
} else if (type.endsWith("miSetConfirm")) {
type_ = String.format("%s.%s", _encodingName, "MI_Set_confirm");
} else if (type.endsWith("miGetRequest")) {
type_ = String.format("%s.%s", _encodingName, "MI_Get_request");
} else if (type.endsWith("miGetConfirm")) {
type_ = String.format("%s.%s", _encodingName, "MI_Get_confirm");
} if (type.endsWith("fntpNPDU")) {
type_ = String.format("%s.%s", _encodingName, "INsapPrimitivesUp");
}
_logger.info("Type: " + type_);
asnOriginalType = TciProvider.getInstance().getTciCDRequired().getTypeForName(type_);
}
_logger.exiting("BuiltInCodec", "getTypeForName", asnOriginalType.getName());
return asnOriginalType;
} // End of method getTypeForName
} // End of class BuiltInCodec
//package org.etsi.its.tool.elvior;
//
//import java.lang.reflect.Constructor;
//import java.util.Map;
//import java.util.TreeMap;
//
//import org.elvior.ttcn.tritci.TriTciChannel;
//import org.etsi.its.codec.ItsCodecImpl;
//import org.etsi.its.codec.ttcn.LibItsBtp_TypesAndValues.LibItsBtp_TypesAndValuesCodec;
//import org.etsi.ttcn.tci.TciCDProvided;
//import org.etsi.ttcn.tci.TciCDRequired;
//
//public class CodecsProvider {
//
// /**
// * Unique instance of the factory
// */
// private static CodecsProvider instance = new CodecsProvider();
//
// /**
// * List of the registered protocol port types
// */
// protected Map<String, Class<? extends ItsCodecImpl>> codecs = new TreeMap<String, Class<? extends ItsCodecImpl>>();
//
// /**
// * Private constructor (Singleton pattern)
// */
// private CodecsProvider() {
// codecs.put("BtpReq", LibItsBtp_TypesAndValuesCodec.class);
// codecs.put("BtpInd", LibItsBtp_TypesAndValuesCodec.class);
// //codecs.put("FNTPPDU", CamPort.class);
// }
//
// /**
// * Gets the unique factory instance
// * @return ProtocolPortFactory instance
// */
// public static CodecsProvider getInstance(){
// return instance;
// }
//
// /**
// * Creates a new instance of the desired codec
// * @param codecName Name of the codec to implement
// * @return The codec instance
// */
// public TciCDProvided createInstance(final String codecName) {
// TciCDProvided codec = null;
// Class<?>[] ctorParams = { TriTciChannel.getInstance().getClass() };
//
// try {
// Class<? extends ItsCodecImpl> cls = codecs.get(codecName);
//
// if (cls == null) {
// throw new RuntimeException("No class registered under " + codecName);
// }
// codec = new LibItsBtp_TypesAndValuesCodec((TciCDRequired)(TriTciChannel.getInstance()));
//// Constructor<? extends ItsCodecImpl> ctor = cls.getConstructor(ctorParams);
//// codec = (TciCDProvided) ctor.newInstance((TciCDRequired)(TriTciChannel.getInstance()));
// } catch (Exception ex) {
// ex.printStackTrace();
// }
//
// return codec;
// }
//
//} // End of class CodecsProvider
package org.etsi.its.tool.elvior;
import org.elvior.ttcn.tritci.IntegerValueEx;
import org.etsi.ttcn.tci.BitstringValue;
import org.etsi.ttcn.tci.BooleanValue;
import org.etsi.ttcn.tci.CharstringValue;
import org.etsi.ttcn.tci.EnumeratedValue;
import org.etsi.ttcn.tci.HexstringValue;
import org.etsi.ttcn.tci.OctetstringValue;
import org.etsi.ttcn.tci.RecordOfValue;
import org.etsi.ttcn.tci.RecordValue;
import org.etsi.ttcn.tci.TciTypeClass;
import org.etsi.ttcn.tci.UnionValue;
import org.etsi.ttcn.tci.Value;
public class GetValueStructure {
static String indent = null;
static StringBuilder buffer = null;
static public void displayValueStructure(final Value value) {
buffer = new StringBuilder();
indent = "";
LogValueDataStructure(value);
System.out.println(buffer.toString());
}
static public String getValueStructure(final Value value) {
buffer = new StringBuilder();
indent = "";
LogValueDataStructure(value);
return buffer.toString();
}
static private void LogValueDataStructure(final Value value) {
if (value.notPresent()) {
buffer.append(indent + value.getClass().getName() + " : omitted" + "\n");
return;
}
switch (value.getType().getTypeClass()) {
case TciTypeClass.BOOLEAN: {
BooleanValue bv = (BooleanValue)value;
buffer.append(indent + bv.getClass().getName() + " : " + new java.lang.Boolean(bv.getBoolean()) + "\n");
}
break;
case TciTypeClass.INTEGER: {
IntegerValueEx iv = (IntegerValueEx)value;
buffer.append(indent + iv.getClass().getName() + " : " + iv.getInt64() + "\n");
}
break;
case TciTypeClass.CHARSTRING: {
CharstringValue csv = (CharstringValue)value;
buffer.append(indent + csv.getClass().getName() + " : " + csv.getString() + "\n");
}
break;
case TciTypeClass.BITSTRING: {
BitstringValue bv = (BitstringValue)value;
buffer.append(indent + bv.getClass().getName() + " : " + bv.getString() + "\n");
}
break;
case TciTypeClass.OCTETSTRING: {
OctetstringValue ov = (OctetstringValue)value;
buffer.append(indent + ov.getClass().getName() + " : " + ov.getString() + "\n");
}
break;
case TciTypeClass.HEXSTRING: {
HexstringValue hv = (HexstringValue)value;
buffer.append(indent + hv.getClass().getName() + " : " + hv.getString() + "\n");
}
break;
case TciTypeClass.ENUMERATED: {
EnumeratedValue ev = (EnumeratedValue)value;
buffer.append(indent + ev.getClass().getName() + " : " + ev.getEnum() + "\n");
}
break;
case TciTypeClass.SET:
// No break;
case TciTypeClass.RECORD: {
RecordValue rv = (RecordValue)value;
buffer.append(indent + rv.getClass().getName() + "\n");
indent += "\t";
for (String key : rv.getFieldNames()) {
LogValueDataStructure(rv.getField(key));
} // End of 'for'statement
indent = indent.substring(0, indent.length() -1);
}
break;
case TciTypeClass.ANYTYPE:
// No break;
case TciTypeClass.UNION: {
UnionValue uv = (UnionValue)value;
buffer.append(indent + uv.getClass().getName() + "\n");
indent += "\t";
LogValueDataStructure(uv.getVariant(uv.getPresentVariantName()));
indent = indent.substring(0, indent.length() -1);
}
break;
case TciTypeClass.RECORD_OF:
// No break;
case TciTypeClass.SET_OF: {
RecordOfValue rofv = (RecordOfValue)value;
buffer.append(indent + rofv.getClass().getName() + " : " + rofv.getLength() + "\n");
indent += "\t";
for (int key = 0; key < rofv.getLength(); key++) {
LogValueDataStructure(rofv.getField(key));
} // End of 'for'statement
indent = indent.substring(0, indent.length() -1);
}
break;
default:
throw new RuntimeException("Unimplemented class type: " + value.getType().getTypeClass());
} // End of 'switch' value
}
}
......@@ -10,11 +10,11 @@ import org.elvior.ttcn.tritci.ChannelEventHandler;
import org.elvior.ttcn.tritci.TciProvider;
import org.elvior.ttcn.tritci.TriTciChannel;
import org.etsi.codec.ITCIRequired;
import org.etsi.codec.ITciCDWrapper;
import org.etsi.codec.TciCDWrapperFactory;
import org.etsi.tool.elvior.TciCDWrapper;
import org.etsi.ttcn.codec.CodecFactory;
import org.etsi.ttcn.tci.TciCDProvided;
import org.etsi.ttcn.tci.TciCDRequired;
import org.etsi.ttcn.tci.Type;
import org.etsi.ttcn.tci.Value;
import org.etsi.ttcn.tri.TriMessage;
......@@ -28,12 +28,16 @@ public class Its_CodecProvider implements TciCDProvided, ChannelEventHandler, IT
*/
private final static Logger _logger = Logger.getLogger("org.etsi.its");
private TciCDRequired _cdReq;
private ITciCDWrapper _cdReq;
private CodecFactory _cf;
public Its_CodecProvider() {
_logger.entering("Its_CodecProvider", "Its_CodecProvider");
// Load Codec settings
_cdReq = null;
_cf = null;
try {
_properties.load(MainCodec.class.getResourceAsStream("/org/etsi/its/tool/elvior/res/codec.properties"));
} catch (IOException e) {
......@@ -62,7 +66,27 @@ public class Its_CodecProvider implements TciCDProvided, ChannelEventHandler, IT
// Register this object as the receiver of events generated by TestCast framework
channel.setEventHandler(this);
initializeCodecs();
}
private void initializeCodecs() {
TciCDWrapperFactory.getInstance().setImpl(new TciCDWrapper());
_cdReq = TciCDWrapperFactory.getTciCDInstance();
// Register External codecs
_cf = CodecFactory.getInstance();
// TODO Reuse Build-in codec here
String[] asn1Codecs = _properties.getProperty("ASN.1Codecs", "").split(",");
for (String codec : asn1Codecs) {
System.out.println("getCodec: " + codec);
String[] paths = _properties.getProperty(codec, "").split(",");
try {
_cf.setExternalCodec(paths[0], (TciCDProvided) Class.forName(paths[1]).newInstance());
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
e.printStackTrace();
}
} // End of 'foreach' loop
}
public void run() {
......@@ -118,7 +142,7 @@ public class Its_CodecProvider implements TciCDProvided, ChannelEventHandler, IT
return null;
}
Value value = codec.decode(message, decodingHypothesis);
_logger.exiting("decode", (value != null) ? value.toString() : "(null)");
_logger.exiting("Its_CodecProvider.decode", (value != null) ? GetValueStructure.getValueStructure(value) : "(null)");
return value;
}
......@@ -150,6 +174,7 @@ public class Its_CodecProvider implements TciCDProvided, ChannelEventHandler, IT
if (codec == null) {
return null;
}
_logger.info("Value to encode: " + GetValueStructure.getValueStructure(value));
TriMessage res = codec.encode(value);
_logger.exiting("encode", res.toString());
return res;
......@@ -163,34 +188,8 @@ public class Its_CodecProvider implements TciCDProvided, ChannelEventHandler, IT
* @return The codec associated to the specified encoding identifier
*/
@Override
public TciCDProvided getCodec(String encodingName) {
_logger.entering("getCodec", encodingName);
_cdReq = TciProvider.getInstance().getTciCDRequired();
// Register External codecs
CodecFactory cf = CodecFactory.getInstance();
// TODO Reuse Build-in codec here
String[] asn1Codecs = _properties.getProperty("ASN.1Codecs", "").split(",");
System.out.println("getCodec: " + asn1Codecs);
for (String codec : asn1Codecs) {
System.out.println("getCodec: " + codec);
String[] paths = _properties.getProperty(codec, "").split(",");
try {
cf.setExternalCodec(paths[0], (TciCDProvided) Class.forName(paths[1]).newInstance());
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
e.printStackTrace();
}
} // End of 'foreach' loop
// TODO To be removed
// cf.setExternalCodec("LibItsFntp_asn1", cs.getCodec("LibItsFntp_asn1"));
// cf.setExternalCodec("LibItsFsap_asn1", cs.getCodec("LibItsFsap_asn1"));
// cf.setExternalCodec("LibItsIicp_asn1", cs.getCodec("LibItsIicp_asn1"));
// cf.setExternalCodec("LibItsCALMmsap_asn1", cs.getCodec("LibItsCALMmsap_asn1"));
// cf.setExternalCodec("LibItsCALMllsap_asn1", cs.getCodec("LibItsCALMllsap_asn1"));
//TODO: add V2G external codecs (xsd)
public TciCDProvided getCodec(final String encodingName) {
// FIXME Remove argument here and in the interface ITCIRequired
return new Codec();
}
......
package org.etsi.its.tool.elvior;
import org.etsi.ttcn.tci.Type;
public class LibItsCALMllsap_asn1 extends LibIts_asn1 {
/**
* Constructor
*/
public LibItsCALMllsap_asn1() {
_logger.entering("LibItsCALMllsap_asn1", "LibItsCALMllsap_asn1");
_encodingName = "CALMllsap";
}
protected Type getTypeForName(final String type) {
_logger.entering("LibItsCALMllsap_asn1", "getTypeForName", type);
String type_ = type;
if (type.endsWith("mnRequestRequest")) {
type_ = String.format("%s.%s", _encodingName, "MN_Request_request");
} else if (type.endsWith("msgIn_in")) {
type_ = String.format("%s.%s", _encodingName, "INsapPrimitivesDown");
}
Type asnOriginalType = super.getTypeForName(type_);
_logger.exiting("LibItsCALMllsap_asn1", "getTypeForName", asnOriginalType.getName());
return asnOriginalType;
} // End of method getTypeForName
} // End of class LibItsCALMllsap_asn1
package org.etsi.its.tool.elvior;
import org.etsi.ttcn.tci.Type;
public class LibItsCALMmsap_asn1 extends LibIts_asn1 {
/**
* Constructor
*/
public LibItsCALMmsap_asn1() {
_logger.entering("LibItsCALMmsap_asn1", "LibItsCALMmsap_asn1");
_encodingName = "CALMmsap";
}
protected Type getTypeForName(final String type) {
_logger.entering("LibItsCALMmsap_asn1", "getTypeForName", type);
String type_ = type;
if (type.endsWith("mfRequestRequest")) {
type_ = String.format("%s.%s", _encodingName, "MF_Request_request");
} else if (type.endsWith("mnRequestRequest")) {
type_ = String.format("%s.%s", _encodingName, "MN_Request_request");
} else if (type.endsWith("miRequestRequest")) {
type_ = String.format("%s.%s", _encodingName, "MI_Request_request");
} else if (type.endsWith("mf_Command_request")) {
type_ = String.format("%s.%s", _encodingName, "MF_Command_request");
} else if (type.endsWith("mn_Command_request")) {
type_ = String.format("%s.%s", _encodingName, "MN_Command_request");
} else if (type.endsWith("mi_Command_request")) {
type_ = String.format("%s.%s", _encodingName, "MI_Command_request");
} else if (type.endsWith("faSapPrimitivesUp")) {
type_ = String.format("%s.%s", _encodingName, "FAsapPrimitivesUp");
}
Type asnOriginalType = super.getTypeForName(type_);
_logger.exiting("LibItsCALMmsap_asn1", "getTypeForName", asnOriginalType.getName());
return asnOriginalType;
} // End of method getTypeForName
} // End of class LibItsCALMmsap_asn1
......@@ -8,7 +8,6 @@ package org.etsi.its.tool.elvior;
*/
public class LibItsCam_asn1 extends LibIts_asn1 {
/**
* Constructor
*/
......@@ -17,4 +16,4 @@ public class LibItsCam_asn1 extends LibIts_asn1 {
_encodingName = "CAM_PDU_Descriptions";
}
} // End of class BuiltInCodec
} // End of class LibItsCam_asn1
package org.etsi.its.tool.elvior;
import org.etsi.ttcn.tci.Type;
public class LibItsFntp_asn1 extends LibIts_asn1 {
/**
* Constructor
*/
public LibItsFntp_asn1() {
_logger.entering("LibItsFntp_asn1", "LibItsFntp_asn1");
_encodingName = "CALMfntp";
}
protected Type getTypeForName(final String type) {
_logger.entering("LibItsFntp_asn1", "getTypeForName", type);
String type_ = type;
if (type.endsWith("nfSapPrimitivesUp")) {
type_ = String.format("%s.%s", _encodingName, "NFsapPrimitivesUp");
} else if (type.endsWith("mnRequestRequest")) {
type_ = String.format("%s.%s", _encodingName, "MN_Request_request");
} else if (type.endsWith("msgIn_nf")) {
type_ = String.format("%s.%s", _encodingName, "NFsapPrimitivesDown");
}
Type asnOriginalType = super.getTypeForName(type_);
_logger.exiting("LibItsFntp_asn1", "getTypeForName", asnOriginalType.getName());
return asnOriginalType;
} // End of method getTypeForName