Rev

Rev 2230 | Details | Compare with Previous | Last modification | View Log | SVN | Bug Tracker

Rev Author Line No. Line
1472 garciay 1
package org.etsi.its.tool.elvior;
2
 
3
import java.util.logging.Level;
4
import java.util.logging.Logger;
5
 
6
import org.elvior.ttcn.tritci.TciProvider;
7
import org.elvior.ttcn.tritci.TriMessageEx;
8
import org.elvior.ttcn.tritci.TriProvider;
9
import org.etsi.common.ByteHelper;
10
import org.etsi.ttcn.tci.RecordValue;
11
import org.etsi.ttcn.tci.TciCDProvided;
12
import org.etsi.ttcn.tci.Type;
13
import org.etsi.ttcn.tci.Value;
14
import org.etsi.ttcn.tri.TriMessage;
15
 
16
/**
17
 *
18
 * Note that "Enable Internal Codec" field shall be set to true
19
 *
20
 */
21
public class BuiltInCodec implements TciCDProvided {
22
 
23
    /**
24
     * Logger instance
25
     */
26
    protected final static Logger _logger = Logger.getLogger("org.etsi.its");
27
 
28
    private String _encodingName;
29
 
30
    /**
31
     * Constructor
32
     */
33
    public BuiltInCodec(final String encodingName) {
34
        _logger.entering("BuiltInCodec", "BuiltInCodec", encodingName);
35
 
36
        if (encodingName.equals("LibItsDenm_asn1")) {
37
            _encodingName = "DENM_PDU_Descriptions";
38
        } else if (encodingName.equals("LibItsCam_asn1")) {
39
            _encodingName = "CAM_PDU_Descriptions";
2655 filatov 40
        } else if (encodingName.equals("LibItsMapemSpatem_asn1")) {
41
            _encodingName = "MAPEM_SPATEM_ETSI";
42
        } else if (encodingName.equals("LibItsIvim_asn1")) {
43
            _encodingName = "IVIM_PDU_Descriptions";
44
        } else if (encodingName.equals("LibItsEvcsn_asn1")) {
45
            _encodingName = "EVCSN_PDU_Descriptions";
46
        } else if (encodingName.equals("LibItsSremSsem_asn1")) {
47
            _encodingName = "SREM_SSEM_ETSI";
1472 garciay 48
        } else if (encodingName.equals("LibItsCALMmsap_asn1")) {
49
            _encodingName = "CALMmsap";
50
        } else if (encodingName.equals("LibItsCALMllsap_asn1")) {
51
            _encodingName = "CALMllsap";
52
        } else if (encodingName.equals("LibItsFntp_asn1")) {
53
            _encodingName = "CALMfntp";
54
        } else if (encodingName.equals("LibItsFsap_asn1")) {
55
            _encodingName = "CALMfsap";
56
        } else if (encodingName.equals("LibItsIicp_asn1")) {
57
            _encodingName = "CALMiitsscu";
58
        } else {
59
            _encodingName = "";
60
        }
61
    }
62
 
63
    /**
64
     * This operation decodes message according to the encoding rules and returns a TTCN-3 value.
65
     * The decodingHypothesis shall be used to determine whether the encoded value can be decoded.
66
     * If an encoding rule is not self-sufficient, i.e. if the encoded message does not inherently
67
     * contain its type decodingHypothesis shall be used. If the encoded value can be decoded without
68
     * the decoding hypothesis, the distinct null value shall be returned if the type determined from
69
     * the encoded message is not compatible with the decoding hypothesis
70
     *
71
     * @param message The encoded message to be decoded
72
     * @param decodingHypothesis The hypothesis the decoding can be based on
73
     * @return Returns the decoded value, if the value is of a compatible type as the decodingHypothesis, else the distinct value null
74
     *
75
     * See ETSI ES 201 873-6 V4.2.1 - 7.3.2.2.1 decode
76
     */
77
    @Override
78
    public Value decode(final TriMessage message, final Type decodingHypothesis) {
79
        _logger.entering("BuiltInCodec", "decode", decodingHypothesis.getName());
80
 
81
        TriMessageEx msg = TriProvider.getInstance().getTriFactory().createMessageEx();
82
        msg.setEncodedMessage(message.getEncodedMessage());
83
        if (_logger.isLoggable(Level.ALL)) ByteHelper.dump("BuiltInCodec.decode: ", msg.getEncodedMessage());
84
 
85
        String type = String.format("%s.%s", _encodingName, decodingHypothesis.getName());
86
        _logger.info("Type: " + type);
87
        Type asnOriginalType = getTypeForName(type);
88
        // Check which type class the decoding hypothesis is of
89
        Value value = TciProvider.getInstance().getSystemInterface().internalDecode(msg, asnOriginalType);
90
        _logger.exiting("BuiltInCodec", "decode", value.toString());
91
        return value;
92
    }
93
 
94
    @Override
95
    public TriMessage encode(final Value template) {
96
        _logger.entering("BuiltInCodec", "encode", template.getType().getName());
97
 
98
        RecordValue asn1Value = (RecordValue)template;
99
        String type = String.format("%s.%s", _encodingName, template.getType().getName());
100
        _logger.info("Type: " + type);
101
        Type asnOriginalType = getTypeForName(type);
102
        if (asnOriginalType != null) {
103
            RecordValue internalASNEncodecValue = (RecordValue)asnOriginalType.newInstance();
104
            String[] fields = internalASNEncodecValue.getFieldNames();
105
            for(String field: fields) {
106
                _logger.info(String.format("Process field %s", field));
107
                Value fieldValue = asn1Value.getField(field);
108
                if(fieldValue.notPresent()) {
109
                    _logger.info(String.format("Field %s was omitted", field));
110
                    internalASNEncodecValue.setFieldOmitted(field);
111
                } else {
112
                    _logger.info(String.format("Field %s was added", field));
113
                    internalASNEncodecValue.setField(field, fieldValue);
114
                }
115
            } // End of 'for' statement
116
            _logger.info("Call internal codec");
117
            TriMessage msg = TciProvider.getInstance().getSystemInterface().internalEncode(internalASNEncodecValue);
118
            ByteHelper.dump("BuiltInCodec.encode: ", msg.getEncodedMessage());
119
            _logger.exiting("BuiltInCodec", "encode");
120
            return msg;
121
        }
122
 
123
        _logger.exiting("BuiltInCodec", "encode", "null");
124
        return null;
125
    }
126
 
127
    private Type getTypeForName(final String type) {
128
        _logger.entering("BuiltInCodec", "getTypeForName", type);
129
 
130
        Type asnOriginalType = TciProvider.getInstance().getTciCDRequired().getTypeForName(type);
131
        if (asnOriginalType == null) { // Failed with default mechanism, try with External Attributes mechanism 
132
//          type = decodingHypothesis.getName(); //FIXME External Attributes mechanism does not work 
133
            String type_ = "";
134
            if (type.endsWith("nfSapPrimitivesUp")) {
135
                type_ = String.format("%s.%s", _encodingName, "NFsapPrimitivesUp");
136
            } else if (type.endsWith("faSapPrimitivesUp")) {
137
                    type_ = String.format("%s.%s", _encodingName, "FAsapPrimitivesUp");
138
            } else if (type.endsWith("mnRequestRequest")) {
139
                type_ = String.format("%s.%s", _encodingName, "MN_Request_request");
140
            } else if (type.endsWith("mfCommandRequest")) {
141
                type_ = String.format("%s.%s", _encodingName, "MF_Command_request");
142
            } else if (type.endsWith("miCommandRequest")) {
143
                type_ = String.format("%s.%s", _encodingName, "MI_Command_request");
144
            } else if (type.endsWith("miCommandConfirm")) {
145
                type_ = String.format("%s.%s", _encodingName, "MI_Command_confirm");
146
            } else if (type.endsWith("miRequestRequest")) {
147
                type_ = String.format("%s.%s", _encodingName, "MI_Request_request");
148
            } else if (type.endsWith("miRequestConfirm")) {
149
                type_ = String.format("%s.%s", _encodingName, "MI_Request_confirm");
150
            } else if (type.endsWith("miSetRequest")) {
151
                type_ = String.format("%s.%s", _encodingName, "MI_Set_request");
152
            } else if (type.endsWith("miSetConfirm")) {
153
                type_ = String.format("%s.%s", _encodingName, "MI_Set_confirm");
154
            } else if (type.endsWith("miGetRequest")) {
155
                type_ = String.format("%s.%s", _encodingName, "MI_Get_request");
156
            } else if (type.endsWith("miGetConfirm")) {
157
                type_ = String.format("%s.%s", _encodingName, "MI_Get_confirm");
158
            } if (type.endsWith("fntpNPDU")) {
159
                type_ = String.format("%s.%s", _encodingName, "INsapPrimitivesUp");
160
            }
161
            _logger.info("Type: " + type_);
162
            asnOriginalType = TciProvider.getInstance().getTciCDRequired().getTypeForName(type_);
163
        }
164
 
165
        _logger.exiting("BuiltInCodec", "getTypeForName", asnOriginalType.getName());
166
        return asnOriginalType;
167
    } // End of method getTypeForName
168
 
169
} // End of class BuiltInCodec