Merge changes If0630105,I9d2d5e61,I1cea2a32,Icc05b6a7,Ic57eb4f8, ...
[packetcable.git] / packetcable-driver / src / main / java / org / umu / cops / ospdp / COPSPdpOSMsgSender.java
1 package org.umu.cops.ospdp;
2
3 import org.umu.cops.prpdp.COPSPdpMsgSender;
4 import org.umu.cops.stack.*;
5 import org.umu.cops.stack.COPSContext.RType;
6 import org.umu.cops.stack.COPSDecision.Command;
7 import org.umu.cops.stack.COPSHeader.Flag;
8
9 import java.io.IOException;
10 import java.net.Socket;
11 import java.util.*;
12
13 /**
14  * COPS message transceiver class for outsourcing connections at the PDP side.
15  *
16  * TODO - change all references of Vector to List<>
17  */
18 public class COPSPdpOSMsgSender extends COPSPdpMsgSender {
19
20     /**
21      * Creates a COPSPepMsgSender
22      *
23      * @param clientType        COPS client-type
24      * @param clientHandle      Client handle
25      * @param sock              Socket to the PEP
26      */
27     public COPSPdpOSMsgSender(final short clientType, final COPSHandle clientHandle, final Socket sock) {
28         super(clientType, clientHandle, sock);
29     }
30
31     /**
32      * Sends a decision message which was requested by the PEP
33      * @param removeDecs    Decisions to be removed
34      * @param installDecs   Decisions to be installed
35      * @throws COPSPdpException
36      */
37     public void sendSolicitedDecision(Vector removeDecs, Vector installDecs) throws COPSPdpException {
38         sendDecision(removeDecs, installDecs, true);
39     }
40
41     /**
42      * Sends a decision message which was not requested by the PEP
43      * @param removeDecs    Decisions to be removed
44      * @param installDecs   Decisions to be installed
45      * @throws COPSPdpException
46      */
47     public void sendUnsolicitedDecision(Vector removeDecs, Vector installDecs) throws COPSPdpException {
48         sendDecision(removeDecs, installDecs, false);
49     }
50
51     /**
52      * Sends a decision message to the PEP
53      * @param removeDecs    Decisions to be removed
54      * @param installDecs   Decisions to be installed
55      * @param solicited     <tt>true</tt> if the PEP requested this decision, <tt>false</tt> otherwise
56      * @throws COPSPdpException
57      */
58     public void sendDecision(Vector removeDecs, Vector installDecs, boolean solicited) throws COPSPdpException {
59         // Common Header holding the same ClientType as the request
60         final Flag flag;
61         if (solicited)
62             flag= Flag.SOLICITED;
63         else
64             flag = Flag.UNSOLICITED;
65
66         final Map<COPSContext, Set<COPSDecision>> decisions = new HashMap<>();
67
68         // Decisions (no flags supplied)
69         //  <Context>
70         final COPSContext cntxt = new COPSContext(RType.CONFIG, (short)0);
71
72         // Remove Decisions
73         //  <Decision: Flags>
74         final COPSDecision rdec1;
75         if (installDecs.size() == 0)
76             rdec1 = new COPSDecision(Command.REMOVE);
77         else
78             rdec1 = new COPSDecision(Command.INSTALL);
79
80         if (decisions.get(cntxt) == null) {
81             final Set<COPSDecision> decisionSet = new HashSet<>();
82             decisionSet.add(rdec1);
83             decisions.put(cntxt, decisionSet);
84         } else {
85             decisions.get(cntxt).add(rdec1);
86         }
87
88         // Client Handle with the same clientHandle as the request
89         final COPSDecisionMsg decisionMsg = new COPSDecisionMsg(1, flag, getClientType(), _handle, decisions,
90                 null, null);
91
92         //** Send decision
93         //**
94         try {
95             decisionMsg.writeData(_sock);
96         } catch (IOException e) {
97             throw new COPSPdpException("Failed to send the decision, reason: " + e.getMessage());
98         }
99     }
100
101 }