How to derive this simple geometric relationship using cosine law? 
Given the above figure, I need to show that
$$cos(a_2) = \frac{x_1^2 + y_1^2 - L_1^2 - L_2^2}{2L_1L_2} $$
Where $L_1, L_2$ are the length of the red lines respectively, and $a_1, a_2$ are the angles
How to show this holds by using cosine law?
Can anyone show me how this can be done?

since
$$a_{2}=\pi-\angle BAO\Longrightarrow \cos{a_{2}}=-\cos{\angle BAO}$$
and sinc $B(x_{1},y_{1})$,then we have
$$|OB|^2=x^2_{1}+y^2_{1}$$
use cosine
$$|OB|^2=|OA|^2+|AB|^2-2|OA|\cdot|AB|\cdot\cos{(\angle OAB)}$$
$$\Longrightarrow x^2_{1}+y^2_{1}=L^2_{1}+L^2_{2}+2L_{1}\cdot L_{2}\cos{a_{2}}$$
so
$$\cos{a_{2}}=\dfrac{x^2_{1}+y^2_{1}-L^2_{1}-L^2_{2}}{2L_{1}L_{2}}$$
