Merge "changed revision of flow-capable transaction"
[packetcable.git] / packetcable-driver / src / main / java / org / pcmm / messages / impl / MessageFactory.java
1 /**
2  * @header@
3  */
4 package org.pcmm.messages.impl;
5
6 import org.pcmm.messages.IMessage.MessageProperties;
7 import org.pcmm.messages.IMessageFactory;
8 import org.pcmm.objects.MMVersionInfo;
9 import org.pcmm.rcd.ICMTS;
10 import org.pcmm.rcd.IPCMMClient;
11 import org.slf4j.Logger;
12 import org.slf4j.LoggerFactory;
13 import org.umu.cops.stack.*;
14
15 import java.net.InetAddress;
16 import java.util.Properties;
17
18 /**
19  *
20  *
21  */
22 public class MessageFactory implements IMessageFactory {
23
24     /** Default keep-alive timer value (secs) */
25     public static final short KA_TIMER_VALUE = 30;
26     /** Default accounting timer value (secs) */
27     public static final short ACCT_TIMER_VALUE = 0;
28
29     private static final Logger logger = LoggerFactory.getLogger(MessageFactory.class);
30
31     private static final MessageFactory instance = new MessageFactory();
32
33     private MessageFactory() {
34     }
35
36     public static MessageFactory getInstance() {
37         return instance;
38     }
39
40     /*
41      * (non-Javadoc)
42      *
43      * @see pcmm.messages.IMessageFactory#create(pcmm.messages.MessageType)
44      */
45     public COPSMsg create(final byte messageType) {
46         return create(messageType, new Properties());
47     }
48
49     /*
50      * (non-Javadoc)
51      *
52      * @see org.pcmm.messages.IMessageFactory#create(org.pcmm.messages.IMessage.
53      * MessageType, java.util.Properties)
54      */
55     public COPSMsg create(final byte messageType, final Properties properties) {
56         logger.info("Creating message of type - " + messageType);
57         // return new PCMMMessage(messageType, content);
58         switch (messageType) {
59             case COPSHeader.COPS_OP_OPN:
60                 return createOPNMessage(properties);
61             case COPSHeader.COPS_OP_REQ:
62                 return createREQMessage(properties);
63             case COPSHeader.COPS_OP_CAT:
64                 return createCATMessage(properties);
65             case COPSHeader.COPS_OP_CC:
66                 return createCCMessage(properties);
67             case COPSHeader.COPS_OP_DEC:
68                 return createDECMessage(properties);
69             case COPSHeader.COPS_OP_DRQ:
70                 break;
71             case COPSHeader.COPS_OP_KA:
72                 return createKAMessage(properties);
73             case COPSHeader.COPS_OP_RPT:
74                 break;
75             case COPSHeader.COPS_OP_SSC:
76                 break;
77             case COPSHeader.COPS_OP_SSQ:
78                 break;
79         }
80         return null;
81     }
82
83     /**
84      *
85      * @param prop - the properties
86      * @return - the message
87      */
88     protected COPSMsg createDECMessage(final Properties prop) {
89         final COPSDecisionMsg msg = new COPSDecisionMsg();
90         // ===common part between all gate control messages
91         final COPSHeader hdr = new COPSHeader(COPSHeader.COPS_OP_DEC, IPCMMClient.CLIENT_TYPE);
92         // handle
93         // context
94         final COPSContext context = new COPSContext(COPSContext.CONFIG, (short) 0);
95         // decision
96         final COPSDecision decision = new COPSDecision();
97         if (prop.get(MessageProperties.DECISION_CMD_CODE) != null)
98             decision.setCmdCode((byte) prop.get(MessageProperties.DECISION_CMD_CODE));
99         if (prop.get(MessageProperties.DECISION_FLAG) != null)
100             decision.setFlags((short) prop.get(MessageProperties.DECISION_FLAG));
101
102         final COPSClientSI si = new COPSClientSI(COPSObjHeader.COPS_DEC, (byte) 4);
103         if (prop.get(MessageProperties.GATE_CONTROL) != null)
104             si.setData((COPSData) prop.get(MessageProperties.GATE_CONTROL));
105         try {
106             msg.add(hdr);
107             final COPSHandle handle;
108             if (prop.get(MessageProperties.CLIENT_HANDLE) != null) {
109                 handle = new COPSHandle(new COPSData((String) prop.get(MessageProperties.CLIENT_HANDLE)));
110             }
111             else {
112                 // TODO - This smells wrong to have a null handle ID
113                 handle = new COPSHandle(null);
114             }
115             msg.add(handle);
116             msg.addDecision(decision, context);
117             msg.add(si);
118
119             // TODO - determine why this block has been commented out
120             // try {
121             // msg.dump(System.out);
122             // } catch (IOException unae) {
123             // }
124
125         } catch (final COPSException e) {
126             logger.error(e.getMessage());
127         }
128
129         return msg;
130     }
131
132     /**
133      * creates a Client-Open message.
134      *
135      * @param prop
136      *            properties
137      * @return COPS message
138      */
139     protected COPSMsg createOPNMessage(final Properties prop) {
140         final COPSHeader hdr = new COPSHeader(COPSHeader.COPS_OP_OPN, IPCMMClient.CLIENT_TYPE);
141         final COPSPepId pepId = new COPSPepId();
142         // version infor object
143         short majorVersion = MMVersionInfo.DEFAULT_MAJOR_VERSION_INFO;
144         short minorVersion = MMVersionInfo.DEFAULT_MINOR_VERSION_INFO;
145         if (prop.get(MessageProperties.MM_MAJOR_VERSION_INFO) != null)
146             majorVersion = (Short) prop.get(MessageProperties.MM_MAJOR_VERSION_INFO);
147         if (prop.get(MessageProperties.MM_MINOR_VERSION_INFO) != null)
148             minorVersion = (Short) prop.get(MessageProperties.MM_MINOR_VERSION_INFO);
149         // Mandatory MM version.
150         final COPSClientSI clientSI = new COPSClientSI((byte) 1);
151         byte[] versionInfo = new MMVersionInfo(majorVersion, minorVersion).getAsBinaryArray();
152         clientSI.setData(new COPSData(versionInfo, 0, versionInfo.length));
153         final COPSClientOpenMsg msg = new COPSClientOpenMsg();
154         try {
155             final COPSData d;
156             if (prop.get(MessageProperties.PEP_ID) != null)
157                 d = new COPSData((String) prop.get(MessageProperties.PEP_ID));
158             else
159                 d = new COPSData(InetAddress.getLocalHost().getHostName());
160             pepId.setData(d);
161             msg.add(hdr);
162             msg.add(pepId);
163             msg.add(clientSI);
164         } catch (Exception e) {
165             logger.error("Error creating OPN message", e);
166         }
167         return msg;
168     }
169
170     /**
171      * creates a Client-Accept message.
172      * @param prop - properties
173      * @return COPS message
174      */
175     protected COPSMsg createCATMessage(final Properties prop) {
176         final COPSHeader hdr = new COPSHeader(COPSHeader.COPS_OP_CAT, IPCMMClient.CLIENT_TYPE);
177         final COPSKATimer katimer;
178         if (prop.get(MessageProperties.KA_TIMER) != null)
179             katimer = new COPSKATimer((short) prop.get(MessageProperties.KA_TIMER));
180         else
181             katimer = new COPSKATimer(KA_TIMER_VALUE);
182
183         final COPSAcctTimer acctTimer;
184         if (prop.get(MessageProperties.ACCEPT_TIMER) != null)
185             acctTimer = new COPSAcctTimer((short) prop.get(MessageProperties.ACCEPT_TIMER));
186         else
187             acctTimer = new COPSAcctTimer(ACCT_TIMER_VALUE);
188
189         final COPSClientAcceptMsg acceptMsg = new COPSClientAcceptMsg();
190         try {
191             acceptMsg.add(hdr);
192             acceptMsg.add(katimer);
193             if (acctTimer.getTimerVal() != 0)
194                 acceptMsg.add(acctTimer);
195         } catch (COPSException e) {
196             logger.error(e.getMessage());
197         }
198         return acceptMsg;
199     }
200
201     /**
202      * creates a Client-Close message.
203      *
204      * @param prop
205      *            properties
206      * @return COPS message
207      */
208     protected COPSMsg createCCMessage(final Properties prop) {
209         final COPSHeader cHdr = new COPSHeader(COPSHeader.COPS_OP_CC, IPCMMClient.CLIENT_TYPE);
210         final COPSError err;
211         if (prop.get(MessageProperties.ERR_MESSAGE) != null) {
212             short code = (short) 0;
213             final short error = (short) prop.get(MessageProperties.ERR_MESSAGE);
214             if (prop.get(MessageProperties.ERR_MESSAGE_SUB_CODE) != null)
215                 code = (short) prop.get(MessageProperties.ERR_MESSAGE_SUB_CODE);
216             err = new COPSError(error, code);
217         } else
218             err = new COPSError(COPSError.COPS_ERR_UNKNOWN, (short) 0);
219
220         final COPSClientCloseMsg closeMsg = new COPSClientCloseMsg();
221         try {
222             closeMsg.add(cHdr);
223             closeMsg.add(err);
224         } catch (COPSException e) {
225             logger.error(e.getMessage());
226         }
227         return closeMsg;
228     }
229
230     /**
231      * creates a Request message
232      *
233      * @param prop
234      *            properties
235      * @return Request message
236      */
237     protected COPSMsg createREQMessage(final Properties prop) {
238         final COPSHeader cHdr = new COPSHeader(COPSHeader.COPS_OP_REQ, IPCMMClient.CLIENT_TYPE);
239         final COPSReqMsg req = new COPSReqMsg();
240
241         final short rType;
242         if (prop.get(MessageProperties.R_TYPE) != null)
243             rType = (Short) prop.get(MessageProperties.R_TYPE);
244         else rType = ICMTS.DEFAULT_R_TYPE;
245
246         final short mType;
247         if (prop.get(MessageProperties.M_TYPE) != null)
248             mType = (Short) prop.get(MessageProperties.M_TYPE);
249         else mType = ICMTS.DEFAULT_M_TYPE;
250
251         final COPSContext copsContext = new COPSContext(rType, mType);
252         final COPSHandle copsHandle;
253         if (prop.get(MessageProperties.CLIENT_HANDLE) != null)
254             copsHandle = new COPSHandle(new COPSData((String) prop.get(MessageProperties.CLIENT_HANDLE)));
255         else
256             // just a random handle
257             copsHandle = new COPSHandle(new COPSData("" + Math.random() * 82730));
258         try {
259             req.add(cHdr);
260             req.add(copsContext);
261             req.add(copsHandle);
262         } catch (COPSException e) {
263             logger.error(e.getMessage());
264         }
265         return req;
266     }
267
268     /**
269      * creates a Keep-Alive message.
270      *
271      * @param prop
272      *            properties
273      * @return COPS message
274      * TODO - Why is there a timer being instantiated but never used?
275      */
276     protected COPSMsg createKAMessage(final Properties prop) {
277         final COPSHeader cHdr = new COPSHeader(COPSHeader.COPS_OP_KA, (short) 0);
278         final COPSKAMsg kaMsg = new COPSKAMsg();
279         final COPSKATimer timer;
280         if (prop.get(MessageProperties.KA_TIMER) != null)
281             timer = new COPSKATimer((Short) prop.get(MessageProperties.KA_TIMER));
282         else
283             timer = new COPSKATimer();
284         try {
285             kaMsg.add(cHdr);
286         } catch (COPSException e) {
287             logger.error(e.getMessage());
288         }
289         return kaMsg;
290     }
291 }