4 package org.pcmm.messages.impl;
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 import org.umu.cops.stack.COPSClientSI.CSIType;
15 import org.umu.cops.stack.COPSContext.RType;
16 import org.umu.cops.stack.COPSDecision.Command;
17 import org.umu.cops.stack.COPSDecision.DecisionFlag;
18 import org.umu.cops.stack.COPSError.ErrorTypes;
19 import org.umu.cops.stack.COPSHeader.ClientType;
20 import org.umu.cops.stack.COPSHeader.OPCode;
21 import org.umu.cops.stack.COPSObjHeader.CType;
23 import java.net.InetAddress;
30 public class MessageFactory implements IMessageFactory {
32 /** Default keep-alive timer value (secs) */
33 public static final short KA_TIMER_VALUE = 30;
34 /** Default accounting timer value (secs) */
35 public static final short ACCT_TIMER_VALUE = 0;
37 private static final Logger logger = LoggerFactory.getLogger(MessageFactory.class);
39 private static final MessageFactory instance = new MessageFactory();
41 private MessageFactory() {
44 public static MessageFactory getInstance() {
51 * @see pcmm.messages.IMessageFactory#create(pcmm.messages.MessageType)
53 public COPSMsg create(final OPCode messageType) {
54 return create(messageType, new Properties());
60 * @see org.pcmm.messages.IMessageFactory#create(org.pcmm.messages.IMessage.
61 * MessageType, java.util.Properties)
63 public COPSMsg create(final OPCode messageType, final Properties properties) {
64 logger.info("Creating message of type - " + messageType);
65 // return new PCMMMessage(messageType, content);
66 switch (messageType) {
68 return createOPNMessage(properties);
70 return createREQMessage(properties);
72 return createCATMessage(properties);
74 return createCCMessage(properties);
76 return createDECMessage(properties);
80 return createKAMessage(properties);
93 * @param prop - the properties
94 * @return - the message
96 protected COPSMsg createDECMessage(final Properties prop) {
98 // ===common part between all gate control messages
99 // final COPSHeader hdr = new COPSHeader(OPCode.DEC, IPCMMClient.CLIENT_TYPE);
103 // TODO - the old command and flag codes are not congruent with the ones described in COPSDecision
104 // TODO - what is the correct client type to be using here???
106 if (prop.get(MessageProperties.DECISION_CMD_CODE) != null)
107 decision.setCmdCode((byte) prop.get(MessageProperties.DECISION_CMD_CODE));
108 if (prop.get(MessageProperties.DECISION_FLAG) != null)
109 decision.setFlags((short) prop.get(MessageProperties.DECISION_FLAG));
113 if (prop.get(MessageProperties.GATE_CONTROL) != null)
114 data = (COPSData) prop.get(MessageProperties.GATE_CONTROL);
118 // TODO - Need to determine is SIGNALED is the correct default CSIType
119 // Decided that CSI object is not what should be encapsulated by the COPSDecisionMsg, therefore placing
120 // data into the COPSDecision object located in the decisionMap
121 // final COPSClientSI si = new COPSClientSI(CSIType.SIGNALED, data);
123 final COPSHandle handle;
124 if (prop.get(MessageProperties.CLIENT_HANDLE) != null) {
125 handle = new COPSHandle(new COPSData((String) prop.get(MessageProperties.CLIENT_HANDLE)));
128 // TODO - This smells wrong to have a null handle ID
129 handle = new COPSHandle(null);
131 final Set<COPSDecision> decisionSet = new HashSet<>();
132 decisionSet.add(new COPSDecision(CType.DEF, Command.NULL, DecisionFlag.NA, data));
133 final Map<COPSContext, Set<COPSDecision>> decisionMap = new HashMap<>();
134 decisionMap.put(new COPSContext(RType.CONFIG, (short)0), decisionSet);
136 final COPSDecisionMsg msg = new COPSDecisionMsg(ClientType.TYPE_1, handle, decisionMap, null);
138 // TODO - determine why this block has been commented out
140 // msg.dump(System.out);
141 // } catch (IOException unae) {
148 * creates a Client-Open message.
152 * @return COPS message
154 protected COPSMsg createOPNMessage(final Properties prop) {
155 // version infor object
156 short majorVersion = MMVersionInfo.DEFAULT_MAJOR_VERSION_INFO;
157 short minorVersion = MMVersionInfo.DEFAULT_MINOR_VERSION_INFO;
158 if (prop.get(MessageProperties.MM_MAJOR_VERSION_INFO) != null)
159 majorVersion = (Short) prop.get(MessageProperties.MM_MAJOR_VERSION_INFO);
160 if (prop.get(MessageProperties.MM_MINOR_VERSION_INFO) != null)
161 minorVersion = (Short) prop.get(MessageProperties.MM_MINOR_VERSION_INFO);
162 // Mandatory MM version.
163 byte[] versionInfo = new MMVersionInfo(majorVersion, minorVersion).getAsBinaryArray();
164 final COPSClientSI clientSI = new COPSClientSI(CSIType.SIGNALED, new COPSData(versionInfo, 0, versionInfo.length));
167 if (prop.get(MessageProperties.PEP_ID) != null)
168 d = new COPSData((String) prop.get(MessageProperties.PEP_ID));
170 d = new COPSData(InetAddress.getLocalHost().getHostName());
171 final COPSPepId pepId = new COPSPepId(d);
172 return new COPSClientOpenMsg(ClientType.NA, pepId, clientSI, null, null);
173 } catch (Exception e) {
174 logger.error("Error creating OPN message", e);
177 // TODO - this probably should not return null and throw an exception instead
182 * creates a Client-Accept message.
183 * @param prop - properties
184 * @return COPS message
186 protected COPSMsg createCATMessage(final Properties prop) {
187 // TODO - determine what the first constructor parameter really should be???
188 final COPSKATimer katimer;
189 if (prop.get(MessageProperties.KA_TIMER) != null)
190 katimer = new COPSKATimer((short)prop.get(MessageProperties.KA_TIMER));
192 katimer = new COPSKATimer(KA_TIMER_VALUE);
194 // TODO - determine what the first constructor parameter really should be???
195 final COPSAcctTimer acctTimer;
196 if (prop.get(MessageProperties.ACCEPT_TIMER) != null)
197 acctTimer = new COPSAcctTimer((short) prop.get(MessageProperties.ACCEPT_TIMER));
199 acctTimer = new COPSAcctTimer(ACCT_TIMER_VALUE);
201 if (acctTimer.getTimerVal() != 0)
202 return new COPSClientAcceptMsg(IPCMMClient.CLIENT_TYPE, katimer, acctTimer, null);
203 else return new COPSClientAcceptMsg(IPCMMClient.CLIENT_TYPE, katimer, null, null);
207 * creates a Client-Close message.
211 * @return COPS message
213 protected COPSMsg createCCMessage(final Properties prop) {
215 if (prop.get(MessageProperties.ERR_MESSAGE) != null) {
216 ErrorTypes code = ErrorTypes.NA;
217 final ErrorTypes error = (ErrorTypes) prop.get(MessageProperties.ERR_MESSAGE);
218 if (prop.get(MessageProperties.ERR_MESSAGE_SUB_CODE) != null)
219 code = (ErrorTypes) prop.get(MessageProperties.ERR_MESSAGE_SUB_CODE);
220 err = new COPSError(COPSError.ERROR_CODE_TO_TYPE.get(error.ordinal()),
221 COPSError.ERROR_CODE_TO_TYPE.get(code.ordinal()));
223 err = new COPSError(ErrorTypes.UNKNOWN, ErrorTypes.NA);
225 return new COPSClientCloseMsg(IPCMMClient.CLIENT_TYPE, err, null, null);
229 * creates a Request message
233 * @return Request message
235 protected COPSMsg createREQMessage(final Properties prop) {
237 if (prop.get(MessageProperties.R_TYPE) != null)
238 rType = (Short) prop.get(MessageProperties.R_TYPE);
239 else rType = ICMTS.DEFAULT_R_TYPE;
242 if (prop.get(MessageProperties.M_TYPE) != null)
243 mType = (Short) prop.get(MessageProperties.M_TYPE);
244 else mType = ICMTS.DEFAULT_M_TYPE;
246 final COPSContext copsContext = new COPSContext(COPSContext.VAL_TO_RTYPE.get((int)rType), mType);
247 final COPSHandle copsHandle;
248 if (prop.get(MessageProperties.CLIENT_HANDLE) != null)
249 copsHandle = new COPSHandle(new COPSData((String) prop.get(MessageProperties.CLIENT_HANDLE)));
251 // just a random handle
252 copsHandle = new COPSHandle(new COPSData("" + Math.random() * 82730));
254 return new COPSReqMsg(ClientType.TYPE_1, copsHandle, copsContext, null, null, null, null, null);
258 * creates a Keep-Alive message.
262 * @return COPS message
263 * TODO - Why is there a timer being instantiated but never used?
265 protected COPSMsg createKAMessage(final Properties prop) {
266 // TODO - determine why this isn't really doing anything
267 return new COPSKAMsg(ClientType.NA, null);
269 final COPSKATimer timer;
270 if (prop.get(MessageProperties.KA_TIMER) != null)
271 timer = new COPSKATimer((short)0, (Short) prop.get(MessageProperties.KA_TIMER));
273 timer = new COPSKATimer();